N. P. O'Donnell, 2020
- Docker Compose is not a part of Docker
- Consists of 2 parts: CLI and
.yaml
files - Deals in services (a service is comprised of several containers)
- Default compose file is
docker-compose.yml
but any filename may be used - First line of compose file is version, but compose file version does not correspond to docker compose version.
sudo apt-get install docker-compose
-or-
pip install docker-compose
Discover version:
docker-compose version
Bring everything up. Run in a directory with a docker-compose.yml
file
docker-compose up
Same thing, but daemonized:
docker-compose up -d
Same thing, but force a rebuild:
docker-compose up --build
Bring everything down
docker-compose down
To refresh a service:
docker-compose up
A very simple docker-compose.yml
:
version: '2.0'
services:
solution:
build .
image: solution
Note: the build .
directive tells docker-compose
to build the solution
image and the location of the Dockerfile is .
.
docker-compose can be run on a remote machine with:
DOCKER_HOST=ssh://<user>@<host> docker-compose up -d
and
DOCKER_HOST=ssh://<user>@<host> docker-compose down
This requires key-based SSH to be set up.
Compose v2 is primarily used for local development and v3 is used for orchestrators such as swarm or kubernetes.
A bind mount is a mechanism where directory on the host machine can be accessed in the container through a mount on the container's file system. Bind mounts are handy for local development.
To create a bind mount, use the volumes
property in the compose file:
volumes:
# Just specify a path and let the Engine create a volume
- /container/location
# Specify an absolute path mapping
- ./myfolder/location:/container/location
Volumes are similar to bind mounts but use the docker engine's volume subsystem, and are more suited to production use. Volumes use a similar syntax to bind mounts:
volumes:
- type: volume
source: db-data
target: /var/lib/mysql/data
For debugging, you may want to shell into one of the containers (services) while it's running:
docker-compose exec <service> /bin/sh
Or after it runs:
docker-compose run <service> /bin/sh