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

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:

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

  1. Download and unzip the template:
    We will assume that this unzipped folder is your current working directory.
  2. 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:
          build: ./nginx/
              - 8888:8888
              - magento_php
              - magento_app
          restart: always
    • setup database information. Change secret_root_password, __dbname__, secret_db_password to the appropriate values. You will use __dbname__ and secret_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.
  3. Rename nginx/default_magento.conf to nginx/default.conf and open it in a text editor:
    • replace __prefix__ with the same value you used in docker-compose.yml
    • In line
      listen 5011 default_server;

      put the port number you used for Nginx in docker-compose.yml

  4. Put Magento 2 files into www/html folder
  5. 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.

  6. 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


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 and used 8888 as Nginx port then you can access Magento 2 using URL
Note: Magento 2 may not install on localhost because it considers localhost as an invalid domain name


Just open the same address with the port for phpMyAdmin. You can use __dbname__ and secret_db_password to enter.


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...

Install Magento 2 on Windows...

This article describes one of the ways to setup Magento 2 on Windows or Linux. This...