Magento 2 Docker Installation

Updated 18 July 2018

Be it developers, or sys-admins, or devops engineers, all of them must have came across Docker once in a while. For those who haven’t heard of this term, Docker is a kind of virtualisation like vagrant, virtual machines etc. Quoting from Docker documentation itself:

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Understanding Docker Architecture:

 

Docker engine is a client-server application which has basically three components,

 

  • Docker daemon server
  • REST API that provides an interface so that programs can talk to docker daemon and give it instructions.
  • Docker command line interface

 

The CLI uses REST API to control or interact with  Docker daemon through CLI commands. Docker engine architecture from Docker docs itself is shown below:

 

 

Docker packages and runs an application in a loosely isolated environment called a container. Unlike VMs, containers do not bundle a full operating system. They wrap up only libraries and settings required to make software work. Containers get launched from docker image(s). Docker container image is a light-weight, standalone, executable package of a piece of software that is code, system tools, system libraries, settings etc. They are constructed from filesystem layers and share common files.

Containers running on single machine share that machine’s operating system kernel. That is why they start instantly and use less compute and RAM. Containers also share OS kernel with other containers as well. They are abstraction at the application layer that package code and dependencies together. A layered architecture of docker (taken from www.slideshare.net) is shown below:

 

 

Images can either be created by launching containers from some base image followed by some installations/alterations and then committing the container to a new image, or it can be created from Dockerfile. For creating an image, writing Dockerfile happens to be a good practice  as it is light-weight  and image built from Dockerfile consume less storage space.

Dockerfile allows to create image either from some base image (ex: ubuntu os image) or directly from scratch where we define all the packages that would be needed in our image. For best practices for writing Dockerfile, refer link https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/ .

 

Magento 2 With Docker:

 

Docker is an open-source project that can be integrated with almost all the applications allowing scope of isolation and flexibility. It can be integrated with Magento-2.x as well. Magento is an e-commerce platform written in PHP and based on zend framework available under both open-source and commercial licenses. In this blog, we will run magento2.x within the architecture of docker on Ubuntu 16.04.

Generally, “each container should have only one concern” i.e. there should be single process running per containers. Then all the running containers should be linked with each other. But in the architecture we will be defining in this blog, we will use supervisor as our main process that will run apache server and mysql server together within a single container. Hence we won’t need container linking.

Also, supervisor is a client-server system that monitors and controls a number of processes. In our later blogs, we will explore architectures built on multiple containers and their linking mechanism.

Presuming the fact that Docker is already installed on your ubuntu server, we will dive into server setup. If Docker is not installed on your server, please install the docker beforehand. Moving forward to our architecture setup, we will create a Dockerfile that will hold the installation of LAMP server and other necessary packages.

To begin with, we will be using Ubuntu 16.04 base image, apache2 server, mysql-server-5.7 and PHP version will be passed as an argument as per your choice (php7.0, php7.1) along its extensions as per Magento 2.x requirements.

Whole server architecture will be wrapped up in one Docker image and Magento-2.x files will be placed on our host. It is a good practice to keep application 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 docker containers. We will also install supervisor for controlling apache and mysql server.

Dockerfile is shown below:

 


 

Take a note that Dockerfile just install packages as it is instructed to do by the commands and build the image.

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 file, we are copying supervisord.conf file from host to docker image that holds that instructions to run apache2 server, mysql server, mysql.sh script and certain ownership command. This file should be placed to Dockerfile and mysql.sh script.

Contents of supervisord.conf file is shown below:

 


 

Our parent directory will have following files/directories:

 

  • Dockerfile
  • supervisord.conf
  • mysql.sh

 

Now when Dockerfile, mysql.sh, supervisord.conf, 000-default.conf and magento2 directory are placed parallel to each other, we are ready to build our image. Go to your docker project directory and run:


After image build up, you can check your built images by command:


As mentioned earlier, we will keeping Magento server files on host. So create a directory naming magento2 parallel to Dockerfile. Download the latest Magento-2.x from https://magento.com/tech-resources/download and place the unarchived files within magento2 directory and change its ownership as www-data.

Now for running containers from this image, first ensure you have port 80 and 3306 is available then run the command:


In above command, “-v” is used to mount or map volumes. In our case it is mapping magento2 directory with /var/www/html/.  After running above command, check the running containers by command:


Now hit the URL or IP on your browser and you will see the magento-2.x installation page.

So far, we have setup Magento-2.x within the Docker architecture with running single container for web server & database server and mapping Magento-2.x files from host to docker container. In our next blog, we will build docker architecture for Magento-2.x by container linking approach.

 

IN CASE OF ANY QUERY,CONTACT US

Category(s) docker linux Uncategorized
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home