Site Analysis
Твиты
Афоризмы
Цитаты
Новости
Analysis

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

2 - Add repository and key from needed download Docker. So, key:

3 - And add repository:

4 - Update user repositories:

5 - Install the Docker

Check installed Docker:

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/") -

Ad permission:

Check work installed app:

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:

 

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:

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":

 

 

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:

 

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:

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
Tags:
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (3 votes, average: 6.00 out of 10)
Loading...

Leave a Reply