Docker Compose #
Docker Compose is useful for handling complex applications that utilize multiple containers.
Rather than manually running multiple docker run
commands, Docker Compose enables the execution of multiple containers using a single docker-compose.yml
file.
The overall structure is:
<container name>:
image: "<image>"
ports:
- <port>:<port>
links:
- <linked container name>
- <linked container name>
To bring up the entire application stack, run docker-compose up
.
Only applies to running on a single Docker host.
Docker run #
Naming containers in a Compose context is important.
docker run -d --name=<container name> <image>
Links #
Command line option to link containers together
docker run -d --name=<container name> -p <port>:<port> --link <container>:<container name in host>
Individual runs can have multiple links.
Build #
Can instruct Docker Compose to build an image with a build: ./<directory>
line in the docker-compose.yml
file, where <directory>
contains a Dockerfile
and other necessary components for the build. (Note: image
is replaced with build
)
<container name>:
build: "./<directory>"
ports:
- <port>:<port>
links:
- <linked container name>
- <linked container name>
Networks #
Can separate containers in the Docker Compose into different network components (e.g., front-end vs. back-end network components).
version: 2
services:
<container>:
image: <image>
networks:
- <network component_1>
<container>:
image: <image>
networks:
- <network component_2>
networks:
<network component_1>:
<network component_2>:
Versions #
Docker Compose has evolved over time, and there may be different designs floating about.
Changes introduced with version 2:
- Requirement to specify
docker-compose.yml
version (e.g.,version: 2
) - Specification of
services:
at the top - Later versions include a
depends_on:
property.
Changes introduced with version 3:
- Support for Docker stacks