Created on 2020-05-25 08:03
Published on 2020-05-25 08:10
The Application Docker Compose
Compose is a tool for using and running multi-container Docker applications. With Compose, you can define a YAML file to configure application services.
In fact, in the YAML file, it is possible to define multiple Docker containers under the " services " hat, both starting from Dockerfile and from ready-made images.
It is not applicable in all cases. If the project is a simple application that does not require the use of third-party services, then for its deployment, you can limit yourself only to Docker. Docker Compose is recommended for use in the design of complex software products that include many processes and services.
It will be possible to create and start all the services defined in the file with a single command. It is, in practice, a process divided into three phases:
The main features of Docker Compose that make it useful are:
Let's imagine we want to pull up WordPress, which needs MySQL. Inside a folder called wordpress-docker, we create a file called docker-compose.yml with this content (present on GitHub ):
version: '3.7'
services:
db:
build: ./mysql
image: poc / mysql-for-wordpress
volumes:
- db_data: / var / lib / mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress: latest
ports:
- "8000: 80"
restart: always
environment:
WORDPRESS_DB_HOST: db: 3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: { }
In this case, we made customization (not that it was needed, it is only for educational purposes), through Dockerfile, of the MySQL container, while we took the default one for WordPress. Note the value of WORDPRESS_DB_HOST " db: 3306 ": the reference to the database container is resolved with the name of the associated service.
Docker Compose, however, is not natively available when installing Docker, but is a separate tool, written in Python, which must be installed independently and automates certain operations of the Docker daemon. It has its own CLI, accessible through the docker-compose command, which greatly simplifies the "group management" of the services listed in the YAML file.
To start all the services, just run:
$> docker-compose up -d
Where with -d you avoid getting stuck to the system out of the containers started. What happens now? Control of the console is returned to us only after these operations:
Creating network "wordpress-docker_default" with the default driver Creating volume "wordpress-docker_db_data" with the default driver Building db Step 1/2: FROM mysql: 5.7 ---> 98455b9624a9 Step 2/2: COPY * .cnf / etc / mysql /conf.d/ ---> 52440b13db7c Successfully built 52440b13db7c Successfully tagged poc / mysql-for-wordpress: latest WARNING: Image for service db was built because it did not already exist. To rebuild this image, you must use `docker-compose build` or` docker-compose up --build.` Creating wordpress-docker_db_1 ... done Creating wordpress-docker_wordpress_1 ... done
The first thing that catches the eye is that the name of the folder containing the YAML file is used as a " namespace " for the nomenclature of the resources created: this approach is interesting because it allows you to relaunch the same stack of services from different folders without creating conflicts.
All these operations are synchronous with the CLI: until the containers are started, we will not be able to regain control of the console. The compose CLI is interesting because it allows you to check the containers in groups:
$> docker-compose ps Name Command State Ports ---------------------------------------- -------------------------------------------------- - wordpress-docker_db_1 docker-entrypoint.sh mysqld Up 3306 / tcp, 33060 / tcp wordpress-docker_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8000->80/tcp
Displays the status of the containers and their mapped ports (if any). It is also possible to control only one, called with the name of the service:
$> docker-compose stop wordpress Stopping wordpress-docker_wordpress_1... done $> docker-compose start wordpress Starting Wordpress... done
If you re-run the start command of all the services ( up ), Compose will check for us if there are new versions of the images and possibly start them (with dependencies in cascade). Changing the image of the database; then, WordPress will also be restarted:
$> docker-compose up -d Recreating wordpress-docker_db_1 ... done Recreating wordpress-docker_wordpress_1 ... done
So far, we have seen that Compose is perfect for managing the build and runtime cycle of multiple services together, why should we need anything else?
It's perfect for software development support: we can practically create and build the infrastructure we need on our development machine! In my opinion, Compose begins to stay tight in two cases:
$> docker-compose up -d --scale wordpress = 2 WARNING: The "wordpress" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash. Starting wordpress-docker_wordpress_1 ... done Creating wordpress-docker_wordpress_2 ... error ERROR: for wordpress-docker_wordpress_2 Cannot start service wordpress: driver failed programming external connectivity on endpoint wordpress-docker_wordpress_2 (f19636f06cfcf461c0d175a182fa3c8f0f0f0f0f0f0f0f0f0f0f0f0f0fcf0fcf0fcf0fcf0fcf0fcf0fcf0fcf0fcf0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0bh0 besides0 but about. port is already allocated
Wanting to scale the database, however, this would work because it has no binding:
$> docker-compose up -d --scale db = 2 Starting wordpress-docker_db_1 ... done Creating wordpress-docker_db_2 ... done wordpress-docker_wordpress_1 is up-to-date
When you need to control multiple services on multiple machines, and maybe scale them, more is needed, at least docker swarm is needed. Let's destroy all containers, including volumes:
$> docker-compose down -v Stopping wordpress-docker_wordpress_1 ... done Stopping wordpress-docker_db_1 ... done Removing wordpress-docker_wordpress_1 ... done Removing wordpress-docker_db_1 ... done Removing network wordpress-docker_default Removing volume wordpress-docker_db_data
Docker is an important tool for every modern developer, as the basis of hardware application virtualization. This technology has wide functionality and capabilities for process control. Docker allows not only to deploy containers but also to quickly scale their instances, work with multi-container applications (Docker Compose).
Docker is characterized by a fairly simple syntax. Therefore, it is quite easy to learn for both experienced IT specialists and beginners. The software is compatible with all versions of Linux and Windows operating systems, so the scope of Docker is practically unlimited.