При запуске сервера из папки через
npm run dev
, он нормально подключается к БД из докер кластера
Но запуская контейнер в кластере или отдельно, он выдает ошибку
connect ECONNREFUSED 127.0.0.1:5432
при запросе к БД
Подключение к БД
const config = {
database: 'postgres',
user: 'postgres',
password: 'postgres',
port: 5432,
max: 20,
idleTimeoutMillis: 1000,
connectionTimeoutMillis: 1000,
maxUses: 7500,
}
Сборка образа
rest-api-server
FROM node:20
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
CMD ["npm", "start"]
version: '3.5'
services:
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
PGDATA: /data/postgres
volumes:
- ./postgres/setup.sql:/docker-entrypoint-initdb.d/setup.sql
- postgres:/data/postgres
ports:
- 5432:5432
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 1s
timeout: 1s
retries: 60
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: admin@mymail.ru
PGADMIN_DEFAULT_PASSWORD: adminpassword
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- 5480:80
networks:
- postgres
restart: unless-stopped
nginx:
container_name: nginx_container
image: nginx
ports:
- 80:80
- 443:443
networks:
- postgres
restart: unless-stopped
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d/:/etc/nginx/conf.d/:ro
server:
container_name: server_container
image: rest-api-server
ports:
- 3000:3000
networks:
- postgres
restart: unless-stopped
depends_on:
- postgres
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin: