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.
Docker Compose Common Commands and Properties
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 psbuild – build or rebuild services
docker-compose buildlogs – view service logs
docker-compose logsport – display the public port a service is bound to
# Show the public port bound to MySQL service port 3306
docker-compose port mysql 3306start – start existing containers of a service
docker-compose start mysqlstop – stop running containers of a service
docker-compose stop mysqlrm – remove containers of a service
docker-compose rm mysqlscale – set the number of containers for a service (service=num)
docker-compose mysql user=3 movie=3run – execute a command in a service
docker-compose run web bashup – build and start containers
docker-compose upkill – send SIGKILL to stop containers
docker-compose kill mysqlpull – download service images
docker-compose pull mysql:latestup -d – start containers in detached mode
docker-compose up -d2. 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'Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
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.
