Operations 21 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Master Docker Compose: From Installation to Advanced Commands

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-compose

Verify the installation:

docker-compose --version

3. Common docker‑compose Commands

3.1 docker-compose up

Usage:

docker-compose up [options]

Example:

docker-compose up -d

Functions:

Create and start services: If services are not yet created, Docker Compose builds and starts them based on the

docker-compose.yml

definition.

Run in background: The

-d

flag 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

build

directive 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

--rmi

is specified.

Typical options:

--volumes

or

-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 down

3.3 docker-compose stop

docker-compose stop

Stops all containers defined in the compose file without removing them.

3.4 docker-compose start

docker-compose start

Starts containers that were previously stopped.

3.5 docker-compose restart

docker-compose restart

Restarts stopped services; changes to containers require a new deployment.

3.6 docker-compose ps

docker-compose ps

Lists 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...]
--follow

or

-f

: stream logs.

--tail

or

-t

: limit output lines.

--no-color

: disable color.

--timestamps

or

-T

: include timestamps.

3.9 Using Multiple Compose Files

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

4. 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

context

and

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.yml

concise; 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.

Docker Compose illustration
Docker Compose illustration
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.