Master Docker Compose: Quick Setup, YAML Basics, and Advanced Tips
Docker Compose, Docker’s official orchestration tool, lets you define and run multi‑container applications via a simple YAML file, covering its history, installation, service definitions, essential CLI commands, and advanced features like environment variables, scaling, and network configuration to streamline development, testing, and production workflows.
Docker Compose is Docker’s official orchestration tool, first released in June 2014. It originated from the open‑source project fig created by Piston Cloud Computing, which Docker later integrated into its ecosystem. The tool enables defining and managing multi‑container applications with a single YAML file, ensuring consistency across development, testing, and production environments.
Version History
1.x : Initial release with basic orchestration.
2.x : Added support for the Docker Engine API.
3.x : Introduced service dependency ordering.
4.x : Added environment variables and command‑line options.
5.x : Added Docker Stacks support for Swarm mode.
How to Use Docker Compose
Installation
On most systems Docker Compose can be installed as a Python package.
# Install pip (Python package manager)
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
# Install Docker Compose with pip
pip install docker-composeDefining Services
Create a docker-compose.yml file. A basic example defines a web service using the nginx:alpine image and a database service using postgres:latest:
version: '3'
services:
web:
image: "nginx:alpine"
ports:
- "80:80"
volumes:
- "web-data:/var/www"
database:
image: "postgres:latest"
volumes:
- "db-data:/var/lib/postgresql/data"
volumes:
web-data:
db-data:Common CLI Commands
Start services: docker-compose up Run services in background: docker-compose up -d Stop services: docker-compose down Show service status: docker-compose ps Rebuild services: docker-compose up --build View logs: docker-compose logs Enter a container: docker-compose exec [service-name] /bin/bash Stop a single service: docker-compose stop [service-name] Start a single service: docker-compose start [service-name] Restart services:
docker-compose restartAdvanced Usage
Docker Compose also supports environment files, scaling, and custom networks.
Environment variables:
version: '3'
services:
web:
image: "nginx:alpine"
env_file:
- web.envScaling (replicas):
version: '3'
services:
web:
image: "nginx:alpine"
deploy:
replicas: 3Network configuration:
version: '3'
services:
web:
image: "nginx:alpine"
networks:
- webnet
networks:
webnet:Conclusion
Docker Compose is a powerful tool for defining and running multi‑container Docker applications, simplifying deployment and management while boosting development efficiency.
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.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
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.
