• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 3: Managing Multi-Container Applications with Compose

Lesson 3: Managing Multi-Container Applications with Compose

3 min read

Introduction

Defining your multi-container application in a docker-compose.yml file is the first step. The next is effectively managing its lifecycle. This lesson focuses on the practical commands for starting, stopping, scaling, and monitoring your Compose-defined applications. We'll cover essential operations that enable you to interact with your entire application stack as a single unit.

Key Concepts

Starting and Stopping Services

  • docker compose up: The primary command to create and start services.

It performs several actions:

  1. Looks for docker-compose.yml (or specified -f file).

  2. Builds any images defined with build instructions.

  3. Creates networks and volumes if they don't exist.

  4. Starts containers for all services.

    • Use docker compose up -d to run in detached mode (background).
    • Use docker compose up --build to force image rebuilding before starting.
  • docker compose stop: Stops running containers gracefully without removing them. You can later restart them with docker compose start.

  • docker compose start: Starts stopped services.

  • docker compose restart: Restarts all services or specified services.

  • docker compose down: Stops and removes containers, networks, and (by default) named volumes associated with your Compose application. This is typically used for a full cleanup.

    • docker compose down --volumes: Also removes named volumes.
    • docker compose down --rmi all: Removes images used by any service.

Monitoring and Logging

  • docker compose ps: Lists the containers managed by Compose, showing their state, ports, and attached commands.

  • docker compose logs: Displays aggregated logs from all services or a specific service. This is invaluable for debugging.

    • docker compose logs -f: Follows the logs in real-time.
    • docker compose logs --tail 100: Shows the last 100 lines.

Executing Commands in Services

  • docker compose exec [service_name] [command]: Executes a command inside a running service container. Similar to docker exec but references the service name.
    • Example: docker compose exec api bash to get a shell inside your api service.

Scaling Services

  • docker compose up --scale [service_name]=[count]: Scales a service to a desired number of instances. This is a powerful feature for simple horizontal scaling on a single host.
    • Example: docker compose up --scale api=3 to run three instances of your api service.

Example/Code

Assume you have the full-stack docker-compose.yml from the previous lesson.

  1. Start the application in detached mode: This will build images (if necessary) and start all services in the background.
    bash
    undefined

docker compose up -d ```

  1. Check the status of services: See which containers are running.
    bash
    undefined

docker compose ps ```

  1. View logs from all services: Follow logs in real-time.
    bash
    undefined

docker compose logs -f ```

  1. Execute a command in the database container: Access the PostgreSQL client.
    bash
    undefined

docker compose exec database psql -U admin myappdb ```

  1. Scale the API service: Run two instances of your api service.
    bash
    undefined

docker compose up -d --scale api=2 ```

  1. Stop and clean up the application: Remove all containers, networks, and named volumes.
    bash
    undefined

docker compose down --volumes ```

Summary/Key Takeaways

  • docker compose up (with -d or --build) starts your application stack.

  • docker compose stop, start, restart, down manage the lifecycle of services.

  • docker compose ps and docker compose logs are vital for monitoring.

  • docker compose exec allows running commands inside a service container.

  • docker compose up --scale enables basic horizontal scaling of services.

End of lesson
👏Well done!
Previous Lesson
Lesson 2: Writing Docker Compose Files
Next Lesson
Lesson 4: Basic Troubleshooting and Best Practices

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Introduction to Docker Compose

Lesson 2: Writing Docker Compose Files

Lesson 3: Managing Multi-Container Applications with Compose

Lesson 4: Basic Troubleshooting and Best Practices

Quiz

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Introduction to Docker Compose

Lesson 2: Writing Docker Compose Files

Lesson 3: Managing Multi-Container Applications with Compose

Lesson 4: Basic Troubleshooting and Best Practices

Quiz