Development Environment for Magento 2 using Docker

If you know about PHP then you must be familiar with the issues that frequently bug you while working with Magento 2. Whether you developing with Apache Tomcat running PHP or you went on the whole path from xampp to vagrant, deep down your soul of Magento Expert you know, the development process in Magento 2 was not easy with PHP.

And then came the Docker and the development in Magento with PHP become bliss. So, what a docker is? There is a possibility that, mere words can’t explain what a docker is even by a seasoned Magento Ecommerce Web Development . Still, let me try,

“The docker performs operating system level containerization (virtualization) which is similar to the operating system feature where the kernel allows multiple isolated userspace instances”

You might be wondering how  difficult it is just to understand with mere words. If words can’t comprehend for you, you can go for this link to understand more.

Now let’s move forward and see what are the things you can do with docker in Magento 2 development environment. So, without any further ado, let’s get you started.

The Capable Setup for Running Magento 2 via Docker

For a perfect illustration, let’s consider that you run magento 2 on http://m2.docker

Add the following line somewhere in your /etc/hosts file:

127.0.0.1 m2.docker www.m2.docker

Now, for further steps, you need a file structure setup. Do as follows,

tomas ~ $ mkdir m2.docker
tomas ~ $ cd m2.docker/
tomas ~/m2.docker $ mkdir docker
tomas ~/m2.docker $ mkdir html
tomas ~/m2.docker $ mkdir -p docker/db
tomas ~/m2.docker $ touch docker/xdebug.ini
tomas ~/m2.docker $ touch docker/apache.conf
tomas ~/m2.docker $ touch docker-compose.yml
tomas ~/m2.docker $ touch .env

Some Important File Extension for you to understand

.env

In this particular file, you can write variables which you can use in docker-compose.yml.

Some variables for you,

CONTAINER_PREFIX=m2docker
SERVER_NAME=m2.docker
SERVER_ALIAS=www.m2.docker
DIRECTORY_NAME=m2.docker
WEB_USER=inchoo
WEB_ROOT = /var/www/html
MYSQL_DB_HOST = ${CONTAINER_PREFIX}_db_1
MYSQL_DATABASE=inchoo
MYSQL_ROOT_USERNAME=root
MYSQL_ROOT_PASSWORD=inchoo
MYSQL_USER=inchoo
MYSQL_PASSWORD=inchoo
DOCKER_EXEC=docker exec
DOCKER_EXEC_INTERACTIVE=docker exec -i
DOCKER_EXEC_TTY=${DOCKER_EXEC_INTERACTIVE} -t

xdebug.ini

The components in this file will be like this,

zend_extension=xdebug.so
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=1

apache.conf

The example variables in this file will be like this,

ServerAdmin webmaster@m2-sample.docker
    ServerName m2.docker
    Serveralias www.m2.docker
 
    DocumentRoot /var/www/html/pub
 
        Options FollowSymLinks
        AllowOverride None
 
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
 
        SetHandler “proxy:unix:/run/php/php-fpm.sock|fcgi://localhost/”
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

 

Running The Development Environment for Magento 2 Be Like

So, with all the aforementioned files, the development environment will be ready. When you run it you will get results similar to these codes,

tomas ~/m2.docker $ docker-compose up -d
Creating network “m2docker_default” with the default driver
Creating m2docker_apache-php_1 … done
Creating m2docker_redis_1      … done
Creating m2docker_phpmyadmin_1 … done
Creating m2docker_db_1         … done
tomas ~/m2.docker $ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS IMAGE NAMES
e3384b0eff8c mysql:5.7 “docker-entrypoint.s…” 3 seconds ago Up 2 seconds 3306/tcp m2docker_db_1
d283bf018330 phpmyadmin/phpmyadmin “/run.sh phpmyadmin” 3 seconds ago Up 2 seconds 9000/tcp, 0.0.0.0:8080->80/tcp m2docker_phpmyadmin_1
ded8bce1d993 redis:latest “docker-entrypoint.s…” 3 seconds ago Up 2 seconds 6379/tcp m2docker_redis_1
fe8a80763ac6 udovicic/echo:apache-php7.1 “/start.sh” 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, 9000/tcp m2docker_apache-php_1

tomas ~/m2.docker $

Closing Words

So, what are your thoughts on this either as a Magento Expert or a business owner? Let me know via comments and I will get back to you as soon as possible via comments. Adios for now mate!!!

 

Leave a Reply

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