This is a field guide to Docker Compose. Docker Compose is a Docker “tool for defining and running multi-container Docker applications.” From a single defined YAML file, users can run multiple services and containers with a single command.
Docker Compose works in three steps:
- Defining your app’s environment with a Dockerfile.
- Defining your services with a Docker Compose YAML file.
- Running Docker Compose on the Docker Compose YAML file.
Useful Docker Compose Commands
- docker swarm init
# Initialize swarm
This command initializes a Docker swarm. The Docker engine targeted by this command becomes the manager node (indeed, the only node) in the new swarm.
This command has these options:
- advertise-addr: Advertised address (format:
[:port]) - autolock: Enable manager autolocking (requiring an unlock key to start a stopped manager)
- availability: Availability of the node (“active”|”pause”|”drain”) (default: “active”)
- cert-expiry: Validity period for node certificates (ns|us|ms|s|m|h) (default: “2160h0m0s”)
- data-path-addr: Address or interface to use for data path traffic (format:
) - dispatcher-heartbeat: Dispatcher heartbeat period (ns|us|ms|s|m|h) (default: 5s)
- external-ca: Specifications of one or more certificate signing endpoints
force-new-cluster: Force create a new cluster from current state
- advertise-addr: Advertised address (format:
- listen-addr: Listen address (format:
[:port]) (default: 0.0.0.0:2377) - max-snapshots: Number of additional Raft snapshots to retain
- snapshot-interval: Number of log entries between Raft snapshots (default: 10000)
- task-history-limit: Task history retention limit (default: 5)
- docker swarm init --advertise-addr 192.168.99.100
# Initialize swarm (with IP address)
This command may be required if your computer has multiple IP addresses on different interfaces.
- docker swarm leave --force
# Remove a worker from the swarm
This command removes the current worker from the swarm. The “–force” component ensures that this command is executed, even if the node leaving the swarm is a manager maintaining the swarm’s quorum.
This command has these options:
- --force, f: Force this node to leave the swarm, ignoring warnings
- docker stack ls
# List available Docker stacks
This command lists currently running Docker stacks.
- docker stack deploy -c docker-compose.yml exampleApp
# Build or update the stack
This file runs the selected Docker Compose file to create a Docker stack, creating a Docker app.
This command has these options:
- bundle-file: Path to a Distributed Application Bundle file
- compose-file, -c: Path to a Compose file
- prune: Prune services that are no longer referenced
- resolve-image always: Query the registry to resolve image digest and supported platforms (“always”|”changed”|”never”)
- with-registry-auth: Send registry authentication details to Swarm agents
- docker stack deploy -c docker-compose.yml exampleApp
# Redeploy the stack - docker stack rm exampleApp
# Remove the stack and app
This command takes down the current app and its stack from the current swarm.
- docker service ls
# List running Docker services
This command lists services running in a swarm, when targeting a manager node for that swarm.
This command has these options:
- filter, -f: Filter output based on conditions provided
- format: Pretty-print services using a Go template
- quiet, -q: Only display IDs
- docker service ps <service>
#
This command lists tasks that are running in a swarm, when targeting a manager node for that swarm.
This command has these options:
- filter, -f: Filter output based on conditions provided
- format: Pretty-print tasks using a Go template
- no-resolve: Do not map IDs to Names
- no-trunc: Do not truncate output
- quiet , -q: Only display task IDs
- docker inspect <task or container>
# Inspect a task or container
This command inspects a task or container, returning low-level information about it.
This command has these options:
- format, -f: Format the output using the given Go template
- size, -s: Display total file sizes if the type is container
- type: Return JSON for specified type
- docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <task or container>
# Retrieve an IP address - docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' <task or container>
# Retrieve a MAC address - docker inspect --format='{{.LogPath}}' <task or container>
# Retrieve a log path - docker inspect --format='{{.Config.Image}}' <task or container>
# Retrieve an image name - docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' <task or container>
# Loop over all port bindings - docker inspect --format='{{(index (index .NetworkSettings.Ports “8787/tcp”) 0).HostPort}}' <task or container>
# Retrieve a port binding - docker inspect --format='{{json .Config}}' <task or container>
# Get a JSON inspection subsection
Docker Swarms
- docker-machine create --driver virtualbox myvm1
# Create Docker machine
This command creates a Docker machine using VirtualBox.
This command requires the “driver” flag to indicate the provider on which the machine should be created, either locally or remotely.
- docker-machine create --driver virtualbox myvm2
- docker-machine create --driver virtualbox myvm3
- docker-machine ls
# List Docker machines
This command lists available Docker machines.
- docker-machine ssh myvm1
# Access Docker machine
This command logs into or runs a command on a Docker machine.
It requires the machine name (from docker-machine ls).
- docker-machine ssh myvm1 free
# Run Docker machine command - docker-machine ssh myvm1 docker swarm init --advertise-addr 192.168.99.101
# Initialize Docker swarm
- docker-machine ssh myvm2 docker swarm join --token <token> <ip-of-original-machine>:2377
# Join swarm as worker
This command joins the current docker machine as a node to a swarm.
Port 2377 is the standard Docker machine swarm management port and should be used.
- docker-machine ssh myvm1 docker swarm join-token manager
# Add manager to swarm
This command provides instructions for joining a node to the swarm as a manager.
- docker-machine ssh myvm1 docker node ls
# View Docker nodes - docker-machine env myvm1
# Get new environment - eval $(docker-machine env myvm1)
# Change machine environment - docker-machine ls
# Confirm new environment
The active machine should be changed to myvm1 in the output.
- docker stack deploy -c docker-compose.yml getstartedlab
# Deploy app in new environment - docker stack ps getstartedlab
# Confirm distributed app
Services and containers should be distributed between the swarm’s virtual machines.
- docker stack rm getstartedlab
# Tear down the stack - eval $(docker-machine env -u)
# Unset environment variables - docker-machine stop $(docker-machine ls -q)
# Stop Docker machines
This commands stops all running Docker machines.
- docker-machine rm $(docker-machine ls -q)
# Remove Docker machines
This command removes a Docker machine from a swarm.
- docker-machine scp docker-compose.yml myvm1:~
# Copy file to machine
This command copies files from the local host to a machine.
Sample Files
These files support the Docker Compose language guide above. They were taken from the Docker Compose docs.
Sample docker-compose YAML file.
Copy this text and paste it into a file named “docker-compose.yml” in the project directory.
The name “docker-compose.yml” is only a convention. The YAML file can be named whatever you want.
- version: "3"
- services:
- web:
- image: username/repo:tag
# Custom user and image
Replace “username”, “repo”, and “tag” with the registry values of a repository you control.
- # image: friendlyhello
# Local image
Reference a local image instead of one on a registry.
- deploy:
- replicas: 5
- restart_policy:
- condition: on-failure
- resources:
- limits:
- cpus: "0.1"
- memory: 50M
- ports:
- - "80:80"
- networks:
- - webnet
- visualizer:
- image: dockersamples/visualizer:stable
- ports:
- - "8080:8080"
- volumes:
- - "/var/run/docker.sock:/var/run/docker.sock"
- deploy:
- placement:
- constraints: [node.role == manager]
- networks:
- - webnet
- redis:
- image: redis
- ports:
- - "6379:6379"
- volumes:
- - /home/docker/data:/data
- deploy:
- placement:
- constraints: [node.role == manager]
- command: redis-server --appendonly yes
- networks:
- - webnet
- networks:
- webnet: