Operations 12 min read

Master Docker Compose: Essential Commands and YAML Configuration Explained

This guide introduces Docker Compose, explains its project‑service‑container hierarchy, lists the most useful docker‑compose commands with examples, and details the key sections and attributes of a docker‑compose.yml file, including version, services, networks, volumes, and restart policies.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
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 tool for defining and running multi‑container Docker applications using a docker-compose.yml file that describes services, networks, volumes and other settings. With a single command you can start, stop and manage the whole application stack.

Compose organizes containers into three layers: project, service and container. All files in the working directory, especially docker-compose.yml, constitute a project. Each service defines the image, environment variables, ports, volumes, etc., and a service may run multiple container instances.

1. Common Docker Compose Commands

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 a service is bound to

# 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

kill – send SIGKILL to stop containers

docker-compose kill mysql

pull – download service images

docker-compose pull mysql:latest

up -d – start containers in detached mode

docker-compose up -d

2. docker-compose.yml 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 with fields such as image, container_name, environment, ports, volumes, depends_on, etc.

networks – configures custom networks and aliases.

volumes – declares named volumes or host‑path mappings.

Key service fields include: image: the container image to use. container_name: custom container name. environment: environment variables passed to the container. ports: host‑to‑container port mappings. volumes: file or directory mounts, with optional ro or rw modes. restart: restart policy (e.g., always, on-failure). network_mode: sets the network mode (bridge, host, none, etc.).

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

DockerDevOpsYAMLcontainer orchestrationDocker Compose
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.