Module 5 Lesson 4: The Docker Compose CLI
·DevOps

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 -d again.
  • 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

CommandActionResult
up -dStartEverything is running and linked
logs -fWatchSee errors across the whole stack
stopPauseEverything is off, but state is kept
downDestroyClean machine (no containers left)
buildCompileRe-run the Dockerfiles

Exercise: The Workflow Race

  1. Run docker-compose up -d.
  2. Make a small change to your nginx configuration (e.g., change the mapped port).
  3. Run docker-compose up -d again. Note the output. Did it say "Recreating" or "Starting"?
  4. Use docker-compose logs -f and then refresh your browser page. Do you see the logs appearing in your terminal?
  5. What happens to your volumes if you run docker-compose down? What about docker-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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn