Start a Project

Magento 2 With Docker-Compose

In our last blog, we had discussed briefly about Docker architecture and its three components. We had also setup an architecture for Magento 2 having apache server and mysql server running on same container. Also, by mounting Magento 2 files from host to container, we had setup single docker container architecture for Magento 2.

Although, for multiple-server production environment, single Docker container architecture is not a good choice as it lacks scalability and flexibility. Single Docker container architecture can be opt in the case when your backend server setup is not going to get frequently modified.

For long run, multiple docker container architecture is a good way to go. Also, as we have discussed in our last blog that there should be single process running per container, so for a LAMP server setup, apache server and mysql server should run on separate containers interlinked with each other.

In order to achieve multiple containers architecture, we can use docker-compose tool.

 

Introduction to Docker-Compose

 

Quoting from Docker docs itself,

 

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

 

With the help of docker-compose we can define containers to be built, their configuration, links, volumes, ports etc in a single file and it gets launched by a single command. We can add multiple servers and services just by adding them to docker-compose configuration file. This configuration file is in YAML format.

Working with docker-compose is generally few steps process:

 

 

Setting Up Magento 2 Using Docker-Compose

 

In this blog, we will be using same architecture as mentioned above to setup Magento 2 using docker-compose on Ubuntu 16.04 . Presuming that fact that docker and docker-compose is already installed on your ubuntu server we will begin with server setup. If not, please install docker and docker-compose first.

Our parent directory holding all the files/directories will be considered as project name. We can also set custom project name in docker-compose.yml file. Here we are creating separate directories for apache2 and mysql server setup that holds Dockerfile and their associated volumes.

Take a note that: Like our previous blog, Magento 2 files will be placed on our host. As it is a good practice to keep application files on host so that it will not be lost if containers or images get accidentally removed. These magento files will be mapped from host to running docker container.

We are also using supervisor to control apache2 server and mysql server in their respective containers. Apart from controlling these servers, supervisor is also running various commands and scripts that will be mentioned later in this blog.

To begin with create a directory on your Ubuntu 16.04 server for this project. Our directory architecture will be something like:

 

Dockerfile

supervisord.conf

Dockerfile

mysql.sh

supervisord.conf

Unarchived Magento 2 files and directories.

 

The docker-compose.yml file is shown below:

 

 

As we can see in docker-compose.yml file above, among various version available, we are using version 3. Here in our configuration file, we are defining two services: web_server and database_server. The web_server is associated with our apache2 server configuration. Container name defined for this service is apache2, linked to database_server and port 80 is allocated to it. There are four volumes or files are being mapped from host to docker container and “context” under “build” points to location of its Dockerfile.

Also, the service database_server is associated with mysql-server. Container name is defined as mysql and port 3306 is allocated to it. Mysql root password and Database name will be passed as build argument and there are two volumes/files mapped from host to docker container. Same as web_server, “context” points to location of its Dockerfile.

Lets take a look in our web_server directory. It contains 2 files. Dockerfile is shown below:

 

And at last we have supervisord.conf file that supervisor use to run apache2 server and ownership commands. Its contents are shown below:

 

 

Moving on to our database_server directory, it contains 3 files. Dockerfile is shown below:

 

 

So as we have seen in our last blog as well that Dockerfile just install packages as it is instructed to do by the commands.

As mentioned earlier consider image as rest package where no services or processes are running. That is why we cannot perform any operation from Dockerfile that requires a particular service to be running. As with case of database, we cannot create database from Dockerfile as mysql service is not running. For database and its user creation, we will create a bash script that will run whenever a container will launch, hence creating mentioned database and its user.

We are using “mysql.sh” as the bash script as mentioned in Dockerfile. Bash script “msyql.sh” resides on our host parallel to Dockerfile.

Contents of mysql.sh is shown below:

 

 

Apart from mysql.sh and Dockerfile, we are also mapping supervisord.conf file. Its contents are shown below:

 

 

And our last directory is magento2 directory. In our case we have download Magento 2 latest version from https://magento.com/tech-resources/download and unarchive it in magento2 directory. This directory will be mapped with /var/www/html directory in docker container.

Now, when directory setup is complete, check the ports to be allocated are available. Go to project directory and run the following command to build the image.

It will create two images. Check the images by running command:

Now to run the containers as a part of single project being as mentioned in docker-compose.yml file, run the command:

Your containers will get running. To list running containers under docker-compose, run command:

Your database credentials are mentioned in mysql.sh file. To get database user password, you can either enter the running container and check the /var/log/check.log file as:

Or, you can get it directly from host as:

Now, your server setup is all ready, hit your domain name or IP to install Magento 2.

Now begin with setting up Magento, perform readiness check,

Now, mention  database details as we have mentioned in mysql.sh file.

Now, mention your admin panel details.

After this, we are set to install our Magento 2, click on install button to proceed.

 

So far we have discussed how to setup Magento 2 using docker-compose on multiple docker container architecture. Also refer to git repository https://github.com/webkul/magento2-docker-compose. In our next blog we will setup Magento 2 with Varnish cache on Docker architecture.

If you have any issues, contact us at support@webkul.com or raise ticket at https://webkul.uvdesk.com/

Exit mobile version