Magento 2 Docker Configuration: Step-by-Step Tutorial

Magento 2 Docker Configuration: Step-by-Step Tutorial

Magento 2 docker is a flexible e-commerce platform for scaling Magento 2 stores. It manages all store processes, such as shipping, billing, and checkout. If you face issues while setting it up, this tutorial offers a simple, step-by-step process for effectively installing and configuring Magento 2 with Docker. Ready for hassle-free setup? Let's dive in!

Key Takeaways

  • Learn about the powerful tool for Magento e-commerce, combining Magento and Docker for a seamless online store setup.

  • Understand how Docker simplifies Magento development, offering a flexible environment for coding and testing.

  • Follow easy steps to install and configure Magento 2 Docker, making the setup process hassle-free.

  • Get insights on enhancing Docker performance with Magento 2 for a smoother development experience.

  • Identify and solve typical performance challenges in Magento 2 and Docker environments.

  • Explore strategies for improving Docker filesystem mounting performance in Magento setups.

  • Find quick answers to common queries about Magento Docker, streamlining your e-commerce development journey.

Importance of Docker for Magento Development

Magento is an ecommerce platform for creating online stores. Conversely, Docker is good for creating and using small, simple environments for building, testing, and deploying applications.


Magento and Docker combine to create a seamless way to build your online store. Docker simplifies the deployment and running of applications in containers, making it easier to build, test, and launch a Magento store.


With Docker, Magento 2 development and setup become more manageable, making them an ideal duo for creating an e-commerce site. Docker ensures a seamless experience when creating or writing code for an online store. A few notable benefits of using the Docker engine are:

  • Enhanced portability is achieved through multi-functionality and cloud-friendliness.

  • Consistency is ensured across online and offline environments, simplifying application development.

  • Operations are secured for a seamless user experience.

  • The environment is easily configured for local development of Magento applications.

  • Compatibility with various operating systems allows for easy deployment across different environments.

How to Install and Configure Magento 2 Docker

1. Build a project by using Composer.

To start setting up Magento 2, the first step is to create a directory. After that, you can proceed with running the code below:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <install-directory-name>

The provided command above will install the most recent release of Magento. If you wish to install a particular version of Magento, input the corresponding version number. For instance:

magento/project-community-edition=2.4.2

2. Switch to the Project directory.

For <install-directory-name>, you might have given a name like example.com. Navigate to the project directory on the server before executing the next set of instructions. cd <install-directory-name>

3. Include the ece-tools and Cloud Docker in the setup.

Cloud Docker allows easy deployment of Magento 2.4 in a Docker environment, serving purposes like development, testing, and automation. To include the ece-tools and cloud docker for community packages, execute the command b. composer require --no-update --dev magento/ece-tools magento/magento-cloud-docker

4. Create a default configuration source file

We now generate the default configuration source file called .magento.docker.yml. This file is utilized to construct the Docker containers for the local environment.

Here's the configuration for Magento 2: -

system:
    mode: 'production'
services:
    php:
        version: '7.4'
        extensions:
            enabled:
                - xsl
                - json
                - redis
    mysql:
        version: '10.4'
        image: 'mariadb'
    redis:
        version: '6.0'
        image: 'redis'
    elasticsearch:
        version: '7.9.0'
        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'
mounts:
    var:
        path: 'var'
    app-etc:
        path: 'app/etc'
    pub-media:
        path: 'pub/media'
    pub-static:
        path: 'pub/static'
        

5. Use the Magento Installation Script

During installation, the script will include the necessary template dependencies and set up the default hostname in your /etc/hosts file.

curl -sL https://github.com/magento/magento-cloud-docker/releases/download/1.2.3/init-docker.sh | bash -s -- --php 7.4

6. Update the project dependencies

To update the project dependencies, use the following command:

composer update

7. Generate the Docker Compose Configuration file

./vendor/bin/ece-docker build:compose --mode="developer"

Once the script finishes, you'll come across a file named docker-compose.yml. This file serves as the configuration file for Docker Compose.


Occasionally, the docker image may be generated improperly. Modifying and rectifying the file based on the corresponding Magento version is necessary in such cases. Additionally, please update the Tags image version on the Magento Docker hub.

8. Add auth.json file

To access the Magento Marketplace, first log in to your account. Then, navigate to your profile and choose the option for Access Keys. If needed, you can also create a new access key.

Navigating to the Magento Marketplace for Access Keys

The and must be accessible from the panel. Create a file named auth.json with the following content in your project directory:

   {
    "http-basic": {
        "repo.magento.com": {
            "username": "<public-key>",
            "password": "<private-key>"
        }
    }
}
    

9. Build files to the container.

To build files into the container and run them in the background, use the command below: docker-compose up -d

10. Deploy Magento as a Docker container

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

11. Configure and Connect Varnish

Next, set up varnish and connect it with Magento.

docker-compose run --rm deploy magento-command config:set system/full_page_cache/caching_application 2 --lock-env
    
