Deployment using Docker compose
There is only one more tool to cover in this tutorial, and that is for starting
and managing Docker containers a bit more easy than what is offered from
docker run
. This is especially true for microservice based systems where many
related Docker containers are expected to run at the same time. A very
convenient solution is the docker compose
command that can automate
docker run
and give more options on how to handle the life cycle of each
microservice. For cyber-physical systems this is especially useful, as the input
file to Docker compose also act as a specification of all running microservices,
and their respective version numbers.
To test this, start by opening three terminals, and in each run the command:
docker run --rm -ti --net=host registry.gitlab.com/USERNAME/opendlv-logic-primechecker:1.1
Make sure to change USERNAME
into your user name at GitLab. The three
terminals will start one Docker container each, and data will start to flow
between the microservices. It works, but it can be envisioned that this will be
complicated if there are a large number of microserrvices that should be started
and if each of them requires specific settings. The two main problems would be:
(1) the lack of overview of what is needed and what is currently running, and
(2) the lack of a single place to manage the life cycle, such as starting,
stopping, and monitoring logs, of all running containers. With Docker compose
this is solved by adding a single .yml
file that acts as a recipe on what
should run. Go ahead and create a docker-compose.yml
outside the code folder,
as this is not formally part of the code but rather the overall deployment, and
fill it with the following content (cd && gedit docker-compose.yml
):
version: '3'
services:
prime-checker-0:
image: registry.gitlab.com/USERNAME/opendlv-logic-primechecker:1.1
network_mode: "host"
command: "/usr/bin/helloworld"
prime-checker-1:
image: registry.gitlab.com/USERNAME/opendlv-logic-primechecker:1.1
network_mode: "host"
command: "/usr/bin/helloworld"
prime-checker-2:
image: registry.gitlab.com/USERNAME/opendlv-logic-primechecker:1.1
network_mode: "host"
command: "/usr/bin/helloworld"
Make sure to replace USERNAME
with your GitLab user name. As seen in the file
all microservices that are expected to run in the system are simply listed, and
any settings such as command line arguments can easily be added as well. Now, to
try this file with Docker compose, save and close it, and then run the following
command:
docker compose up
The command will directly look for a file called docker-compose.yml
in the
current directory and then start all the listed Docker images into containers.
If the file would have had another name, then the flag -f
could have been used
to indicate the name. Furthermore, if the containers are expected to run in the
background, without an active terminal which is common for cyber-physical
systems, then the -d
flag could have been added for detached mode.
In detached mode, the docker compose logs -f
command can be used to follow the
logs from all running containers, and docker compose down
can be used to stop
all containers.