Using Docker for your development environment

Why Docker? Because it starting faster than vagrant, because you don't need to install all required libs, utils etc. on you OS, because you can make the same environment that on your production server and all developers that works on the project will use the same environment(same OS, same versions etc.).

I'll show in this post how to create LAMP containers group. 


First that you need it's install Docker, just follow the instructions on official Docker site
When you have Docker installed let's create directory structure for our containers:
$ mkdir ~/docker-compose
docker-compose directory will contain our docker-compose settings.
Now let's create docker-compose settings file for our lamp containers group:
$ cd ~/docker-compose
$ mkdir lamp
$ touch lamp/docker-compose.yml
$ vim lamp/docker-compose.yml
And paste this code to the docker-compose.yml:
version: '3.2'

services:
   mysql:
       image: mysql
       environment:
           MYSQL_ROOT_PASSWORD: root
       volumes:
           - ./mysql-data:/var/lib/mysql

   your_project:
      build: ./php
      ports:
          - "80:80"
      volumes:
          - type: "bind"
            source: /path/to/your/project
            target: /var/www/your_project
      links:
          - mysql
Here we can see two images mysql and your_project. 
mysql it is our mysql server container, here we set environment variable MYSQL_ROOT_PASSWORD = root (you can check mysql on the Docker Hub and view all available environment variables) and volumes to store our mysql data on the local drive and make sure that our database data will not be removed after container restarting.

your_project - it is php-apache image. Here we set apache port as 80:80 - <your_pc_port>:<container_port>. And bind our project directory.


Now let's create directory for mysql data:

$ mkdir lamp/mysql-data

And define configs for our php-apache image:
$ mkdir lamp/php
$ touch lamp/php/Dockerfile

Dockerfile contain php-apache image settings:
FROM php:7.0-apache

#Uncomment if you need override some php settings
#COPY php.ini /usr/local/etc/php/

#Uncomment if you need override apache config
#COPY custom.conf /etc/apache2/conf-enabled/

#override host settings
COPY 000-default.conf /etc/apache2/sites-enabled/

#install additional libs and php extentions
RUN apt-get update \
  && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev libmcrypt-dev nano mc \
  && docker-php-ext-install mysqli pdo_mysql mbstring gd iconv mcrypt opcache

#add your project domain name to the hosts file
RUN echo 127.0.0.1 your_project.local > /etc/hosts

#Uncomment if you need enable apache mod_rewrite
#RUN a2enmod rewrite
Under directory lamp/php you can create additional files php.ini and custom.conf and uncomment COPY command in Dockerfile, this will allow you to specify your own php, apache settings.
Now let's create a 000-default.conf file that will contain our VirtualHost settings:
$ touch lamp/php/000-default.conf
And put this content to the 000-default.conf:
<virtualhost>
    ServerName your_project.local
    ServerAlias www.your_project.local
    DirectoryIndex index.php

    DocumentRoot "/var/www/your_project"

    <directory var/www/your_project>
        Order allow,deny
        Allow from all
    </directory>
</virtualhost>

Now edit your local hosts file and add your_project.local domain:
$ vim /etc/hosts

Add the following line to the hosts:
127.0.0.1 your_project.local

It's time to build and run!
$ cd ~/docker-compose/lamp
$ docker-compose build
$ docker-compose up -d

So now, if everything fine, you can open in your browser http://your_project.local :)

P.S. if you need to connect to your container just run:
$ docker exec -i -t "container_name_1" /bin/bash

If something wrong you can check docker-compose logs:
$ docker-compose logs

You can add -f option and view logs in real time:
$ docker-compose logs -f

Comments