Module 5 Lesson 4: The Docker Compose CLI
Master the commands for managing multi-container stacks. Learn how to view aggregate logs, stop services safely, and rebuild images on the fly.
Module 5 Lesson 4: The Docker Compose CLI
Now that you have your docker-compose.yml file, you need to know how to interact with it. The docker-compose command (or docker compose in newer versions) is your control center.
1. The Core Lifecycle Commands
docker-compose up
Creates and starts all services in the file.
-d: Detached mode (background).--build: Force a rebuild of all images before starting.
docker-compose down
Stops and Removes all containers, networks, and images defined in the file.
-v: Also remove the Volumes (Be careful! This deletes your data).
docker-compose stop
Stops the containers but does not remove them.
2. Monitoring and Debugging
docker-compose ps
Lists the status of all containers managed by the current Compose file.
docker-compose logs
Shows the aggregated output from every container in the stack.
-f: Follow (stream) the logs in real-time.docker-compose logs web: Only show logs for the 'web' service.
docker-compose exec <service_name> <command>
Run a command inside a specific service.
- Example:
docker-compose exec db psql -U postgres
3. Modifying the Stack
If you change your docker-compose.yml file while the stack is running:
- Just run
docker-compose up -dagain. - Docker Compose will detect which parts of the file changed and only recreate those specific containers. It leaves the unchanged ones running. (This is incredibly powerful for uptime!)
4. Summary Table
| Command | Action | Result |
|---|---|---|
up -d | Start | Everything is running and linked |
logs -f | Watch | See errors across the whole stack |
stop | Pause | Everything is off, but state is kept |
down | Destroy | Clean machine (no containers left) |
build | Compile | Re-run the Dockerfiles |
Exercise: The Workflow Race
- Run
docker-compose up -d. - Make a small change to your
nginxconfiguration (e.g., change the mapped port). - Run
docker-compose up -dagain. Note the output. Did it say "Recreating" or "Starting"? - Use
docker-compose logs -fand then refresh your browser page. Do you see the logs appearing in your terminal? - What happens to your volumes if you run
docker-compose down? What aboutdocker-compose down -v?
Summary
The Compose CLI is tailored for managing groups of containers. By mastering up, down, and logs, you gain total visibility into the complex web of interactions that make up a modern multi-service application.
Next Lesson: Putting it all together: A real-world multi-container application example.