A local environment with Docker Compose

Intermediate · 12 min

Start Taskboard's Postgres and file storage with one command using Docker Compose, and learn the three mistakes that make a local database unreachable.

Before thisShared types and code between client and server

Ben Awad · 12 min

A local environment with Docker Compose

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Ben AwadReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A Compose file describes your local services, such as Postgres and file storage, so a new teammate gets the same setup with one command.
  • Run the databases and storage in containers, and run your own app code on the host, so you keep fast reloads and normal debugging.
  • A named volume keeps the data when the container is removed, and removing the volume is how you reset to empty.
  • A healthcheck tells you when the database accepts connections, because a started container is not yet a ready database.
  • From your laptop you reach a service at localhost and the published port, while from another container you reach it by service name.

The “works on my machine” tax

Taskboard needs Postgres, and soon it needs somewhere to put uploaded files. The slow way to give a new teammate those is a wiki page: install this version, create this user, run this command. Steps get skipped. Versions drift. Someone has Postgres 14 and someone has 17, and a query behaves differently.

Docker Compose replaces the wiki page with a file. You describe the services your app needs, and one command starts all of them.

A caution before the details: I could not run Docker in the environment where I wrote this lesson. I checked that the file below parses as valid YAML and that its structure matches Compose’s documented format, but I did not start the containers. Where I describe behaviour, it comes from the Compose and Postgres image documentation as I understand it, not from watching it run. Try it yourself and trust your terminal over this page.

Taskboard’s compose file

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: taskboard
      POSTGRES_PASSWORD: taskboard
      POSTGRES_DB: taskboard
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U taskboard -d taskboard"]
      interval: 2s
      timeout: 3s
      retries: 15

  storage:
    image: minio/minio
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: dev
      MINIO_ROOT_PASSWORD: devdevdev
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio:/data

volumes:
  pgdata:
  minio:

Two services. db is Postgres. storage is MinIO, an object store that speaks the same protocol as Amazon S3, so the upload code you write in file uploads from browser to storage and back works unchanged against it and against real S3 later. Bring them up with docker compose up -d, and stop them with docker compose down.

What each part is for

image: postgres:17. Pin the major version. postgres:latest means the version changes under you the next time someone pulls.

environment. The Postgres image reads these on first start to create a user and a database. These are development credentials, and they live in a file in the repo on purpose: nothing real is behind them. Production credentials never go in a file like this. That comes up again in environment variables and secrets across layers.

ports: "5432:5432". The format is host:container. This publishes the container’s port 5432 on your machine’s port 5432. If you already run Postgres locally on 5432, change the left side to "5433:5432" and use 5433 in your connection string.

volumes: pgdata:.... A named volume is storage Docker manages outside the container. The database’s files live there. Take the container down and bring it back, and your data is still there. To start over with an empty database, remove the volume as well with docker compose down -v. Learn that flag early. It is your “reset” button, and it is destructive.

healthcheck. A container being started only means its process launched. Postgres needs a moment to initialise before it accepts connections. pg_isready is a small tool in the image that reports whether it does. With a healthcheck, docker compose ps shows healthy when the database is ready, and other services can wait for it with depends_on and condition: service_healthy.

Run the app on the host, not in the compose file

You could put the API and the client in containers too. For daily development, don’t. Keep Compose for the things you don’t want to install: the database and the storage. Run your own code on the host with npm run dev:

  • Reloads on save are faster.
  • Your debugger and editor work normally.
  • You aren’t rebuilding images every time a dependency changes.

You will containerise the API later for deployment, which the backend roadmap covers in containerizing a backend service. Development doesn’t need it.

The three connection mistakes

Most “I can’t connect to the database” problems are one of these.

1. Wrong host. Where is your code running when it connects?

Your code runs… Connect to Why
On your laptop localhost:5432 You use the published port
In another container on the Compose network db:5432 The service name is the host name, and the container port is used

The same service has two addresses depending on where you stand. Mixing them is the commonest mistake.

2. Not waiting. The API starts before Postgres is ready and crashes with a connection error. Use the healthcheck, or make the API retry its first connection a few times.

3. Stale data after a schema change. You change the POSTGRES_PASSWORD or the initial setup, run up again, and nothing changes. The image only runs its first-time setup when the data directory is empty, and your volume isn’t. Remove the volume to re-initialise.

The resulting connection string for your host-run API is:

DATABASE_URL=postgres://taskboard:taskboard@localhost:5432/taskboard

What goes in the repo

Commit compose.yaml. Add a short README section: docker compose up -d, then the migration command, then the seed command from the next lessons. Don’t commit anything with real secrets. A teammate should reach a running database in about three commands, and if they can’t, the setup is not done.

I ran the database layer of Taskboard against PGlite (a Postgres build that runs in-process, version 0.5.17) while writing this, because Docker wasn’t available. For schema and query behaviour that is a faithful stand-in, but it is not a substitute for testing against the same Postgres version you deploy, which is exactly what the Compose file is for.

What the video covers

Ben Awad’s “Docker Compose Tutorial with PostgreSQL and Node.js” is about twelve minutes and was published in October 2018. Its description says it creates a PostgreSQL container and a Node.js container with Docker Compose and links the code. It is old. Compose has changed since: the standalone docker-compose command became docker compose, and the top-level version: key you may see in older files is no longer needed. The core idea, which is services in a YAML file connected by a network, is the same. I could not read a transcript, so I can’t say how the video handles the host-versus-service-name point above. codeonedigest’s 2025 video on the same pairing is linked as a more recent second view.

Where this fits

You have a database and storage running from one command. They’re empty, and the app needs configuration to find them. Next: environment variables and secrets across layers.

Check yourself

  1. 01Why run Postgres in a container instead of installing it on your machine?

    Everyone gets the same version and settings from one file, and you can throw it away and recreate it without touching your system.

  2. 02What does a named volume do for the database?

    It stores the data outside the container, so the data survives the container being removed and recreated. Deleting the volume gives you a clean database.

  3. 03Why is a container that has started not necessarily ready?

    The process is running but the database may still be initialising, so connections fail for a few seconds. A healthcheck reports when it actually accepts them.

  4. 04Your API runs on the host and Postgres is in Compose. What host name goes in DATABASE_URL?

    localhost, with the host port Compose published. The service name only resolves from inside other containers on the Compose network.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark