Introduction
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
For example, it can be used to deploy a web application with a database, a cache, a message broker and other services, with the infraestructure declared in a single file and version controlled.
Installation
Can be installed during Docker Installation.
Structure
Create the compose file
The file is named docker-compose.yml
and is written in YAML format. It is located in the root of the project.
Services
The services are the containers to be orchestrated. Each service has a name and a configuration.
- In this example, the service is named
database
and it uses thepostgres
image, which is a database server. - To access the database from the host, the port
8082
is forwarded to the port80
of the container via theports
key. - To prevent the container from stopping, the
restart
is set toalways
. - The environment variables can be set with the
environment
key. In this case, the user, password and database name are set. This is specific to the postgres image, which can be checked in the official image documentation. - It mounts the host folder
./docker_data/create.sql
to the container folder/docker-entrypoint-initdb.d/init.sql
with thevolumes
key. This is used to initialize the database with a script. - It uses a volume named
postgres_data
to persist the data of the database, that is located at/var/lib/postgresql/data
in the container.
Networks
The Docker Networking can be defined with named networks and have their own configuration.
- The
networks
key is used to link the services to the networks. - In this example, the network is named
coffee
and it has a subnet of192.168.1.0/24
with thesubnet
key. - The
driver
key is set todefault
, which is the default network adapter. It can be changed tobridge
,host
,overlay
,macvlan
,none
,null
,custom
orplugins
. - The
ipam
key is the IP Address Manager, which is used to manage the IP addresses of the network. - The
config
key is the configuration for the network.
Persistance
The volumes can be configured with a specific name to be used elsewhere in the compose file to enable persistance.
- The
volumes
key is used to define the volumes. - In this example, a volume is provisioned, named
postgres_data
.
Network and Service Link
Creating another service called webApp
that depends on the database
service to work properly and is connected to the coffee
network.
- The
depends_on
key is used to specify the services that must start before this one. - The
networks
key is used to link the service to the networks. In this case, thewebApp
service is connected to thecoffee
network. - The
ipv4_address
key is used to set the IP address of the service. This is optional and can be omitted.
Full file
docker-compose.yml
it is the file that contains the configuration of the services and networks that composes the application. It is written in YAML format. It is located in the root of the project.
Essential Commands
Up
Starts the containers and networks
Daemon mode
Starts the containers and networks in the background
Down
Deletes the containers and networks
Delete volumes
Also deletes the volumes
Stop
Stops the containers, doesn’t delete
Ps
Lists the containers generated by the compose file’
Examples
Curated List of Compose Templates