Docker + Docker Compose – use containers for Web Development
Docker based in 2013 for inner needs dotCloud company. For 5 years Docker stays popular startup to create development different engineer environments. Currently when you start work on big project often your choice will be Docker.
Needed learning Docker?
Usually for recomendet Docker has next advantage list:
- Quick start and deployment clothe production environment;
- Scale containers and use Orchestration system;
- Module system, each containers can work with separate component your project;
- and more others.
Docker - Installing
Instruction on official site for each system:
Docker not supported installing to Windows 7 as native app, but you can use Docker Toolbox for install Docker across Virtual Machine (will be use VirtualBox).
Also, if you want manage a few containers good choise will be both with Docker installing Docker Compose (manage and configure docker containers system).
Installing Docker и Docker Compose на Debian 9
1 - Install components
1 2 3 4 5 6 |
$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg2 \ software-properties-common |
2 - Add repository and key from needed download Docker. So, key:
1 |
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - |
3 - And add repository:
1 2 3 4 |
$ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \ $(lsb_release -cs) \ stable" |
4 - Update user repositories:
1 |
$ sudo apt-get update |
5 - Install the Docker
1 |
$ sudo apt-get install docker-ce |
Check installed Docker:
1 |
$ sudo docker --version |
In your console, we have to see information about version Docker which was installed.
Install Docker Compose
Official documentation - Install Docker Compose and github repo - Compose.
Download files Docker Compose (need openn GitHub Docker-Compose and look current version. Next you should use version number in link after "/download/") -
1 |
sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose |
Ad permission:
1 |
sudo chmod +x /usr/local/bin/docker-compose |
Check work installed app:
1 |
docker-compose --version |
Commands for Docker Compose for CLI yoi found here.
Docker compose configuration
We will be use next coniguration as sample https://github.com/Atlogex/axdocker-lemp
Docker - configure project
Sample for configure LEMP web server (Linux - Engine-X/nginx - MySQL - PHP). Folder structure:
- /docker/ - settings containers:
- /nginx/nginx.conf - setting nginx;
- /php-fpm/Dockerfile - file build for PHP containers;
- /db_data/ - folder for data DB containers;
- /www/ - codebase your project;
- /docker-compose.yml - main configuration file Docker Compose.
1. Create configuration file
Docker compose configuration contains in docker-compose.yml file, our sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
version: "3.1" services: mysql: image: mysql:8.0 container_name: atlogex-mysql working_dir: /application volumes: - ./db_data:/application environment: - MYSQL_ROOT_PASSWORD=112233 - MYSQL_DATABASE=db - MYSQL_USER=atlogex - MYSQL_PASSWORD=123456 ports: - "8082:3306" webserver: image: nginx:alpine container_name: atlogex-webserver working_dir: /apl volumes: - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/nginx.conf/ ports: - "80:80" php-fpm: build: docker/php-fpm container_name: atlogex-php-fpm working_dir: /apl volumes: - ./www:/var/apl - ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.1/fpm/conf.d/99-overrides.ini |
Base properties:
version - exist little differences for different Compose version, for more in official Compose documentation.
services - your containers and base parameters for it.
Sample MySQL DB container:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql: image: mysql:8.0 container_name: atlogex-mysql working_dir: /application volumes: - ./db_data:/application environment: - MYSQL_ROOT_PASSWORD=112233 - MYSQL_DATABASE=db - MYSQL_USER=atlogex - MYSQL_PASSWORD=123456 ports: - "8082:3306" |
image - prepare image which will be downloading from Docker Hub, exist installed OS + MySQL;
container_name - your containter name;
working_dir - directory for your data;
volumes - common directory between your system and system inner container;
enviroments - container settings;
posrts - port your system : port inner container.
And more others in official documentation.
Webserver settings
For nginx we should add "./docker/nginx/nginx.conf":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
server { listen 80 default; server_name yoursite.lcl www.yoursite.lcl; client_max_body_size 108M; access_log /var/log/nginx/application.access.log; root /var/apl/; index index.php; location / { index index.php index.html index.htm; } location ~ \.php$ { fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log"; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; } } |
2. Build containers
Containers for which we use Dockerfile need building. In our sample we have "php-fpm" containers, it will be build according to it Dockerfile "./docker/php-fpm/Dockerfile". Dockerfile contains OS and commands list which will be apply in time building container (After change this file need re-build container again).
Documentation for Docker PHP or autogenerated Configuration file. For sample will be used simple configuration:
1 2 3 4 5 6 7 8 |
FROM phpdockerio/php71-fpm:latest WORKDIR "/application" # Install selected extensions and other stuff RUN apt-get update \ && apt-get -y --no-install-recommends install php7.1-mysql \ && apt-get -y install git \ && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* |
Run building containers:
docker-compose build
3. Run Docker containers
After build containers we can run our environment:
docker-compose up
If for stop containers - "docker-compose down", restart - "docker-compose restart".
Building and statr in one line:
docker-compose up -d --force-recreate --build
Argument "-d" not blocking console.
You can restart separate container use name it container, command:
docker-compose restart webserver
4. Checking system works
List running containers and it ports:
docker-compose ps
Output:
1 2 3 4 5 |
Name Command State Ports ---------------------------------------------------------------------------------- atlogex-mysql docker-entrypoint.sh mysqld Up 0.0.0.0:8082->3306/tcp atlogex-php-fpm /bin/sh -c /usr/bin/php-fpm Up 9000/tcp atlogex-webserver nginx -g daemon off; Up 0.0.0.0:80->80/tcp |
Nginx listen port 80, and our project will open in this port (but port 80 is default and we can just use link whithout it). MySQL container available to us on port 8082 (You can use application HeidiSQL or Workbench for connected to your DB).
Your project should be open by next address:
- 0.0.0.0
- 127.0.0.1
For use usualy site name (friendly URL) you can update your etc/hosts file (Linux - "/etc/hosts", Windows - "C:\Windows\System32\drivers\etc\hosts"):
127.0.0.1 yoursite.lcl
Run containers | Nginx | Site |
Docker - possible problems
- Error 1396 in time create user:
Docker mysql ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
You have not right MySQL configuration. Please not use const "MYSQL_USER" equal "root", because this is already as default username.
- Error DB initialized:
error: database is uninitialized and password option is not specified
mysql exited with code 1
Try set next const:
- MYSQL_ROOT_PASSWORD - set root password;
- MYSQL_ALLOW_EMPTY_PASSWORD - allow initialized MySQL without password;
Links:
- Sample source on github - https://github.com/Atlogex/axdocker-lemp
- Interactive education - http://training.play-with-docker.com
- Auto generation config file for PHP projects - https://phpdocker.io/generator