Master Docker Compose: Installation, Commands, and Best Practices
This guide explains what Docker Compose is, how to install it offline, walks through the most common docker‑compose commands, details the structure and options of a docker‑compose.yml file, and offers practical tips for managing multi‑container Docker applications.
Docker Compose Overview
Docker Compose is a tool for defining and running multi‑container Docker applications using a single docker-compose.yml file that describes all required services such as web servers, databases, and caches.
Installation (offline method)
Download the binary from the official GitHub releases page, transfer it to the target Linux host, 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-composeVerify the installation with: docker-compose --version To uninstall, simply remove the binary:
sudo rm /usr/local/bin/docker-composeCommon Docker‑Compose Commands
docker-compose up – creates and starts services; -d runs them in detached mode.
docker-compose down – stops and removes containers, networks, and optionally volumes and images ( --volumes, --rmi all).
docker-compose stop – stops containers without removing them.
docker-compose start – starts previously stopped containers.
docker-compose restart – restarts containers; useful after configuration changes.
docker-compose ps – lists containers and their status.
docker-compose rm – removes stopped containers; options allow removing volumes, images, or all containers.
docker-compose logs – shows service logs; useful options include --follow, --tail, --no-color, and --timestamps.
docker‑compose.yml Configuration Details
The file consists of top‑level keys: version – specifies the Compose file format version. services – defines each service and its settings. networks – declares custom networks used by services. volumes – declares named volumes for persistent storage.
Key service options include: container_name – custom container name. image – image name or ID. build – path to a Dockerfile (or context and dockerfile). command – overrides the default container command. depends_on – defines startup order dependencies. environment – sets environment variables (list or dictionary). expose – exposes internal ports without host mapping. ports – maps host ports to container ports (e.g., "8000:80"). extra_hosts – adds host‑to‑IP mappings inside the container. networks – attaches the service to one or more declared networks, optionally with aliases. restart – restart policy (no, always, on-failure, unless-stopped). healthcheck – defines a command to test container health. env_file – loads environment variables from external files.
Version compatibility table (selected entries):
Compose specification – Docker Engine 19.03.0+
3.8 – Docker Engine 19.03.0+
3.7 – Docker Engine 18.06.0+
3.6 – Docker Engine 18.02.0+
3.5 – Docker Engine 17.12.0+
3.4 – Docker Engine 17.09.0+
3.3 – Docker Engine 17.06.0+
3.2 – Docker Engine 17.04.0+
3.1 – Docker Engine 1.13.1+
3.0 – Docker Engine 1.13.0+
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.
Example minimal compose file for a web service, an application service, and a MySQL database demonstrates the use of images, ports, volumes, environment variables, networks, dependencies, and restart policies.
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.
