This guide helps you set up a local development environment using Docker, PostgreSQL, and your project image.
Local Development Environment with Docker, PostgreSQL, and Node.js (TypeScript) This project sets up a complete local development environment using Docker, PostgreSQL, and a Node.js application written in TypeScript. The goal is to provide an isolated, reproducible, and scalable setup where each component runs in its own container.
⚙️ Architecture Overview PostgreSQL Database runs inside one Docker container.
Node.js Application runs inside a separate Docker container.
Both containers are connected using a custom Docker network (user_project) to enable communication between them.
📡 Why Use a Docker Network? By default, Docker containers are isolated from each other. To allow communication:
We create a custom Docker network using docker network create user_project.
Both the Node.js app and PostgreSQL containers are attached to this network.
This allows the Node.js app to connect to the database using the container name (postgres) as the hostname.
- Docker must be installed and running.
- Basic understanding of terminal commands and Docker is helpful.
If you don't have Docker installed, follow the official installation guide:
Docker networks allow containers to communicate with each other by name (hostname).
docker network create user_project
This creates an isolated network named user_project
.
This command runs a PostgreSQL instance in a Docker container and connects it to your custom network.
docker run --network user_project --name postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-d -p 5432:5432 postgres
--network user_project
: connects it to your network.--name postgres
: gives the container a name.-e POSTGRES_PASSWORD=...
: sets the DB password.-d
: runs in detached mode.-p 5432:5432
: maps the container port to host.
Make sure your project has a Dockerfile
in the root directory.
docker build --network=host -t user-project .
--network=host
: useful if your build needs internet access.-t user-project
: tags the image with the nameuser-project
..
: current directory (where the Dockerfile is located).
Finally, run your application and link it to the PostgreSQL database.
docker run \
-e DATABASE_URL=postgresql://postgres:mysecretpassword@postgres:5432/postgres \
--network user_project \
-p 3000:3000 user-project
DATABASE_URL
: environment variable your app uses to connect to PostgreSQL.--network user_project
: ensures the app can resolvepostgres
as the DB host.-p 3000:3000
: maps port 3000 of the container to your local machine.
Once everything is up:
- Your PostgreSQL DB is running at
localhost:5432
- Your app is available at http://localhost:3000
- Use
.env
files to avoid hardcoding secrets like passwords. - For production, consider
docker-compose
for managing multi-container setups. - You can add Prisma migrations like this (after containers are running):
npx prisma migrate dev --name init
project-root/
├── Dockerfile
├── prisma/
│ └── schema.prisma
├── node_modules/
├── package.json
└── ...
Feel free to fork or open issues to improve this guide.
This project provides a clean and simple boilerplate for running a Node.js application with PostgreSQL using Docker. It's great for development and testing environments where containerized databases and services are preferred.
- Dockerized PostgreSQL setup
- Custom Docker network support
- Environment variable-based configuration
- Easy setup and teardown
You can name your repository something descriptive and relevant. Here are a few suggestions:
docker-node-postgres-starter
dev-env-docker-postgres
containerized-node-app
node-postgres-docker-boilerplate
dockerized-dev-setup
Choose a name that reflects your project's purpose and is easy to understand for other developers.
Contributions are welcome! Feel free to open issues or submit pull requests to improve the guide or add features.
This project is open source and available under the MIT License.
This project demonstrates how to connect a Node.js application (written in TypeScript) to a PostgreSQL database using Docker containers.
- PostgreSQL runs in one Docker container.
- Node.js app runs in a separate Docker container.
- Both containers communicate through a shared Docker network (
user_project
).
When you run services in separate containers, they are isolated by default. Using a Docker network allows the containers to communicate with each other by name. In this case, the Node.js app uses the hostname postgres
to connect to the PostgreSQL container.
Make sure to use the correct DATABASE_URL
environment variable in your app. Example using pg
library:
import { Client } from 'pg';
const client = new Client({
connectionString: process.env.DATABASE_URL, // e.g. 'postgresql://postgres:mysecretpassword@postgres:5432/postgres'
});
client.connect()
.then(() => console.log('Connected to PostgreSQL successfully!'))
.catch(err => console.error('Connection error', err.stack));
If you're using Prisma with TypeScript, your .env
file should look like:
DATABASE_URL="postgresql://postgres:mysecretpassword@postgres:5432/postgres"
Then run:
npx prisma generate
npx prisma migrate dev --name init
- Fully isolated environments for services.
- Easy to scale or swap out containers.
- Portable and replicable development environments.