
In this article, we will describe one of the ways to run Magento 2 on Docker.
Docker is a software container platform. In simple words, it’s an isolated environment where we can run different pre-configured images from simple OS to a specific version of your favorite database engine.
In our case, we are going to run Magento over Nginx, PHP 7, MySQL 5.7 and additional utilities sFTP and phpMyAdmin.
First of all, we will need the Docker itself. In can be downloaded from https://www.docker.com/get-docker.
And then we will need very useful utility Docker Compose. It helps to run multiple Docker containers as one system. Installation instructions can be found here: https://docs.docker.com/compose/install/.
When all required software is installed we can start building a project template. This template can be reused in future to launch multiple projects event on one host.
If you do not want to read the whole article you can download the full template from GitHub.
Let’s start.
Using the template
- Download and unzip the template: https://github.com/Eleanorsoft/web-server-docker-template/archive/master.zip.
We will assume that this unzipped folder is your current working directory. - Open docker-compose.yml in a text editor and make the following changes:
- replace
__prefix__
with unique project name (lowercase, no spaces, special chars, etc.) - set Nginx port to a unique for your host number. You should use the same port before and after the colon. For example, let’s set port
8888
:magento_nginx: build: ./nginx/ ports: - 8888:8888 links: - magento_php volumes_from: - magento_app restart: always
- setup database information. Change
secret_root_password
,__dbname__
,secret_db_password
to the appropriate values. You will use__dbname__
andsecret_db_password
to access the database from Magento 2. - set phpMyAdmin port to a unique for your host number (differs from the Nginx port). Here you should change only the first number (before the colon).
- set phpMyAdmin port to a unique for your host number (differs from nginx and phpMyAdmin ports). Here you should change only the first number (before the colon).
- change
__secret_sftp_password__
to a strong password phrase.
- replace
- Rename
nginx/default_magento.conf
tonginx/default.conf
and open it in a text editor:- replace
__prefix__
with the same value you used indocker-compose.yml
- In line
listen 5011 default_server;
put the port number you used for Nginx in
docker-compose.yml
- replace
- Put Magento 2 files into
www/html
folder - Run
chmod -R www-data:www-data www/html
(use sudo if needed). All files in this folder should be owned by the webserver to allow changing them through our SFTP Docker container.
- In Terminal in current directory run
sudo docker-compose up -d
It may take a while because Docker needs to download all requested images. But it’s only for the first run. All other projects should start up much faster.
How to use this setup
Webserver
You can open Magento 2 in your browser using your host’s address and port for Nginx from docker-compose.yml
.
For example, if you run a project on 127.0.0.1
and used 8888
as Nginx port then you can access Magento 2 using URL http://127.0.0.1:8888/.
Note: Magento 2 may not install on localhost because it considers localhost
as an invalid domain name
phpMyAdmin
Just open the same address with the port for phpMyAdmin. You can use __dbname__
and secret_db_password
to enter.
sFTP
Use your sFTP client (e.g. FileZilla) to connect to the same address and port for the FTP entry in docker-compose.yml
. Login is your project name and password is a string you’ve used to replace __secret_sftp_password__
.
Command line
Magento 2 requires running some console commands from time to time. And here things become a little bit tricky. The commands should be run in the php container. Let’s connect to it. In terminal run (remove sudo if you’re not in Ubuntu):
sudo docker ps
Find your PHP instance and copy its CONTAINER ID
.
Now connect to the instance:
sudo docker exec -it {CONTAINER ID goes here} /bin/bash
And switch to www-data user, which has permission to change files in www/html/
(no sudo required):
su -s /bin/bash www-data
Now you can run Magento 2 commands. For example:
php bin/magento cache:clean
To exit the console use Ctrl+D
combination.
You can run as many such projects as you wish until you have a free port for each.
Related Post
Search for ALL words in...
By default Magento 2 QuickSearch feature searches for at least one word from the entered...
- August 2, 2017
- By eleanors_admin
- Magento
Install Magento 2 on Windows...
This article describes one of the ways to setup Magento 2 on Windows or Linux. This...
- August 26, 2017
- By eleanors_admin
- Magento
Speed Up Magento 2 DI...
Here we will describe how to improve standard Magento 2 compilation in order to make it...
- November 29, 2017
- By eleanors_admin
- Magento
PayPal error: We can’t...
There is a bug with PayPal checkout on early versions of Magento 2. Users may see error...
- March 12, 2019
- By eleanors_admin
- Magento
Magento2 Mail Logger
This module helps to debug Magento2 emails saving them into a folder as seperate files....
- January 14, 2020
- By eleanors_admin
- Magento
EleanorSoft Became a...
On March 12th, we applied to Adobe Solution Partner Program (SPP) and were approved as a...
- March 15, 2020
- By eleanors_admin
- General