Operations 13 min read

Master Docker Compose: Essential Commands and YAML Configuration Explained

This guide introduces Docker Compose as a powerful tool for defining and running multi‑container applications, explains its three‑layer architecture, lists the most frequently used docker‑compose commands with examples, and details the key sections and options of a docker‑compose.yml file, including a complete sample configuration.

Raymond Ops
Raymond Ops
Raymond Ops
Master Docker Compose: Essential Commands and YAML Configuration Explained

Docker Compose Common Commands and Properties

Docker Compose illustration
Docker Compose illustration

Docker Compose is a crucial tool for defining and running multi‑container Docker applications. By describing services, networks, ports, and other settings in a docker-compose.yml file, a single command can start, stop, and manage all containers, greatly simplifying development, testing, and deployment.

Docker Compose organizes containers into three layers: project, service, and container. All files in the working directory, especially docker-compose.yml, form a project. A project contains multiple services, each of which may run one or more container instances.

For Docker installation and basic commands, refer to external resources.

1. Docker Compose Common Commands

Commands must be executed in a directory that contains docker-compose.yml or docker-compose.yaml.

ps – list all running containers

docker-compose ps

build – build or rebuild services

docker-compose build

logs – view service logs

docker-compose logs

port – display the public port bound to a service

# Show the public port bound to MySQL service port 3306
docker-compose port mysql 3306

start – start existing containers of a service

docker-compose start mysql

stop – stop running containers of a service

docker-compose stop mysql

rm – remove containers of a service

docker-compose rm mysql

scale – set the number of containers for a service (service=num)

docker-compose mysql user=3 movie=3

run – execute a command in a service

docker-compose run web bash

up – build and start containers

docker-compose up

Notes

Docker‑compose files must be named docker-compose.yml (or .yaml) to use docker-compose up directly; for custom filenames, use docker-compose -f my-compose.yml up.

Both .yml and .yaml extensions are accepted.

Add -d to run containers in detached mode.

kill – send SIGKILL to stop containers

docker-compose kill mysql

pull – download service images

docker-compose pull mysql:latest

2. docker-compose.yaml File Attributes

The YAML file has strict indentation and consists of several top‑level sections:

version – specifies the Compose file format version.

services – defines each service (container) with image, environment, ports, volumes, etc.

networks – declares custom networks for inter‑service communication.

volumes – declares named volumes for persistent storage.

environment – sets environment variables for a service.

ports – maps container ports to host ports.

restart – defines container restart policies (no, always, on‑failure, unless‑stopped).

container_name – assigns a custom name to a container.

depends_on – expresses service startup order.

build – builds an image from a Dockerfile with optional build arguments.

Example of a service definition with build arguments:

version: '2'
services:
  webapp:
    build:
      context: ./my_dir
      dockerfile: Dockerfile-alternate
    args:
      buildno: 1

Image specification examples:

image: mysql
image: ubuntu:14.03
image: tutum/influxdb
image: example-registry.com:3000/postgresql
image: a4nhg65fd

Volume mapping examples (host:path, read‑only flag, named volumes, etc.).

Link and external_link definitions allow services to reference each other or external containers.

Network mode can be set to bridge, host, none, service:[name], or container:[id].

3. Complete docker-compose.yaml Example

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

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.