I know, here a lot of similar question and I read a lot of that, but I still can’t resolve that issue.
So I have traditional docker-compose config with: posgresql, pgAdmin, nest.js app.
I run all services in docker compose, under docker network.
Db and pgAdmin is successful works, I may to connect from pgAdmin to postgres using container name ‘db’.
But when I try to connect to my db from my nest application by TypeOrm, I was take that error:
Error: getaddrinfo ENOTFOUND db at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26)
When I start backend outside docker and connect by postgres container ip, all connected,
but in docker, container name / networkName / container ip not working, only one connection was without that error is with host:host.docker.internal
, but then was ssl connection error, I was added url to connect pg 'postgresql://postgres:password@host.docker.internal:5435/postgres'
but then was again same connection error.
docker-compose.yml
version: '3.5'
services:
db:
image: postgres
env_file:
- development.env
environment:
PGDATA: /data/postgres
volumes:
- db:/data/postgres
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
ports:
- "5432:5432"
networks:
- postgresNet
restart: unless-stopped
pgAdmin:
image: dpage/pgadmin4
restart: unless-stopped
env_file:
- development.env
volumes:
- ./pgadmin/servers.json:/pgadmin4/servers.json
- ./pgadmin/storage:/var/lib/pgadmin/storage
ports:
- "5050:80"
networks:
- postgresNet
command: ["/bin/sh", "-c", "python /pgadmin4/setup.py --load-servers /pgadmin4/servers.json --user ${PGADMIN_DEFAULT_EMAIL} --password ${PGADMIN_DEFAULT_PASSWORD}"]
backend:
build:
context: .
args:
DATABASE_URL: 'postgresql://postgres:password@db:5435/postgres'
POSTGRES_PORT: 5432
POSTGRES_HOST: 'db'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'password'
POSTGRES_DB: 'postgres'
ports:
- "3000:3000"
depends_on:
- db
networks:
- postgresNet
networks:
postgresNet:
driver: bridge
volumes:
db:
pgadmin:
OrmConfig.ts
const configOrm: DataSourceOptions = {
type: 'postgres',
host: 'db',
port: 5432,
username: 'postgres',
password: 'password',
database: 'postgres',
logging: true,
synchronize: false,
entities: [`${__dirname}/**/*.entity.{ts,js}`],
migrations: [getMigrationDirectory()],
subscribers: ['dist/subscriber/**/*.{ts,js}'],
ssl: (isProd || isStage) && {
rejectUnauthorized: false,
},
};