Dockerfile & Docker Compose Generator · Tutorial

How to run a Node.js app and PostgreSQL with Docker Compose

10 min readintermediateUpdated

You want everyone on the team to start the app and its database with one command. You will generate a Dockerfile for a Node.js app, a .dockerignore and a compose.yaml with PostgreSQL, then run it.

What you need

  • The Dockerfile Generator
  • Docker Desktop or Docker Engine with Compose
  • A Node.js app with a package-lock.json

Steps

  1. Describe the app

    Load the example: Node.js 24, port 3000, start command node dist/server.js, a build step and a health check at /health. Change the start command to however your app starts.

  2. Read the Dockerfile

    The build stage installs all dependencies, runs npm run build and removes dev dependencies. The final stage copies the result into a clean image and runs it as the node user.

  3. Add PostgreSQL

    PostgreSQL is ticked in the example. compose.yaml now has a db service with a health check, a volume for its data and DATABASE_URL set on the app.

    environment:
      DATABASE_URL: postgres://app:change-me@db:5432/app
  4. Save the three files

    Download each file into the project root. Make sure your app reads DATABASE_URL and listens on 0.0.0.0, not localhost, or it won’t be reachable from outside the container.

  5. Start everything

    Run docker compose up --build. Compose starts the database, waits for it to be healthy, then starts the app on http://localhost:3000.

Try it yourself

Opens the Dockerfile & Docker Compose Generator with this tutorial's sample loaded.

Try it now

Common mistakes

The app listens on localhost only
Inside a container, localhost means the container itself. Bind to 0.0.0.0 so port mapping works.
Missing lock file
npm ci needs package-lock.json. Commit it, or the build stops.
Losing the database
docker compose down -v deletes volumes, including your data. Use plain down to keep it.

Next steps

Go back to the Dockerfile & Docker Compose Generator, or keep going with one of these:

FAQ

Can I connect to the database from my computer?

Add ports: ["5432:5432"] to the db service. Leave it out in production.