Docker compose is system that makes the process of running multiple associated containers with different configuration options very simple. It is made up of two parts: the YAML configuration file and the CLI tool (docker-compose).
The docker-compose configuration file is written in a language called YAML (yam-uhl). Compose YAML has several versions starting with 1 (1, 2, 2.1 ,3, 3.1) and is specified with the very first line.
The file is separated into 3 sections after the version: Services, Volumes, and Networks.
This is the template for creating a docker-compose.yaml file:
version: '3.1' # if no version is specified then v1 is assumed. Reccomend v2 minimum
services: # containers. same as docker run
servicename: # a friendly name. this is also DNS name inside network
image: # Optional if you use build:
command: # Optional, replace the default CMD specified by the image
enviornment: # Optional, same as -e in docker run
volumes: # Optional, same as -v in docker run
depends_on: # Optional, used if another service must be started before this
servicename2:
volumes: # Optional, same as docker volume create
network: # Optional, same as docker network create
A docker-compose.yaml file for a wordpress site:
version: '2'
services:
wordpress:
image: wordpress
ports:
- 8080:80
enviornment:
WORDPRESS_DB_PASSWORD: example
volumes:
- ./wordpress-data:/var/www/html
mysql:
image: mariadb
enviornment:
MYSQL_ROOT_PASSWORD: example
volumes:
- ./mysql-data:/var/lib/mysql
Although this is still technically part of docker, the actual technology in docker-compose resides outside of the docker core functionality. This is why in Linux, installing docker-compose is a separate step.
The two most common commands are:
docker-compose up
— sets up all of the volumes/networks and starts all containers.docker-compose down
— stops all containers and removes all of the networks.
--volumes
or -v
at the end to remove all volumes as well.The up
command sets up everything you would need for an entire development/testing environment. It bootstraps the entire environment configuration process into a single command.
Other useful commands:
docker-compose ps
— list all of the containers created by docker-compose.docker-compose logs
— show all of the logs for the running services.Building Images with docker compose is really useful for complex builds that have a lot of variables or building arguments. You can use docker-compose to a create custom image from a Dockerfile. The “image” key then accepts a custom image name as its value.
Build configurations are made in the yaml file:
services:
servicename:
build:
context: # The directory that the dockerfile resides
dockerfile: # The name of the dockerfile
image: # The name of the custom image
If you don’t specify an image name, docker will create a unique image name for that build. This can be useful, because it is easier to remove when you wish to use docker-compose down
. Simply add --rmi local
at the end, and it will remove all of the custom images specified in the yaml file. This only works if the “image” key is not specified.
Note that if the image is changed, docker-compose up
will continue to use the old version of the image. This is because the image already exists, and unless the image is removed prior to the command, docker will just whatever is available. A better way to update the image is by using :
docker-compose build
This will re-build the image(s) specified in the yaml file.
You can also use docker-compose up --build
.