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
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.Read the Dockerfile
The build stage installs all dependencies, runs
npm run buildand removes dev dependencies. The final stage copies the result into a clean image and runs it as thenodeuser.Add PostgreSQL
PostgreSQL is ticked in the example. compose.yaml now has a
dbservice with a health check, a volume for its data andDATABASE_URLset on the app.environment: DATABASE_URL: postgres://app:change-me@db:5432/appSave the three files
Download each file into the project root. Make sure your app reads
DATABASE_URLand listens on0.0.0.0, notlocalhost, or it won’t be reachable from outside the container.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.
Common mistakes
- The app listens on localhost only
- Inside a container, localhost means the container itself. Bind to
0.0.0.0so port mapping works. - Missing lock file
npm cineeds package-lock.json. Commit it, or the build stops.- Losing the database
docker compose down -vdeletes volumes, including your data. Use plaindownto 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.