Posted on 07 Nov 2019
This is the seventh article of the Getting started with Docker series. In this article, I will discuss Docker Compose and how to use it to improve the code developed so far for our PostgreSQL cluster.
In the code developed so far, we used some helper scripts to create and clean up the postgresql image and volumes, to start and stop the containers. If the application you are building requires more than one container you can manage the image build and runtime configuration with a new Docker tool: Docker Compose.
Docker Compose is a command-line tool that talks with the docker engine and it is able to manage multi containers applications deployed on a single node. The tool is able to build the application docker image and runtime configuration like port mapping, bind and volumes mounts, networking, IPC, and so on.
You can think of the Docker Compose command line as a wrapper that extends the functionalities of the docker command line.
Docker Compose takes in input a YAML file and uses it to create one or more services (the applications) deployed in one or more containers. The YAML file, for each service, specifies the Docker image to use and how to configure it at runtime.
The following commands work exactly the same as their docker counterparts, the only difference is that they can affect more than one container.
The most important Docker Compose commands are:
The former builds the required docker image if they don’t exist and start the containers. The latter stop the containers and other docker resources.
For more details on docker-compose commands read the official documentation.
The goal of this section is to modify our PostgreSQL application to leverage on Docker Compose to improve the code removing all the helpers’ scripts used so far.
To do that, we need to create the YAML file where we define three PostgreSQL services (applications), each one having:
Here you can see the YAML file. The tag version specifies the version of the YAML file.
Then we define the three service volumes.
The services tag defines the three services (applications) to manage: node1, node2, and node3.
Let’s see in detail the definition of one of these services. The container has a name and it has the image postgresql as a template. If the image doesn’t exist in the local docker registry a new one is built using the Dockerfile in ../src.
The service will use the following environment variable whose values are in the .env file.
Container port 5432 maps on host 5432 port. The other two containers map the local 5432 port host port 5433 and 5434.
All three containers store logs and data directory in /home/postgres/data mapped with docker volumes.
Finally, the container connects to the network bridge driver and has IP 10.0.2.31 and hostname node1.domain.com.
Here the definition of the bridge driver with subnet 10.0.2.1/24.
To start the cluster you can type the following command.
The test of the upgrade and failover scenarios is exactly the same as the previous article. The only difference is that this time you can use the docker-compose command instead of the docker one.
To shut down the cluster you can type the following command.
This command does not remove the volume so the data still exists. If you start again the cluster no data loss occurs. If you want to remove the volume you can add the -d option to the command.
In this article, we explained what Docker Compose is and how to use it to deploy multi containers application on a single host. However, have a database cluster with all containers on a single node is ok in development and test environments but makes no sense in production because if the node goes down the whole cluster crash. In the next article, to solve this issue and implement a real High Availability solution we need to leverage on Docker Swarm.