/
4. docker-compose pingme

4. docker-compose pingme

t shoud be apparent that there a quite a few steps involved in bringing up the two pingme apps in a configuration that suites our needs. Getting the steps wrong means stopping and deleting things, restarting things etc. There has to be a better way of doing? Yes there is, docker-compose.

Docker-compose allows you to spin up multiple containers at once, create bridges between those containers, and share volumes between them, all in a single configuration file.

We are going to use docker-compose to simplify the starting and stopping of our two pingme services.

Here is the complete docker-compose.yaml file that we will be working towards

We’ll break it down line by line.

Line 1. Markup standard that our script is based on

Lines 3 and 15. Top-level element definitions. Typically you will see; services, volumes, networks, configs, secrets etc.

services:

Lines 4 and 12. Defines the service name, NOT the container name

Line 5. Can be build or image - The image element will use an already available image, if it's not available the image will be pulled from docker.io. The build element will use the sub-elements like context and dockerfile to build the required image.

context - defines either a path to a directory containing a Dockerfile, or a url to a git repository.

dockerfile - allows you to set an alternate Dockerfile. A relative path MUST be resolved from the build context. So in our example lines 7 and 17 are not needed.

Lines 8 and 18. container_name - are the container names. This is an optional element.

Lines 9 and 19. restart - defines the policy that the platform will apply on container termination.

no: The default restart policy. Does not restart a container under any circumstances.

always: The policy always restarts the container until its removal.

on-failure: The policy restarts a container if the exit code indicates an error.

unless-stopped: The policy restarts a container irrespective of the exit code but will stop restarting when the service is stopped or removed.

Lines 10 and 20. ports - This is the same as the port switch on the docker run command.

Line 13. depends_on - sometimes you might need to containers to start in a specific order e.g. a ReSTful api depending on a database backend. Use this element to ensure that one container starts after another. The dependent container can depend on 1 or more containers.

Line 22. environment - Is used to create a number of environment variables that can be passed into a container.

networks:

Used to create network bridges that are shared amongst the containers.

Line 26. default - pingme-net will be the default network that all the containers created by the services will attach to

name - the name of the network

external - if true, the network must exist before docker-compose is run (you create the network using docker network create). If false, docker-compose will create the network during docker-compose up and tear it down during docker-compose down.