Magento 2 Docker Setup Guide: Steps By Steps
Vinh Jacker | 08-03-2021
If you are searching “How to set up Magento 2 on Docker”, “Magento 2 Docker Setup” or “Set up Magento 2 on Docker”, you come to the right post.
Magento 2 is one of the powerful open-source eCommerce platforms that is used to manage all store processes like shipping, billing, checkout, and more. Docker is a development environment platform using an operating system (OS). The integration of Magento 2 and Docker platforms will make your online store more powerful and flexible.
This post will show you a step-by-step guide on How to Set Up Magento 2 on Docker.
Without further ado, let’s jump right into the details!
Why Docker Important for Magento Development?
Magento is one of the leading platforms for building online stores, and Docker is a tool for creating simple, efficient environments to build, test, and deploy applications.
When we use Magento and Docker together, it’s easier to develop eCommerce businesses. Docker simplifies the process of setting up and managing Magento stores by providing a consistent environment for development and deployment.
Using Docker with Magento 2 offers several benefits:
- Easy Portability: Work well with different systems and cloud services
- Consistency: Ensure your local setup matches your live site, reducing errors
- Security: Keep your operations safe and smooth
- Simple Configuration: Quickly set up local environments for Magento development
- Cross-Platform Compatibility: Run seamlessly on different operating systems
With Docker, building and managing a Magento store becomes simpler and more efficient, saving time and effort for developers.
How to Set up Magento 2 on Docker
Some common prerequisites:
Install Docker
Obviously, the first step in Magento 2 Docker Setup you have to do is to download and install Docker. Now the Docker platform is available on all operating systems. Whether you are using any operating system, you can also install and start it up.
Install Composer
In many different ways to install and manage Magento 2, Composer is the first preferred choice to set up. Because installing any plugin/ platform via Composer is always accessible and quick compared to other ways. Besides, Composer also enables you to manage, install packages, and run additional scripts after each update.
Download Magento 2
To download Magento 2 for stores, please follow the guide below:
- You need to access Mageplaza to download Magento 2.
- After logging in, navigate to Access Keys, and this time, please keep that window open because the keys are necessary for step 3.
- Decide a location on your local machine that you want to put the website files to live.
- Please open a command line terminal window. Please replace the placeholder path with the final path where you will install Magento, before copying and pasting the code below.
cd /path/to/where/you/will/download/magento && \
composer create-project --repository-url=https://repo.magento.com/
magento/project-community-edition
- Please choose the version you want and press the download button. Afterward, you will be redirected to the directory where you installed the Magento,
/path/to/magento
. - At this point, please increase the PHP memory limit from the default value of 765M to 2048M.
- Finally, select a domain name you would like to access the site, then add it to your host’s file.
sudo -- sh -c "echo '127.0.0.1 local.domain.com' >> /etc/hosts"
That’s all the prerequisites. Let’s explore 3 important Magento 2 docker setup steps !
Step 1: Generate a docker-composer.yml file
#1. You need to select a place to put your Docker configuration files. Refer this location as /path/to/docker
.
#2. Next, open a new document.
#3. Now, click on “copy” and paste the following code into a new file:
version: '3'
services:
web:
image: webdevops/php-apache-dev:ubuntu-16.04
container_name: web
restart: always
user: application
environment:
- WEB_ALIAS_DOMAIN=local.domain.com
- WEB_DOCUMENT_ROOT=/app/pub
- PHP_DATE_TIMEZONE=EST
- PHP_DISPLAY_ERRORS=1
- PHP_MEMORY_LIMIT=2048M
- PHP_MAX_EXECUTION_TIME=300
- PHP_POST_MAX_SIZE=500M
- PHP_UPLOAD_MAX_FILESIZE=1024M
volumes:
- /path/to/magento:/app:cached
ports:
- "80:80"
- "443:443"
- "32823:22"
links:
- mysql
mysql:
image: mariadb:10
container_name: mysql
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
volumes:
- db-data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
restart: always
image: phpmyadmin/phpmyadmin:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- "8080:80"
links:
- mysql:db
depends_on:
- mysql
volumes:
db-data:
external: false
#4. Navigate to line 8 and replace the domain name with the created domain before.
#5. On line 17, please replace /path/to/magento with the absolute path to the Magento files that you have downloaded before.
#6. Finally, in step 1, remember to save the new file as /path/to/docker/docker-compose.yml
.
Step 2: Ready to start with your virtual machine
#1. In this step, you still need to open your terminal to /path/to/docker
.
#2. It’s time to fire up your virtual machine. As soon as you spin up, it takes some minutes so that Dockers can download the images. After that, spin-ups in the future will take a few seconds.
docker-compose up -d --build
#3. Please make sure that everything is set up and run as per your plan. Then, you need to go to 127.0.0.1.8080
in your web browser, and you will see phpMyAdmin
. Congrats, it’s successful if you can see it.
Step 3: Install Magento 2 in detail
It’s time to do the most important step in Magento 2 Docker setup. Let’s get going!
#1. Access your command line that contains your Docker web: docker exec -it web bash
#2. Please go ahead to the web document root: cd /app
#3. Although this is an optional step, highly recommended: deploy sample data: php bin/magento sampledata:deploy
#4. Install Magento 2. Remember to copy and paste the following command into the Docker terminal. Note that you must replace the values on lines 2-6 with your details. On lines 7-8, please replace the placeholder domain with your created domain name before.
php bin/magento setup:install \
--admin-firstname=John \
--admin-lastname=Doe \
[email protected] \
--admin-user=admin \
--admin-password='SomePassword123' \
--base-url=https://local.domain.com \
--base-url-secure=https://local.domain.com \
--backend-frontname=admin \
--db-host=mysql \
--db-name=magento \
--db-user=root \
--db-password=root \
--use-rewrites=1 \
--language=en_US \
--currency=USD \
--timezone=America/New_York \
--use-secure-admin=1 \
--admin-use-security-key=1 \
--session-save=files \
--use-sample-data
#5. Navigate to your website at https://local.domain.com
or whatever domain you select. You need to spend some minutes loading the page when you access the website for the first time. The reason for this is that nothing is cached and the Magento system will automatically create files when loading the pages. Don’t worry, as the loading process will be quicker next time.
Additionally, as the web container applies a self-signed SSL certificate, you can get a security alert from the browser when visiting the URL for the first time.
#6. Congratulations! You succeeded in setting up Magento 2 on Docker. Please remember that you can repeat this process for other projects through our step-by-step guide.
While the benefits of running Magento 2 on Docker are numerous, mastering some key details and commands can take your project to the next level. Here’s a concise guide to essential Docker information and commands for a smooth experience:
1. Docker Information
- Docker Data Persistence: Even after you restart the virtual machine, your database will remain unchanged. This is because the
docker-compose.yml
file contains a data volume. - Database Access: Need to connect to other databases or tools like Sequel Pro? Simply access your Magento database at
127.0.0.1:3306
using the credentialsusername: root, password: root, database name: magento.
2. Docker Commands
- Spin Up:
docker-compose up -d --build
- Tear Down:
docker-compose down
- Web Container CLI Access:
docker exec -it web bash
- Database Container CLI Access:
docker exec -it mysql bash
Tips to Boost Docker Performance with Magento 2
Enhancing Docker’s performance for Magento 2 ensures smooth and efficient development. Here are practical tips to optimize your setup:
- Use a Lightweight Base Image: Select a minimal Docker image tailored for Magento 2 to reduce container size and improve startup times.
- Optimize Caching: Implement Redis for full-page cache, session storage, and metadata caching to minimize database queries and enhance speed.
- Allocate Sufficient Memory: Assign enough memory to Docker containers based on Magento 2’s requirements and any installed extensions or modules.
- Streamline Volume Mounting: Mount frequently accessed directories like
pub/media, var/log
, andvar/session
as separate volumes outside the container for faster I/O operations. - Enable OPCache: Configure PHP’s OPCache to store precompiled scripts in memory, speeding up execution.
- Tune MySQL Settings: Adjust parameters like
innodb_buffer_pool_size
andkey_buffer_size
to optimize database performance based on your needs. - Monitor Resource Usage: Use tools such as Grafana or Prometheus to track CPU, memory, and disk I/O metrics regularly and address potential bottlenecks.
Optimizing your Docker setup for Magento 2 not only enhances performance but also ensures a more efficient development experience throughout the project lifecycle.
Common Performance Issues with Magento 2 and Docker
Using Magento 2 with Docker can sometimes lead to performance challenges that affect your store’s speed and efficiency. These issues may include slow page loading, high resource usage, and database connection errors. Addressing these problems is essential for delivering a smooth shopping experience to customers.
One frequent issue arises from filesystem mounting in Docker. Communication between Magento’s file system and Docker containers can be sluggish, causing delays in file access and negatively impacting site performance. To improve this, you can optimize NFS settings, or switch to more efficient file systems like OverlayFS or AUFS. These adjustments can significantly enhance file access speed in your Magento setup.
Another common issue is insufficient resource allocation. Without enough memory or CPU resources assigned to the Docker containers, your store may experience slow response times and decreased performance. Allocating adequate resources based on your website’s specific needs is vital.
Solutions to Improve Filesystem Mounting Performance
- Set up Selective Sync to improve performance when dealing with large file systems in a Magento 2 environment.
- Use a volume mount approach to optimize the file system for better performance.
- Mount NFS volumes to enhance file system efficiency, particularly for Magento 2. By addressing these common issues and implementing targeted solutions, you can optimize the performance of Magento 2 on Docker, ensuring a faster and more reliable online store for your customers.
Final thoughts
That’s it! Now you have Magento 2 and run it on the Docker environment. It’s straightforward to follow if you spend more time reading this guide carefully. We hope that this article will help you install Magento 2 on Docker and of course, it will bring your Magento 2 store more great benefits afterward.
If you see our Guidance in setting up Magento 2 on Docker is useful, don’t forget to share it with your friends as we will give you more helpful information in the next blog.
Thanks a lot for reading!