study.

Run Services with Compose · Docker

Describe the application’s services and configuration in one file.

← Docker

Learn with progress and quizzes

First, understand the concepts.

Compose model

services describes containers; the project name groups their resources. Inspect the resolved model with config before starting it.

Service-name connectivity

A service name becomes a DNS name on the default Compose network. The client reaches web:80 without a published host port.

Started is not ready

A started container may not yet be ready. This example retries HTTP a limited number of times; production setups should also consider health checks and dependency conditions.

Why it matters

A Compose file declares services as one application. Service names, project names, and generated container names are related but distinct.

Work through the reasoning

Read and apply the model

Inspect the combined configuration with config and reconcile services with up. Services on a project’s default network can use service names instead of hard-coded container IPs.

Separate start from readiness

Startup ordering alone does not mean a database accepts requests. Distinguish healthchecks and service_healthy dependencies from application-level retries.

Learn by doing.

Use Docker Engine or Docker Desktop with Linux containers, Compose v2, a POSIX shell, and curl on a personal practice host. Clean up only the study-prefixed resources you create. Pulling images requires network access.

Write a Compose file in a temporary directory, start the server, and run a one-shot client. Use -p study-compose to separate these resources from other projects.

study_compose_dir=$(mktemp -d)
cd "$study_compose_dir"
cat > compose.yaml <<'YAML'
services:
  web:
    image: nginx:stable-alpine
  client:
    image: busybox:1.36
    command: ["sh", "-c", "for i in 1 2 3 4 5; do wget -qO- http://web && exit 0; sleep 1; done; exit 1"]
YAML
docker compose -p study-compose config
docker compose -p study-compose up -d web
docker compose -p study-compose run --rm client
docker compose -p study-compose logs --tail=10 web

How to check your result

Expect nginx HTML from the client and an HTTP request in web logs. config validates configuration rather than starting services. Run down from the same directory with the same project name.

Clean up practice resources

Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.

docker compose -p study-compose down

Apply what you learned

The web service starts after the database but exits on a connection failure. What should you consider instead of increasing an arbitrary sleep?

Reveal the reasoning after trying

Consider a meaningful database healthcheck, readiness dependency, and bounded application retries. The app must also tolerate later database restarts; initial ordering alone does not solve availability.

Ready to check your understanding?

Which hostname reaches the web service from client on the default Compose network?

  1. web
  2. Dockerfile
  3. localhost always means web
  4. The absolute project directory path
Reveal the reasoning after trying

web

Services on the same Compose network are discoverable by service name. localhost inside client refers to client itself.

Official documentation · Run Services with Compose · Further reading · official documentation