Docker Compose
Docker Compose is a tool for defining and deploying multi container applications.
Table of Contents
- Installation
- Creating a Docker Compose File
- Starting and Stopping the Containers
- Updating a Container
- Scaling
Installation
To use Docker Compose, we first have to install it. Depending on the distro used, this step may vary. This example will be using Debian Linux for demonstrations. The command will be:
apt install docker-compose
Alternatively, we can install the Docker Engine from the official Docker repositories and use thedocker compose command.
Note: Commands used here will be using the package from the official Docker repositories (docker compose). If installed from the Debian repositories, add a dash (docker-compose).
Creating a Docker Compose File
Once installed, we will need to create a file called compose.yaml. This Docker Compose file will have all the instructions we will need in order to deploy containers.
The following will be a basic structure of the Docker Compose file.
services:
app1:
image: 'image/here' #Image to use
restart: policy-here #always, unless-stopped, on-failure, no (default option)
ports: #Expose and map ports from host to container
- '5000:80' #Host:Container port
- '5000:5000'
networks: #Specify what network the container is in
network-name-here: #Network name
app2:
image: 'image/here'
restart: policy-here
networks:
network-name-here:
app3:
image: 'image/here'
restart: policy-here
networks:
network-name-here:
networks:
network-name-here:
driver: driver-here #bridge, host, none, overlay, ipvlan, macvlan
Note: If you are using a older version of Docker (i.e. installed from the Debian repositories), the version: tag is required at the start of the docke-compose.yml file.
Starting and Stopping the Containers
Once the Docker Compose file is created, we can run it with the following command:
docker compose up -d
Command breakdown:
up- Starts the containers using thecompose.yamlfile.-d- Run in detached mode.
Note: The above command will only work if there a file called compose.yaml present in the current working directory.
To specify a Docker Compose file to use, we can use the -f flag. An example will be:
docker compose -f my-docker.yaml up -d
The file does not have to be named compose.yaml for Docker Compose to run it.
To stop a group of containers with Docker Compose, we can use the following command:
docker compose down
To specify a file:
docker compose -f My_File.yaml down
Command breakdown:
down- Tells Docker Compose to stop all containers specified in the YAML file.-f- Specifies the YAML file to use.
Updating a Container
To update a container, we can simply use the following command.
docker compose pull && docker compose up -d --force-recreate && docker image prune
The above command will pull for the latest image (if any), recreate the Docker containers (stack), and remove any unused images.
Scaling
To scale containers, we can use the sudo docker compose scale <service_name>=<number of replicas>. An example will be where we have a stack running a service called nginx.
sudo docker scale nginx=3
The above command will create 3 replicas of the nginx container.
We can also specify this in the compose.yaml file using the deploy option. An example:
services:
nginx:
image: nginx
restart: unless-stopped
ports:
- '80:80'
deploy:
mode: replicated
replicas: 3
The above stack will create 3 instances of the nginx container.