Loading...

How to Install Magento 2 Using Docker?

5 Mins
Jayram Prajapati  ·   04 Dec 2024
Magento 2 Installation Guide
service-banner

If you want to install magento 2 on your device, Elightwalk always suggests that you install it using Docker. It is easy to install an error-free process for beginners. You can find thousands of documents on how to install Magento 2, but they will be stuck in the middle of installation. Here is the proper beginner's guide for installing Magento 2 on your computer with detailed instructions.

Prerequisites

Ensure the following are installed and properly configured on your system:

Docker and Docker Compose

Required for containerizing and managing multi-container applications.

Verify installation with:

docker --version
docker-compose --version

PHP 8.2 or Later

Necessary for running the application.

Check the version with:

php -v

Composer

Used to manage PHP dependencies.

Verify installation with:

composer --version

Ubuntu-Based Operating System (or Similar)

A stable, compatible environment such as Ubuntu 20.04 or later. Ensure your system is up to date with:

sudo apt update && sudo apt upgrade

So here is the stage first for. Your system is ready to install Magento 2. If you have any problems here, then visit our blog page for more information.

Now follow the one-by-one steps to install Magento 2 error-free!

Step 1: Install PHP and Required Extensions

To set up PHP and its necessary extensions, run the following commands:

sudo add-apt-repository ppa:ondrej/php
sudo apt install php8.2 -y
sudo apt install php8.2-bcmath php8.2-intl php8.2-soap php8.2-zip php8.2-gd php-mysql php8.2-curl php8.2-cli php8.2-xml php8.2-xmlrpc php8.2-gmp php8.2-common php8.2-sodium

This command ensures that all necessary PHP modules are installed for your application to function properly.

Step 2: Set Up Magento Project

Create Magento Project

Run the following commands to create a new Magento project:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 magento246
cd magento246

Add Development Tools

Add the required development tools to your project:

composer require --no-update --dev magento/ece-tools magento/magento-cloud-docker

When you run these commands, your Magento project is set up, and the necessary development tools are added.

Step 3: Configure Docker Environment

Create .magento.docker.yml File

Create a configuration file with the following content to define your Docker environment:

name: magento
system:
    mode: "production"
services:
    php:
        version: "8.2"
        extensions:
            enabled:
                - xsl
                - json
                - redis
    mysql:
        version: "10.6"
        image: "mariadb"
    redis:
        version: "7.0"
        image: "redis"
    elasticsearch:
        version: "7.10"
        image: "magento/magento-cloud-docker-elasticsearch"
    hooks:
        build: |
            set -e
            php ./vendor/bin/ece-tools run scenario/build/generate.xml
            php ./vendor/bin/ece-tools run scenario/build/transfer.xml
        deploy: "php ./vendor/bin/ece-tools run scenario/deploy.xml"
        post_deploy: "php ./vendor/bin/ece-tools run scenario/post-deploy.xml"
   var:
        path: "var"
    app-etc:
        path: "app/etc"
    pub-media:
        path: "pub/media"
    pub-static:
        path: "pub/static"

Initialize Docker Setup

Run the following commands to initialize and build your Docker environment:

curl -sL https://github.com/magento/magento-cloud-docker/releases/download/1.2.3/init-docker.sh | bash -s -- --php 8.2
composer update
./vendor/bin/ece-docker build:compose --mode="developer"

Step 4: Modify docker-compose.yml

Update your docker-compose.yml file based on the provided configuration in the guide. Adjust ports and aliases to match your preferences.

