Compose Service — One Component of Your Application Stack
What Is a Compose Service in Simple Terms?
In Docker Compose, a service is one named component of your application. Your application might have three services: api, postgres, and redis. Each service definition tells Docker exactly how to run that component — which image to use, which ports to expose, which environment variables to set, which volumes to mount, and which other services it depends on.
YAML
version: "3.8" services: payment-api: # <- service name (also DNS hostname) image: payment-api:v3.1.0 ports: * "8080:8080" environment: DB_HOST: postgres # reaches postgres service by name depends_on: postgres: condition: service_healthy restart: unless-stopped deploy: resources: limits: cpus: "1.5" memory: 512M healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"] interval: 30s timeout: 5s retries: 3 postgres: image: postgres:15-alpine environment: POSTGRES_PASSWORD: secret volumes: * postgres-data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s retries: 5 volumes: postgres-data:All Service Configuration Keys
YAML
services: myservice: image: myimage:tag # image to use build: . # or build from Dockerfile container_name: myservice # fixed container name ports: * "8080:80" environment: KEY: value env_file: * .env volumes: * data:/app/data * ./config:/app/config:ro networks: * mynetwork depends_on: other: condition: service_healthy restart: unless-stopped command: ["node", "server.js"] # override CMD entrypoint: ["/entrypoint.sh"] # override ENTRYPOINT working_dir: /app user: "1000:1000" read_only: true tmpfs: * /tmp logging: driver: json-file options: max-size: "100m" deploy: resources: limits: cpus: "1" memory: 512M profiles: * debug # only starts when debug profile activeService Scaling
Bash
# Scale a service to 3 instancesdocker compose up -d --scale payment-api=3 # IMPORTANT: remove container_name before scaling# A fixed container_name conflicts when scaling to multiple instances # Check which instances are runningdocker compose ps# NAME STATUS# project-api-1 Up# project-api-2 Up# project-api-3 UpPLACEMENT PRO TIP**Tip:** The service name in Docker Compose is also the DNS hostname other services use to reach it. Name your services after what they do (`api`, `postgres`, `redis`) not after the image they use. This makes your application config readable and easy to understand.
COMMON MISTAKE / WARNING**Common Mistake:** Setting `container_name` on services you want to scale. Docker Compose cannot create two containers with the same name — scaling a service with a fixed `container_name` fails immediately.