Docker Compose allows running and configuring multiple containers.
Docker Compose is a separate tool from the Docker developers that serves to run an isolated multi-container environment on one host system. Typical it concerns cases like we described in the previous part: In one container there is a web server and in the other there is a database server. Everything for working and communication of the mentioned containers in the isolated environment is possible to set in the text configuration file YAML docker-compose.yml. The advantage of this type of settings is the fact that to start all containers you execute one command docker-compose up. In the same way we can restart the containers with new settings, while containers with unchanged settings stay untouched.
Installing Docker Compose
The tool Docker Compose can be downloaded from the official repository of GitHub (current link in documentation) and save it in the directory /usr/local/bin/ (first line). Then we set the execution rights to the file (second line). In this example we install the version 1.27.4, if you need a different version, change the label to a different version.
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Set and run the containers
In the previous part we have run two containers to set assign them to a single network. With a tool Volume we have set the connection to the host system. We have set all in the configuration file Dockerfile and command line. Now we will show you how to accomplish that more systematically via tool Docker Compose.
Let’s move to a directory ~/docker-test (first line), where we will rename the html directory to html-compose-1 (second line) and make a copy html-compose-2 (third line). In both directories there is a file index.html that will be displayed by the Nginx web server as a main page:
cd ~/docker-test
mv html html-compose-1
cp -r html-compose-1 html-compose-2
We will create a configuration file of the Docker Compose tool called docker-compose.yml:
nano docker-compose.yml
and write the following setting inside:
version: "3.8"
services:
nginx-test-compose-1:
image: nginx
volumes:
- "./html-compose-1:/usr/share/nginx/html"
networks:
- siet-test
nginx-test-compose-2:
image: nginx
volumes:
- "./html-compose-2:/usr/share/nginx/html"
networks:
- siet-test
networks:
siet-test:
external: true
File is saved with the shortcut Ctrl + X and then by pressing key y and confirming with Enter.
The first line we will define the version of the formate of the configuration file (here 3.8, your can differ). Correct version should be selected depending on the version of the project Docker that we have installed. This table from the official documentation will help you.
Secondly we will define the services. In the terminology of Docker Compose the service is the same as a container in the terminology of project Docker. Here we will run the services (containersú called nginx-test-compose-1 and nginx-test-compose-2. In each service we will set 3 settings: From what image the service (containers) will build and run, what volume of the host computer has the service (containers) at disposal and to what networ we will assign the service (containers) to.
And at the last three lines we define that the network siet-test is external. Docker had previously created it and Docker Compose will now only assign the services (containers) to it.
All necessary setting are saved and will run on both containers via command:
docker-compose up -d
We can check, if the containers are running with a command:
docker-compose ps
If we make any changes in the configuration file docker-compose.yml and we need to load the changes to the containers, simply enter the command docker-compose up -d. Containers will restart with the new settings, unchanged will remain running.
Check the functionality of containers
Soon we will discover, what IP addresses (Containers > IPv4Address) are assigned to the containers in the network siet-test:
docker network inspect siet-test
Then we will log in to the command line (bash) of the first container nginx-test-compose-1 (first line), where we will check the functionality and communication of both containers (second line, your IP address may differ):
docker-compose exec nginx-test-compose-1 bash
curl 172.18.0.3
If we see the content of the file index.html, it means that all is working correctly. Both containers are running, they use the host system (file index.html) and are assigned to the same network called siet-test.
Summary
We have learned how to simply run multiple containers under one host system via Docker Compose.