Docker Compose — Services

less than 1 minute read

Welcome to Chapter 4 — the heart of Docker Compose. Services are the core building block — each service defines a container that Compose manages.


Topics in This Chapter

# Topic Description
1 Defining Services Service structure, image, container_name, restart
2 Image vs Build Using pre-built images vs building from Dockerfile
3 Ports and Expose Publishing ports and internal expose
4 Command and Entrypoint Overriding default commands
5 Service DNS Naming How services discover each other

What You’ll Learn

  • How to define services with all common options
  • When to use image: vs build:
  • Port mapping in short and long form
  • How to override container commands
  • How service-to-service DNS works

Key Concept

Each service in your compose file becomes a running container with a DNS name:

┌─────────── compose.yml ───────────┐
│                                    │
│  services:                         │
│    web:      ──→ Container "web"   │
│    api:      ──→ Container "api"   │
│    db:       ──→ Container "db"    │
│                                    │
│  DNS Resolution:                   │
│    web → 172.18.0.2                │
│    api → 172.18.0.3                │
│    db  → 172.18.0.4                │
└────────────────────────────────────┘