How to prepare Postgresql with Nestjs and Docker for Local Development: Fast Directory

Launching a new project and needs postgres to develop Nestjs, but don’t want to adhere to the DB production provider (so far)? Docker’s local postgress operation is your best friend. basic. trusted. No system chaos.
Below, I will walk with you with my setting for Nestjs and posgresql using Docker – minimal friction, textable, always cloning. You will get practical composition, orders to directly access the container, and to configure the Nestjs database.
Why this setting?
Early development is everything related to moving quickly: changing the plans, reset data, running the deportation, and sometimes on the same day. The cloud databases (like neon) is a great final destination, but for local pirates and testing, Docker wins each time. It keeps postgres outside your host device, and avoids “business on my device”. This is the real connection and operation of the local Dev.
Project structure and required files
This is what we will prepare:
- Dockerfile For the Nestjs app
- Docker-Corm.yml To connect the knot and postgres
- Env A file for environmental variables
- A sample of composition and text programs Nestjs
- Practical orders for joint work
Dockerfile: A simple knot environment
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "start:dev"]
Docker-CORM.YML: Node + Postgres alongside
This is the magic sauce that escapes from your node applications and for example posGRES can be eliminated together.
version: "3.8"
services:
db:
image: postgres:13
restart: always
env_file:
- .env
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
api:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
depends_on:
- db
env_file:
- .env
command: sh -c "npm run migration:run && npm run start:dev"
volumes:
db-data:
Advice: The folder key allows your database to survive without losing data.
Env
construction .env
File on your project root:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=changeme
POSTGRES_DB=app_db
POSTGRES_HOST=db
POSTGRES_PORT=5432
PORT=3000
Keep your secrets from Git! .env
Enter .gitignore
.
Package.json Scrips: Interactive containers
Why do you remember the container’s identifiers? Add this to your own package.json
Fast access programs:
"scripts": {
"db": "docker exec -it $(docker-compose ps -q db) bash",
"api": "docker exec -it $(docker-compose ps -q api) bash"
}
Now, just run npm run db
For a database shell, or npm run api
For application.
Nestjs: Contact your restricted database
In your main start (for example main.ts
):
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT);
}
bootstrap();
Database composition:
Here is a common formation file for Typeorm:
const config = {
type: "postgres",
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT, 10),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
entities: [__dirname + "/**/*.entity{.ts,.js}"],
synchronize: false, // safer for non-prod
migrations: [__dirname + "/migrations/**/*{.ts,.js}"],
autoLoadEntities: true,
};
Development workflow: daily orders
- Start everything:
docker-compose up --build
(The first time) or onlydocker-compose up
- Records view:
docker-compose logs -f api
- Show it (container removal):
docker-compose down
- Jumped in a DB shell:
npm run db
- Jump in the application container:
npm run api