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:
-
Looks for
docker-compose.yml(or specified-ffile). -
Builds any images defined with
buildinstructions. -
Creates networks and volumes if they don't exist.
-
Starts containers for all services.
- Use
docker compose up -dto run in detached mode (background). - Use
docker compose up --buildto force image rebuilding before starting.
- Use
-
docker compose stop: Stops running containers gracefully without removing them. You can later restart them withdocker 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 todocker execbut references the service name.- Example:
docker compose exec api bashto get a shell inside yourapiservice.
- Example:
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=3to run three instances of yourapiservice.
- Example:
Example/Code
Assume you have the full-stack docker-compose.yml from the previous lesson.
- 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 ```
- Check the status of services: See which containers are running.
bash
undefined
docker compose ps ```
- View logs from all services: Follow logs in real-time.
bash
undefined
docker compose logs -f ```
- Execute a command in the database container: Access the PostgreSQL client.
bash
undefined
docker compose exec database psql -U admin myappdb ```
- Scale the API service: Run two instances of your
apiservice.bashundefined
docker compose up -d --scale api=2 ```
- 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-dor--build) starts your application stack. -
docker compose stop,start,restart,downmanage the lifecycle of services. -
docker compose psanddocker compose logsare vital for monitoring. -
docker compose execallows running commands inside a service container. -
docker compose up --scaleenables basic horizontal scaling of services.