Master Docker Compose: From Installation to Advanced Commands
This comprehensive guide walks you through Docker Compose fundamentals, offline installation steps, essential commands like up, down, stop, start, logs, and detailed configuration options for services, networks, volumes, and best practices for maintaining clean and version‑controlled compose files.
1. Docker Compose Overview
Docker Compose is a tool for defining and running multi‑container Docker applications using a single docker-compose.yml file to configure services such as web servers, databases, and caches.
2. Installing docker‑compose
Download the binary from the official GitHub releases, upload it to your Linux server, move it to
/usr/local/bin/docker-compose, and make it executable:
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-composeVerify the installation:
docker-compose --version3. Common docker‑compose Commands
3.1 docker-compose up
Usage:
docker-compose up [options]Example:
docker-compose up -dFunctions:
Create and start services: If services are not yet created, Docker Compose builds and starts them based on the
docker-compose.ymldefinition.
Run in background: The
-dflag runs containers in detached mode.
Recreate containers: Existing containers are stopped and removed before new ones are created.
Build images: Images are built if a
builddirective is present.
Validate configuration: The file is checked before starting services.
Use cases:
Initial deployment – ensures all services are created with the latest configuration.
Service updates – applies changes after modifying the compose file or images.
State synchronization – guarantees services run with the current definition.
3.2 docker-compose down
Usage:
docker-compose down [options]Functions:
Stops and removes containers, networks, and optionally volumes (
-v) defined in the compose file.
Removes images when
--rmiis specified.
Typical options:
--volumesor
-v: delete all defined volumes.
--rmi all: delete all service images.
-h,
--help: display help.
Examples:
docker-compose down docker-compose down --volumes --rmi all docker-compose down --rmi local docker-compose -f my-compose-file.yml down3.3 docker-compose stop
docker-compose stopStops all containers defined in the compose file without removing them.
3.4 docker-compose start
docker-compose startStarts containers that were previously stopped.
3.5 docker-compose restart
docker-compose restartRestarts stopped services; changes to containers require a new deployment.
3.6 docker-compose ps
docker-compose psLists containers, showing status, command, and ports.
3.7 docker-compose rm
Removes stopped containers (or all containers with
-a) and optionally associated volumes or images.
docker-compose rm [OPTIONS] [SERVICE...]Key options include
-f/--file,
-v/--volumes,
-a/--all, and
--rmi.
3.8 docker-compose logs
Shows logs for services. Useful for debugging.
docker-compose logs [options] [SERVICE...] --followor
-f: stream logs.
--tailor
-t: limit output lines.
--no-color: disable color.
--timestampsor
-T: include timestamps.
3.9 Using Multiple Compose Files
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up4. docker-compose.yml Configuration Details
4.1 Top‑level Keys
version : Compose file format version.
services : Service definitions.
networks : Network definitions.
volumes : Volume definitions.
4.2 Service Configuration Options (selected)
container_name : Custom container name.
image : Image name or ID.
build : Path to Dockerfile (or
contextand
dockerfile).
command : Override default command.
depends_on : Service dependency ordering.
environment : Environment variables (list or map).
expose : Expose ports internally.
ports : Port mappings (HOST:CONTAINER).
extra_hosts : Add host‑name mappings.
networks : Attach service to networks.
restart : Restart policy (no, always, on‑failure, unless‑stopped).
healthcheck : Define health check command and parameters.
env_file : Load environment variables from a file.
4.3 Volumes
Mount host paths or named volumes into containers, optionally read‑only.
4.4 Networks
Define custom networks, use default network, or attach to existing external networks.
5. Best Practices
Keep
docker-compose.ymlconcise; split development and production settings into separate files.
Version‑control the compose files to track configuration changes across environments.
Use named volumes to preserve data across container restarts.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.