🧩 Background
While developing a backend project with NestJS + TypeORM + PostgreSQL, I used docker-compose
to spin up both the backend service and the database. Although I had successfully created tables and inserted data into the database, the NestJS app kept failing to connect—mysteriously attempting to authenticate as my Mac user, callustang
.
Here’s the error log:
[Nest] 75975 - ERROR [TypeOrmModule] Unable to connect to the database. Retrying...
Error: password authentication failed for user "callustang"
But my .env
configuration clearly specified:
DB_USER=root
And even in app.module.ts
, I logged the DB config:
console.log('DB Config:', {
host: config.get('DB_HOST'),
username: config.get('DB_USER'),
...
})
Everything looked correct, yet the connection kept failing. 🤯
🕵️ Investigation Path
The core question was: Where is the app actually running, and what database is it trying to connect to?
✅ I checked:
-
.env
hadDB_HOST=pgsqldb
-
docker-compose.yml
defined apgsqldb
service correctly - NestJS printed out
DB_HOST: 'pgsqldb'
- But the logs showed:
getaddrinfo ENOTFOUND pgsqldb
❗ The real issue:
The NestJS app was running locally on my Mac, not inside a Docker container.
Your local machine cannot resolve pgsqldb
, which is a hostname only available inside Docker’s internal network. Hence, the DB connection failed.
🔧 Solution 1: Run the NestJS App Inside Docker ✅
I created a proper development Dockerfile and ensured the container had essential tools like ps
(used by SWC for hot reload).
FROM node:20.14.0-bookworm-slim
RUN apt-get update && apt-get install -y \
vim curl wget git procps \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "start:dev"]
In docker-compose.yml
, I updated the app
service:
services:
app:
build:
context: .
ports:
- "4000:4000"
volumes:
- .:/usr/src/app
env_file:
- .env
depends_on:
- pgsqldb
networks:
- app-network
Then I rebuilt and started everything:
docker compose down -v
docker compose build --no-cache
docker compose up
Boom! NestJS successfully connected to PostgreSQL inside the container.
🔧 Solution 2: Still want to run NestJS locally?
Then you must change .env
to use:
DB_HOST=localhost
That way, your Mac connects to the containerized PostgreSQL via port mapping.
Ensure your pgsqldb
service has:
ports:
- 5432:5432
This works but is not recommended for long-term use, especially in team projects or CI/CD pipelines.
❓ What about /bin/sh: ps: not found
?
When running npm run start:dev
, I saw this error:
Error: Command failed: ps -A -o pid,ppid
/bin/sh: 1: ps: not found
Turns out the Docker image I used (node:bookworm-slim
) doesn't include the ps
utility. The solution is simple:
RUN apt-get update && apt-get install -y procps
SWC, Nest CLI, and some TypeScript checkers rely on ps
to inspect running processes. It’s not critical, but removing the error makes development smoother.
🔒 Should I install procps
in production?
No.
In production, your command is simply:
"start:prod": "node dist/src/main"
This does not need ps
, SWC
, or any hot-reloading tools. Keep your prod images lean and secure.
I used a multi-stage Docker build:
# Stage 1: Build
FROM node:20.14.0-bookworm-slim as builder
...
RUN npm run build && npm prune --production
# Stage 2: Runtime
FROM node:20.14.0-bookworm-slim
...
USER node
CMD ["npm", "run", "start:prod"]
🚫 No ps
needed. No bloat.
📌 Summary & Lessons Learned
Problem | Cause | Solution |
---|---|---|
DB connection fails as callustang
|
App is running on the host and defaulting to Mac user | Run the app inside Docker |
ENOTFOUND pgsqldb |
Local machine can't resolve Docker hostname | Either switch to Docker or use localhost
|
/bin/sh: ps: not found |
Slim image lacks procps
|
Add apt-get install procps in Dockerfile |
Should prod image include ps ? |
No | Keep it minimal and secure |
💬 Have You Run Into This?
Let me know in the comments if you’ve hit similar container vs. host confusion with NestJS, or if you’ve debugged weird Docker DNS issues. If this helped, give it a ❤️, bookmark it, or share it with someone struggling with dev containers!
Top comments (0)