Cloud Native 13 min read

Master Docker Compose: Essential Commands and YAML Configuration Explained

This guide walks you through Docker Compose’s most useful commands, explains the structure and key fields of a docker‑compose.yml file, and provides practical examples and tips for building, running, and managing multi‑container Docker applications efficiently.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Docker Compose: Essential Commands and YAML Configuration Explained

Docker Compose Common Commands

Docker Compose manages multi‑container applications defined in a docker-compose.yml file. The most frequently used commands are:

ps – List containers in the current project. docker-compose ps build – Build or rebuild images for services. docker-compose build up – Create and start containers. Add -d to run in detached mode. docker-compose up -d down – Stop and remove containers, networks, and volumes created by up. docker-compose down logs – View combined logs of all services (or a specific one). docker-compose logs -f redis start – Start existing stopped containers. docker-compose start mysql stop – Stop running containers. docker-compose stop mysql rm – Remove stopped containers (add -f to force). docker-compose rm -f redis run – Run a one‑off command in a new container of a service. docker-compose run web bash exec – Execute a command in an already‑running container.

docker-compose exec web python manage.py migrate

port – Show the host port mapped to a container port. docker-compose port mysql 3306 pull – Pull service images from a registry. docker-compose pull redis:latest When the compose file has a non‑default name, specify it with -f, e.g. docker-compose -f my-compose.yml up.

docker-compose.yml File Structure

A Compose file is indentation‑sensitive YAML. The top‑level sections are:

version – Compose file format version (e.g. "3" for Docker 1.13+).

services – Definition of each container (service) and its configuration.

networks – (Optional) Custom network definitions.

volumes – (Optional) Named volume definitions.

Key Service Fields

image – Image name (registry/repo:tag) or ID. image: redis:latest container_name – Custom name for the container. container_name: im-redis-compose build – Build instructions when an image must be built from a Dockerfile.

build:
  context: ./my_dir
  dockerfile: Dockerfile-alternate
  args:
    buildno: 1

environment – Environment variables passed to the container.

environment:
  - DATA_SOURCE_NAME="user:password@(hostname:3306)/"

ports – Host‑to‑container port mappings. Simple "HOST:CONTAINER" format or long format (v3.2+).

# Simple mapping
- "3000:3000"
# Long format example
- target: 80
  published: 8080
  protocol: tcp
  mode: host

volumes – Volume mounts. Supports anonymous, host‑path, and named volumes with optional ro / rw flags.

volumes:
  - /var/lib/mysql                     # anonymous
  - /opt/data:/var/lib/mysql           # host‑path bind
  - ./cache:/tmp/cache                 # relative bind
  - datavolume:/var/lib/mysql           # named volume

restart – Restart policy (e.g. no, always, on-failure, unless-stopped).

network_mode – Set the network mode ( bridge, host, none, service:[service], container:[id]).

depends_on – Declare service dependencies to control start order.

links – Legacy way to link containers; also influences start order.

pid – Share the host PID namespace ( host).

Example docker-compose.yml

version: "3"
services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    container_name: im-redis-compose
    restart: always
    command: redis-server --appendonly yes

  rabbitmq:
    image: rabbitmq:management
    ports:
      - "5672:5672"
      - "15672:15672"
    container_name: im-rabbitmq-compose
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
      RABBITMQ_DEFAULT_VHOST: my_vhost

  backend:
    build: .
    links:
      - redis
      - rabbitmq
    container_name: im-server-compose
    restart: on-failure
    depends_on:
      - rabbitmq
      - redis
    ports:
      - "3000:3000"
    command: sh -c './wait-for.sh rabbitmq:15672'

This example defines a three‑service stack (Redis, RabbitMQ, and a custom backend) with explicit container names, restart policies, port mappings, environment variables, and inter‑service dependencies.

Version Compatibility

Compose file formats have three major versions: 1, 2.x, and 3.x. The current mainstream is 3.x, which requires Docker Engine 1.13.0 or newer.

Specify the version at the top of the file, e.g. version: "3", to enable the corresponding syntax and features.

Additional Service Options

expose – Expose ports to linked services without publishing them to the host.

expose:
  - "3000"
  - "8000"

networks – Attach a service to one or more custom networks and optionally define aliases.

networks:
  some-network:
    aliases:
      - alias1
  other-network:
    aliases:
      - alias2
Docker Compose illustration
Docker Compose illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerYAMLcontainer orchestrationDocker Compose
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

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.