# ./vendor/bin/ece-docker 'build:compose' --mode=developer
    version: '2.1'
    services:
    db:
        hostname: db.magento2.docker
        image: 'mariadb:10.6'
        shm_size: 2gb
        environment:
        - MYSQL_ROOT_PASSWORD=magento2
        - MYSQL_DATABASE=magento2
        - MYSQL_USER=magento2
        - MYSQL_PASSWORD=magento2
        ports:
        - '3306'
        volumes:
        - '.:/app:delegated'
        - 'magento-magento-db:/var/lib/mysql'
        healthcheck:
        test: 'mysqladmin ping -h localhost -pmagento2'
        interval: 30s
        timeout: 30s
        retries: 3
        networks:
        magento:
            aliases:
            - db.magento2.docker
    redis:
        hostname: redis.magento2.docker
        image: 'redis:7.0'
        volumes:
        - '.:/app:delegated'
        ports:
        - 6379
        sysctls:
        net.core.somaxconn: 1024
        ulimits:
        nproc: 65535
        nofile:
            soft: 20000
            hard: 40000
        healthcheck:
        test: 'redis-cli ping || exit 1'
        interval: 30s
        timeout: 30s
        retries: 3
        networks:
        magento:
            aliases:
            - redis.magento2.docker
    elasticsearch:
        hostname: elasticsearch.magento2.docker
        image: 'magento/magento-cloud-docker-elasticsearch:7.10-1.3.6'
        ulimits:
        memlock:
            soft: -1
            hard: -1
        environment:
        - cluster.name=docker-cluster
        - bootstrap.memory_lock=true
        networks:
        magento:
            aliases:
            - elasticsearch.magento2.docker
    fpm:
        hostname: fpm.magento2.docker
        image: 'magento/magento-cloud-docker-php:8.2-fpm-1.3.6'
        extends: generic
        volumes:
        - '.:/app:delegated'
        networks:
        magento:
            aliases:
            - fpm.magento2.docker
        depends_on:
        db:
            condition: service_healthy
    web:
        hostname: web.magento2.docker
        image: 'magento/magento-cloud-docker-nginx:1.19-1.3.6'
        extends: generic
        volumes:
        - '.:/app:delegated'
        environment:
        - WITH_XDEBUG=0
        - NGINX_WORKER_PROCESSES=1
        - NGINX_WORKER_CONNECTIONS=1024
        networks:
        magento:
            aliases:
            - web.magento2.docker
        depends_on:
        fpm:
            condition: service_started
    varnish:
        hostname: varnish.magento2.docker
        image: 'magento/magento-cloud-docker-varnish:6.6-1.3.6'
        networks:
        magento:
            aliases:
            - varnish.magento2.docker
        depends_on:
        web:
            condition: service_started
    phpmyadmin:
        hostname: phpmyadmin.magento2.docker
        restart: always
        image: phpmyadmin/phpmyadmin:latest
        networks:
        magento:
            aliases:
            - magento2.docker
        environment:
        - MYSQL_ROOT_PASSWORD=magento2
        - PMA_USER=magento2
        - PMA_PASSWORD=magento2
        ports:
        - "8080:80"
        depends_on:
        - db
    tls:
        hostname: tls.magento2.docker
        image: 'magento/magento-cloud-docker-nginx:1.19-1.3.6'
        extends: generic
        networks:
        magento:
            aliases:
            - magento2.docker
        environment:
        - NGINX_WORKER_PROCESSES=1
        - NGINX_WORKER_CONNECTIONS=1024
        - UPSTREAM_HOST=varnish
        - UPSTREAM_PORT=80
        ports:
        - '80:80'
        - '443:443'
        depends_on:
        varnish:
            condition: service_started
    generic:
        hostname: generic.magento2.docker
        image: 'magento/magento-cloud-docker-php:8.2-cli-1.3.6'
        env_file: ./.docker/config.env
        environment:
        - MAGENTO_RUN_MODE=developer
        - 'PHP_EXTENSIONS=bcmath bz2 calendar exif gd gettext intl mysqli pcntl pdo_mysql soap sockets sysvmsg sysvsem sysvshm opcache zip xsl sodium redis'
    build:
        hostname: build.magento2.docker
        image: 'magento/magento-cloud-docker-php:8.2-cli-1.3.6'
        extends: generic
        volumes:
        - '.:/app:delegated'
        networks:
        magento-build:
            aliases:
            - build.magento2.docker
        depends_on:
        db:
            condition: service_healthy
        redis:
            condition: service_healthy
        elasticsearch:
            condition: service_healthy
    deploy:
        hostname: deploy.magento2.docker
        image: 'magento/magento-cloud-docker-php:8.2-cli-1.3.6'
        extends: generic
        volumes:
        - '.:/app:delegated'
        networks:
        magento:
            aliases:
            - deploy.magento2.docker
        depends_on:
        db:
            condition: service_healthy
        redis:
            condition: service_healthy
        elasticsearch:
            condition: service_healthy
    volumes:
    magento-magento-db: {  }
    networks:
    magento:
        driver: bridge
    magento-build:
        driver: bridge