docker-compose run --rm deploy magento-command setup:config:set --http-cache-hosts=varnish
    

12. Clear the Cache

To complete the process, clear the cache and verify the results. Use the command below to clear the cache:

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

Access the local Magento Cloud template using Magento 2 docker.

Log in to the Admin panel using the default credentials:

  • Username = Admin

  • Password = 123123qWe have finished installing Magento 2 Docker and can now use it on any operating system.

Tips to Optimize Docker Performance with Magento 2

Optimizing Docker performance with Magento 2 is crucial for smooth and efficient development. Here are some helpful tips to enhance your Docker setup:

  • Use a lightweight base image: Choose a minimalistic and optimized Docker base image specific to Magento 2. This reduces the container's size and improves startup time.

  • Optimize caching mechanisms: Use Redis for Magento's full-page cache, session storage, and metadata caching. This significantly enhances performance by reducing database queries.

  • Adjust memory limits: Allocate sufficient memory resources to your Docker containers based on the requirements of Magento 2 and any Magento extensions or modules being used.

  • Implement volume mounting strategies: Mount frequently accessed directories such as pub/media, var/log, var/session, etc., as separate volumes outside the container for better I/O performance.

  • Enable OPCache: Configure PHP's OPCache extension to store precompiled script bytecode in shared memory, resulting in faster execution times.

  • Fine-tune MySQL settings: Modify MySQL configuration parameters like innodb_buffer_pool_size, key_buffer_size based on your application needs to optimize database performance.

  • Monitor resource utilization: Use tools like Grafana or Prometheus to regularly track CPU, memory usage, and disk I/O metrics to identify bottlenecks and make necessary adjustments.

By optimizing your Docker environment for Magento 2 development, you can improve performance and efficiency throughout the development lifecycle.

Common Performance Issues with Magento 2 and Docker

Magento 2 and Docker may face some performance issues that impact your online store's speed and efficiency. These issues include slow page loading, high resource consumption, and database connection problems. Optimizing performance is crucial for a seamless shopping experience for your customers.


One common performance issue is related to filesystem mounting in Docker. When using Docker, the communication between Magento's file system and Docker containers can be slow, causing file access delays. This can impact the overall performance of your website. Optimize NFS settings to boost filesystem mounting performance or switch to a different file system like OverlayFS or AUFS. Implementing these solutions can boost file access speed in your Magento environment.


Another important aspect is resource allocation for running Magento 2 with Docker. Inadequate memory or CPU limits on containers can cause slow response times and decreased performance.


Allocating enough resources based on your website's needs is recommended. By addressing common performance issues in Magento 2 and Docker, you can optimize your online store's speed and enhance customer user experience.

Solutions to Improve Docker Filesystem Mounting Performance

  • Selective Sync setup greatly improves Docker performance with large file systems in a Magento 2 development environment.

  • Using a volume mount approach can improve Docker filesystem mounting performance for the Magento Docker setup guide.

  • Mounting NFS volumes can enhance Docker file system performance, particularly for Magento 2.

FAQs

1. What is Magento Docker for commerce?

Magento Docker for commerce provides an environment for development that's easy to manage on the cloud docker, specifically tailored for Adobe Commerce on a cloud infrastructure project.


2. How can I set up Magento 2 with Adobe Commerce?

To start new Magento 2 projects with Adobe Commerce, access keys from the Magento marketplace and properly deploy them within your host machine.


3. Is any special software needed?

To run your e-commerce website smoothly on the Magento Cloud, you must install software like Elasticsearch or Opensearch, SSH, Xdebug, and Nginx.


4. What if I want to modify CSS files or create custom commands in my setup?

You can do this using VS Code or vscode and running appropriate custom cli commands under your hostname.


5. Is PHPMyAdmin used in setting up a cloud Docker?

PHPMyAdmin is critical in managing MySQL databases while setting up a Magento base in this environment.


6. What should you consider in a hosting provider if you’re using Magento 2 Docker?

When choosing a Magento hosting provider for Magento 2 Docker, consider their reliability, speed, and support for Docker technology. Ensure they offer scalable solutions that can accommodate your business growth. Also, check for 24/7 customer support to resolve any issues swiftly.

Summary

Setting up Magento 2 Docker can greatly simplify the development process for Magento store owners and developers. We suggest using the docker-magento GitHub repository for a simple Magento installation with Docker.


This tutorial covered all the steps to install and configure Magento in a Docker environment. Start using Docker to streamline your Magento development. Consider using a Magento auto-scaling hosting provider that supports Docker and provides optimized servers for Magento.

Magento Hosting Free Demo on AWS

Shivendra Tiwari
Shivendra Tiwari
Technical Writer

Shivendra has over ten years of experience creating compelling content on Magento-related topics. With a focus on the Magento community, he shares valuable tips and up-to-date trends that provide actionable insights.


Get the fastest Magento Hosting! Get Started