Linux & Docker · Lesson 7 of 7

Docker Compose, volumes & deployment

Running an app with a database using Compose, persisting data with volumes and environment variables.

Real apps need several containers — an API, a database, maybe a cache. Docker Compose describes them in one compose.yaml file and starts them together with docker compose up.

  • Services on the same Compose network reach each other by service name (the API connects to host db).
  • Container files disappear when the container is removed, so databases need a volume to keep data.
  • Pass configuration as environment variables, reading secrets from a .env file that is never committed.

For production, images are pushed to a registry (Docker Hub, Amazon ECR, GitHub Container Registry) and run on a server, a cloud container service or Kubernetes.

Example in the terminal

Run these on your own computer
$ cat compose.yaml
services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/jobs
    depends_on:
      - db
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: jobs
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

$ docker compose up -d --build
 ✔ Network job-api_default  Created
 ✔ Volume "job-api_pgdata"  Created
 ✔ Container job-api-db-1   Started
 ✔ Container job-api-api-1  Started

$ docker compose ps
NAME            SERVICE  STATUS   PORTS
job-api-api-1   api      running  0.0.0.0:3000->3000/tcp
job-api-db-1    db       running  5432/tcp

$ docker compose down        # data stays in the pgdata volume
$ docker compose down -v     # also deletes the volume (data lost)

Lines starting with $ are commands you type; the rest is what the terminal prints. Use Linux, macOS, WSL on Windows or a free cloud shell. Names and paths are examples.

Key points

  • Compose runs multi-container apps from one YAML file.
  • Services reach each other by service name.
  • Volumes keep database data when containers are recreated.
  • Keep secrets in a .env file that is not committed.

Quick check

Your API container needs to connect to the db service in the same Compose file. Which host name does it use?

Practice task

Write a compose.yaml that runs your app with Redis or PostgreSQL, start it, stop it with docker compose down and confirm the data is still there when you start it again.

Interview corner

What is Docker Compose used for?

Defining and running multi-container applications (app, database, cache) from one YAML file with their networks, volumes and environment variables. It's mainly used for local development, testing and simple single-server deployments.

How do you persist data in Docker?

Use named volumes (managed by Docker) or bind mounts (a host folder) mounted into the container's data path, so data survives when containers are removed or recreated.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.