Step 5: Set Up Authentication

Create an auth.json file with Magento authentication keys. Replace the username and password with your Magento Marketplace credentials.

{
    "http-basic": {
        "repo.magento.com": {
            "username": "your-username",
            "password": "your-password"
        }
    }
}

Place this file in the root directory of your project to enable secure access to Magento packages and updates. Verify the setup by running composer install or composer update.

Step 6: Build and Start Docker Containers

Start the Containers

Use the following command to start the Docker containers in detached mode:

docker-compose up -d

Deploy Magento on Docker

Deploy Magento within the Docker environment:

docker-compose run --rm deploy cloud-deploy

Switch to Developer Mode

Set Magento to developer mode for development and debugging:

docker-compose run --rm deploy magento-command deploy:mode:set developer

Run Post-Deployment Tasks

Execute any remaining post-deployment tasks:

docker-compose run --rm deploy cloud-post-deploy

Now, your Magento setup on Docker is ready for use.

Step 7: Configure Magento

Set Full-Page Cache

Enable and configure the full-page cache application:

docker-compose run --rm deploy magento-command config:set system/full_page_cache/caching_application 2 --lock-env

Configure HTTP Cache Hosts

Set up HTTP cache hosts to use Varnish:

docker-compose run --rm deploy magento-command setup:config:set 
--http-cache-hosts=varnish

Clean the Cache

Clear Magento's cache to apply the changes:

docker-compose run --rm deploy magento-command cache:clean

If you follow these steps, your Magento instance will be optimized and correctly configured for caching.

Step 8: Optional Setup

Deploy Sample Data

To deploy Magento's sample data for testing or development, run:

docker-compose run --rm deploy magento-command sampledata:deploy

This will install Magento's sample products, categories, and other data for your store.

Create an Admin User

To create an admin user for logging into the Magento admin panel, use the following command:

docker-compose run --rm deploy magento-command admin:user:create --admin-user=admin --admin-password=admin123 --admin-email=admin@example.com --admin-firstname=Admin --admin-lastname=User
  • --admin-user: Specifies the username for the admin account (e.g., test).
  • --admin-password: Sets the password for the admin account (e.g., test@123).
  • --admin-email: Defines the email associated with the admin account.
  • --admin-firstname: First name of the admin user.
  • --admin-lastname: Last name of the admin user.

After executing the command, you should see a confirmation message indicating the admin user has been created successfully.

This will create a new admin user with the specified credentials. You can then log-in to the Magento admin interface at http://localhost/admin (or the relevant URL based on your configuration).

Accessing the Magento Store

Once the admin user is created, you can access the following:

Storefront:

  • URL: http://magento2.docker/
  • This is the public-facing part of your Magento store where customers can browse and shop.

Admin Panel:

  • URL: http://magento2.docker/admin
  • Use the credentials you set during the admin creation process:
    • Username: admin
    • Password: admin@123

The Admin Panel allows you to manage products, customers, orders, and the overall store configuration.

Optional Service: phpMyAdmin

If you included phpMyAdmin in your Docker setup, you can access it to manage the database directly:

  • URL: http://localhost:8080
  • Username: magento2
  • Password: magento2

Use phpMyAdmin for advanced database operations, troubleshooting, or backups.

Essence

After completing the Magento 2 Docker setup, you can manage your store using the Admin Panel and access phpMyAdmin for database management. Our Magento Developers have created a number of detailed documents on our Blogs page. You can visit Elightwalk Technology for more information and tutorials on optimizing your Magento 2 store for better performance and user experience. Contact us to get help with any additional customization or support requests you may have.

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Unlock The Power Of Plugins In Magento 2: A Comprehensive Guide

Get started on utilizing Magento 2 plugins with our comprehensive tutorial. Master the art of plugin building and integration to improve the functionality and customization of your e-commerce shop.

Augmented Reality (AR) the Future of eCommerce Business
Augmented reality (AR) is changing eCommerce by making the shopping experience better for customers, getting them more involved, and increasing sales in the online market.
Easy Ways to Check If an Array is Empty in JavaScript
In this quick guide for checking an empty array in JavaScript, we streamline the code and create an easy way to check an empty array in simple steps. Carefully read this guide. This will help you efficiently address empty arrays in your JavaScript code.