Guide Docker Beginner

10. Docker Compose

Describe and manage a local application using a Compose file.

1 min read

What Is Compose?

Compose describes services, networks, and volumes in YAML. It gives a project a repeatable configuration instead of requiring a long collection of manually typed run commands.

Create compose.yaml in an empty directory:

services:
  web:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8083:80"

Validate and Start

docker compose -p docker-course config
docker compose -p docker-course up -d
docker compose -p docker-course ps
docker compose -p docker-course logs web

Open http://localhost:8083. Run the commands from the directory containing compose.yaml.

Stop the Project

docker compose -p docker-course down

This removes the project’s containers and default network. Named volumes are retained unless explicitly requested for removal.

Startup vs Readiness

A running process may not yet be ready to serve requests. For dependencies, define health checks and use appropriate dependency conditions. Applications should also tolerate temporary connection failures.

Practice

Change the host port to 8084 and apply up -d again. Verify the new published port with ps, then tear down the practice project.

Quick Interview Answer

Compose declares an application’s services and related resources. Up reconciles the project with that configuration, while down removes its containers and networks.

Reference: Docker Compose quickstart.


Previous: 9. Volumes and Bind Mounts | Next: 11. Logs, Health, and Resources

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form