D: Docker Compose is a powerful tool for defining and running multi-container Docker applications. �🐋 Whether you’re a developer, DevOps engineer, or just starting with containers, this cheat sheet will help you master the most commonly used Docker Compose commands.
Let’s dive in! 🚀
🔹 1. Basic Docker Compose Commands
Start Services
docker-compose up
- Starts all services defined in
docker-compose.yml
. - Use
-d
to run in detached mode (background):docker-compose up -d
Stop Services
docker-compose down
- Stops and removes containers, networks, and volumes.
- Add
--volumes
to also remove named volumes:docker-compose down --volumes
List Running Containers
docker-compose ps
- Shows the status of all containers managed by Compose.
🔹 2. Managing Services
Start a Single Service
docker-compose up
<service_name>
- Example:
docker-compose up nginx
Stop a Single Service
docker-compose stop
<service_name>
- Example:
docker-compose stop mysql
Restart Services
docker-compose restart
- Restarts all services.
- To restart a specific service:
docker-compose restart <service_name>
🔹 3. Logs & Debugging
View Logs
docker-compose logs
- Shows logs from all services.
- To follow logs in real-time:
docker-compose logs -f
- To see logs for a specific service:
docker-compose logs <service_name>
Execute Commands Inside a Container
docker-compose exec
<service_name> <command>
- Example (accessing a MySQL shell):
docker-compose exec mysql mysql -u root -p
🔹 4. Building & Updating Services
Rebuild Images
docker-compose build
- Rebuilds all images defined in
docker-compose.yml
. - To rebuild a specific service:
docker-compose build <service_name>
Force Recreate Containers
docker-compose up --force-recreate
- Useful when you change environment variables or configurations.
🔹 5. Advanced Commands
Scale Services
docker-compose up --scale
<service_name>=<num_instances>
- Example (running 3 instances of a web service):
docker-compose up --scale web=3
Check Service Dependencies
docker-compose config
- Validates and displays the final configuration.
🎉 Bonus Tips for Docker Compose Newbies
✅ Always use docker-compose down
before docker-compose up
to avoid conflicts.
✅ Use docker-compose pull
to update images before starting services.
✅ Store sensitive data (like passwords) in .env
files instead of hardcoding them.
Final Thoughts 🏁
Docker Compose simplifies container orchestration, and mastering these commands will save you tons of time! Bookmark this cheat sheet and refer to it whenever needed.
Happy Dockering! 🐳💙
(Need more help? Check the official Docker Compose docs!)