Docker compose with NestJS PostgreSQL not connecting - postgresql

I have tried to dockerize NestJS app with PostgreSQL. Postgres is refusing connection and is also showing one log that says database system was shut down at <<some timestamp>>. This is docker-compose.yml and the logs.
version: '3'
services:
postgres:
image: postgres
restart: always
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- '5432:5432'
environment:
- POSTGRES_DB=gm
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
pgadmin:
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=admin#gmail.com
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=5050
ports:
- "5050:5050"
api:
image: gm-server
build:
dockerfile: Dockerfile
context: .
volumes:
- .:/home/node
ports:
- '8081:4001'
depends_on:
- postgres
env_file: .env
command: npm run start:prod
volumes:
pgdata:
server-postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
server-postgres-1 |
server-postgres-1 | 2023-01-04 09:36:45.249 UTC [1] LOG: starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
server-postgres-1 | 2023-01-04 09:36:45.250 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
server-postgres-1 | 2023-01-04 09:36:45.250 UTC [1] LOG: listening on IPv6 address "::", port 5432
server-postgres-1 | 2023-01-04 09:36:45.255 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
server-postgres-1 | 2023-01-04 09:36:45.261 UTC [29] LOG: database system was shut down at 2023-01-04 09:36:27 UTC
server-postgres-1 | 2023-01-04 09:36:45.274 UTC [1] LOG: database system is ready to accept connections
server-api-1 |
server-api-1 | > nestjs-app#0.0.1 start:prod
server-api-1 | > node dist/main
server-api-1 |
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [NestFactory] Starting Nest application...
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [InstanceLoader] MulterModule dependencies initialized +61ms
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [InstanceLoader] MulterModule dependencies initialized +1ms
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [InstanceLoader] AppModule dependencies initialized +1ms
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
server-api-1 | [Nest] 19 - 01/04/2023, 9:36:47 AM ERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5432
server-api-1 | Error: connect ECONNREFUSED 127.0.0.1:5432
server-api-1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)
server-api-1 exited with code 1
And I have also tried most of the relevant answers (before Stackoverlow stars marking me as duplicate) and they didn't work. Yes, I have tried changing the host to host.docker.internal as suggested by the previous
For more clarity, here is my typeorm-datasource config in NestJS
import { DataSource } from 'typeorm';
export const typeOrmConnectionDataSource = new DataSource({
type: 'postgres',
host: 'host.docker.internal',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'gm',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
logging: true,
synchronize: false,
migrationsRun: false,
});
Why this problem is different than the other "duplicate" questions?
The reason this problem is different is due to the reason because
the other threads also don't solve the issue.
even if we consider they do, the solutions didn't work for me.
More evidence?
I have tried all solutions for this one
Another solution with didn't work.
Another one, even followed the chat

Apparently the problem lies in NestJS not compiling properly because of my customization to its scripts. Check if that's an issue.
After fixing this issue, Just follow the instructions to use "postgres" as host, and it will work (if you are facing the same issue).

Related

Airflow in docker: Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries

I was following this example and I ran into the error after docker-compose up
airflow-airflow_scheduler-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow_scheduler-1 exited with code 1
airflow-airflow-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow_worker-1 | Error executing 'postInstallation': Failed to connect to postgres:5432 after 36 tries
airflow-airflow-1 exited with code 1
airflow-airflow_worker-1 exited with code 1
My docker-compose.yml file looks like this:
# SPDX-License-Identifier: Apache-2.0
version: "3.7"
services:
airflow:
image: bitnami/airflow:1.10.13
ports:
- "8080:8080"
env_file:
- openlineage.env
environment:
- AIRFLOW_USERNAME=airflow
- AIRFLOW_PASSWORD=airflow
- AIRFLOW_EMAIL=airflow#example.com
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
airflow_scheduler:
image: bitnami/airflow-scheduler:1.10.13
env_file:
- openlineage.env
environment:
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
airflow_worker:
image: bitnami/airflow-worker:1.10.13
env_file:
- openlineage.env
environment:
- AIRFLOW_FERNET_KEY=Z2uDm0ZL60fXNkEXG8LW99Ki2zf8wkmIltaTz1iQPDU=
- AIRFLOW_DATABASE_HOST=postgres
- AIRFLOW_DATABASE_NAME=airflow
- AIRFLOW_DATABASE_USERNAME=airflow
- AIRFLOW_DATABASE_PASSWORD=airflow
- AIRFLOW_EXECUTOR=CeleryExecutor
- AIRFLOW_LOAD_EXAMPLES=no
- AIRFLOW_CONN_EXAMPLE_DB=postgres://example:example#postgres:5432/example
volumes:
- ./dags:/opt/bitnami/airflow/dags
- ${PWD}/whl:/whl
- type: bind
source: ${PWD}/requirements.txt
target: /bitnami/python/requirements.txt
marquez:
image: marquezproject/marquez:latest
ports:
- "5000:5000"
- "5001:5001"
volumes:
- ./docker/wait-for-it.sh:/usr/src/app/wait-for-it.sh
depends_on:
- postgres
entrypoint: ["./wait-for-it.sh", "postgres:5432", "--", "./entrypoint.sh"]
# Enables SQL statement logging (see: https://www.postgresql.org/docs/12/runtime-config-logging.html#GUC-LOG-STATEMENT)
# command: ["postgres", "-c", "log_statement=all"]
marquez_web:
image: marquezproject/marquez-web:latest
environment:
- MARQUEZ_HOST=marquez
- MARQUEZ_PORT=5000
ports:
- "3000:3000"
stdin_open: true
tty: true
depends_on:
- marquez
postgres:
image: bitnami/postgresql:12.1.0
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- AIRFLOW_USER=airflow
- AIRFLOW_PASSWORD=airflow
- AIRFLOW_DB=airflow
- MARQUEZ_USER=marquez
- MARQUEZ_PASSWORD=marquez
- MARQUEZ_DB=marquez
- EXAMPLE_USER=example
- EXAMPLE_PASSWORD=example
- EXAMPLE_DB=example
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- ./docker/init-db.sh:/docker-entrypoint-initdb.d/init-db.sh
redis:
image: bitnami/redis:6.0.6
environment:
- ALLOW_EMPTY_PASSWORD=yes
What confuses me is that postgres seems to be ready according to airflow-postgres
airflow-postgres-1 | postgresql 05:58:32.34 INFO ==> Starting PostgreSQL in background...
airflow-postgres-1 | postgresql 05:58:32.46 INFO ==> Changing password of postgres
airflow-postgres-1 | postgresql 05:58:32.48 INFO ==> Configuring replication parameters
airflow-postgres-1 | postgresql 05:58:32.51 INFO ==> Configuring fsync
airflow-postgres-1 | postgresql 05:58:32.51 INFO ==> Loading custom scripts...
airflow-postgres-1 | postgresql 05:58:32.52 INFO ==> Loading user's custom files from /docker-entrypoint-initdb.d ...
airflow-postgres-1 | postgresql 05:58:32.52 INFO ==> Starting PostgreSQL in background...
airflow-postgres-1 | postgresql 05:58:32.82 INFO ==> Enabling remote connections
airflow-postgres-1 | postgresql 05:58:32.83 INFO ==> Stopping PostgreSQL...
airflow-postgres-1 | postgresql 05:58:33.84 INFO ==> ** PostgreSQL setup finished! **
airflow-postgres-1 |
airflow-postgres-1 | postgresql 05:58:33.90 INFO ==> ** Starting PostgreSQL **
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: starting PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
airflow-postgres-1 | 2022-06-08 05:58:33.924 GMT [1] LOG: listening on IPv6 address "::", port 5432
airflow-postgres-1 | 2022-06-08 05:58:33.931 GMT [1] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
airflow-postgres-1 | 2022-06-08 05:58:33.947 GMT [216] LOG: database system was shut down at 2022-06-08 05:58:32 GMT
airflow-postgres-1 | 2022-06-08 05:58:33.957 GMT [1] LOG: database system is ready to accept connections
But it seems like airflow-marquez simply can't connect to it:
airflow-marquez-1 | wait-for-it.sh: timeout occurred after waiting 15 seconds for postgres:5432
airflow-marquez-1 | WARNING 'MARQUEZ_CONFIG' not set, using development configuration.
I'm on Linux Manjaro.
I'm very new to docker and maybe I haven't included enough information for my problem, please point that out if it's true and I'll include more information.
Thanks in advance!

NestJS app build with docker can't access postgres database in the same docker network: ECONNREFUSED 127.0.0.1:5432

I want to run my app on my local machine within Docker. I don't want to optimize the size of my docker app or build it for production now.
Docker builds my backend-api app and postgres-db successfully. But I can only access the docker postgres database outside my docker e.g. with dbeaver installed on my computer. Also if I start my backend-api app WITHOUT Docker with "npm run start", my app can also access the database without any errors and can also write into the posgres db. Only when I build the backend-api with Docker and launch the app inside the Docker, I get this error. Since this only happens within Docker, I assume that something important is missing in my Dockerfile.
My docker-compose.yml:
version: '3'
services:
backend:
build: .
container_name: backend-api
command: npm run start
restart: unless-stopped
ports:
- 3000:3000
volumes:
- .:/usr/src/backend
networks:
- docker-network
depends_on:
- database
database:
image: postgres:latest
container_name: backend-db
ports:
- 5432:5432
volumes:
- postgresdb/:/var/lib/postgresql/data/
networks:
- docker-network
environment:
POSTGRES_USER: devuser
POSTGRES_PASSWORD: devpw
POSTGRES_DB: devdb
volumes:
postgresdb:
networks:
docker-network:
driver: bridge
My Dockerfile:
FROM node:16.15.0-alpine
WORKDIR /usr/src/backend
COPY . .
RUN npm install
sudo docker-compose up --build output:
Starting backend-db ... done
Recreating backend-api ... done
Attaching to backend-db, backend-api
backend-db |
backend-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
backend-db |
backend-db | 2022-05-03 20:35:46.065 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
backend-db | 2022-05-03 20:35:46.066 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
backend-db | 2022-05-03 20:35:46.066 UTC [1] LOG: listening on IPv6 address "::", port 5432
backend-db | 2022-05-03 20:35:46.067 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
backend-db | 2022-05-03 20:35:46.071 UTC [26] LOG: database system was shut down at 2022-05-03 20:35:02 UTC
backend-db | 2022-05-03 20:35:46.077 UTC [1] LOG: database system is ready to accept connections
backend-api |
backend-api | > backend#0.0.1 start
backend-api | > nodemon
backend-api |
backend-api | [nodemon] 2.0.15
backend-api | [nodemon] to restart at any time, enter `rs`
backend-api | [nodemon] watching path(s): src/**/*
backend-api | [nodemon] watching extensions: ts
backend-api | [nodemon] starting `IS_TS_NODE=true ts-node -r tsconfig-paths/register src/main.ts`
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [NestFactory] Starting Nest application...
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +73ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] AppModule dependencies initialized +0ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
backend-api | [Nest] 30 - 05/03/2022, 8:35:50 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
backend-api | Error: connect ECONNREFUSED 127.0.0.1:5432
backend-api | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
backend-api | [Nest] 30 - 05/03/2022, 8:35:53 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
My ormconfig.ts:
import { ConnectionOptions } from 'typeorm';
const config: ConnectionOptions = {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'devuser',
password: 'devpw',
database: 'devdb',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: false,
migrations: [__dirname + '/**/migrations/**/*{.ts,.js}'],
cli: {
migrationsDir: 'src/migrations',
},
};
export default config;
Please let me know what other information you would like me to provide. I'm new to the Docker world.
When you run a docker container localhost results in the container's localhost, not your machine's, so it's not the right host to use. As you're using docker-compose, a docker network is automatically created using the service's names as hosts. So instead of using localhost for you database host, you can use database as the host, and now the docker network will route the request properly to the database service as defined in your docker-compose.yml file
Ran into the same issue and this docker-compose.yml setup worked for me
version: "3.8"
services:
database:
container_name: db
image: postgres:14.2
ports:
- "5432:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD
backend:
container_name: api
build:
dockerfile: Dockerfile
context: .
restart: on-failure
depends_on:
- database
ports:
- "3000:3000"
environment:
- DATABASE_HOST=database
- DATABASE_PORT=5432
- DATABASE_USER=postgres
- DATABASE_PASSWORD
- DATABASE_NAME
- DATABASE_SYNCHRONIZE
- NODE_ENV
Following were two main notable changes
changed the host to database so the docker network will route the
request properly to the database service as defined in your
docker-compose.yml file.
added a user postgres as default
PostgreSQL user

Pgadmin container kills postgres when using docker-compose

I run postgres using docker-compose -f postgres.yml up. When I try to run pgadmin4 using docker-compose -f pgadmin4.yml up, postgres container is automatically killed.
I am new to docker and doesn't understand why is this issue happening?
postgres.yml
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ****
volumes:
- maple:/var/lib/postgresql/data
volumes:
maple:
name: maple
external: true
pgadmin.yml
version: "3.7"
services:
postgres:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: ******
PGADMIN_DEFAULT_PASSWORD: *****
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
pgadmin4-vol:
name: pgadmin4-vol
external: true
$ docker-compose -f psql.yml up
Starting postgres ... done
Attaching to postgres
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2020-11-27 10:18:05.635 UTC 1 LOG: starting PostgreSQL 13.0 (Debian 13.0-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres | 2020-11-27 10:18:05.636 UTC 1 LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2020-11-27 10:18:05.636 UTC 1 LOG: listening on IPv6 address "::", port 5432
postgres | 2020-11-27 10:18:05.639 UTC 1 LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2020-11-27 10:18:05.643 UTC [24] LOG: database system was shut down at 2020-11-27 10:17:59 UTC
postgres | 2020-11-27 10:18:05.647 UTC 1 LOG: database system is ready to accept connections
postgres | 2020-11-27 10:18:11.089 UTC 1 LOG: received fast shutdown request
postgres | 2020-11-27 10:18:11.092 UTC 1 LOG: aborting any active transactions
postgres | 2020-11-27 10:18:11.099 UTC 1 LOG: background worker "logical replication launcher" (PID 30) exited with exit code 1
postgres | 2020-11-27 10:18:11.108 UTC [25] LOG: shutting down
postgres | 2020-11-27 10:18:11.127 UTC 1 LOG: database system is shut down
postgres exited with code 0
$ docker-compose -f pgadmin.yml up
Recreating postgres ... done
Attaching to pgadmin4
pgadmin4 | sudo: setrlimit(RLIMIT_CORE): Operation not permitted
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Starting gunicorn 19.9.0
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Listening at: http://[::]:80 (1)
pgadmin4 | [2020-11-27 10:18:13 +0000] 1 [INFO] Using worker: threads
pgadmin4 | /usr/local/lib/python3.8/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
pgadmin4 | return io.open(fd, *args, **kwargs)
pgadmin4 | [2020-11-27 10:18:13 +0000] [87] [INFO] Booting worker with pid: 87
docker inpsect 69438bebc3f9 here
your services are both named postgres
so the pgadmin.yml recreates the service postgres, wich is your first database container.. as the logs are clearly showing
'Recreating postgres ... done'
take another name for the pgadmin service then postgres and it should work.
version: "3.7"
services:
pgadmin:
also docker-compose is for building stacks, if you would have done this in one compose file you would have seen the error from begin:
docker-compose.failing.yml:
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: test
volumes:
- maple:/var/lib/postgresql/data
postgres:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: test
PGADMIN_DEFAULT_PASSWORD: test
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
maple:
name: maple
external: true
pgadmin4-vol:
name: pgadmin4-vol
external: true
docker-compose.working.yml:
version: "3.7"
services:
postgres:
container_name: "postgres"
image: "postgres"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: test
volumes:
- maple:/var/lib/postgresql/data
pgadmin:
container_name: "pgadmin4"
image: "dpage/pgadmin4"
ports:
- "5433:80"
environment:
PGADMIN_DEFAULT_EMAIL: test
PGADMIN_DEFAULT_PASSWORD: test
volumes:
- pgadmin4-vol:/var/lib/pgadmin
volumes:
maple:
name: maple
external: true
pgadmin4-vol:
name: pgadmin4-vol
external: true

Not able to connect to Postgres database from golang Docker container

I am trying to connect to a Postgres database in a separate container from another separate container that stores a Go server:
Pool, err = pgxpool.Connect(context.Background(),"postgres://postgres:postgres#postgres:5432/postgres")
After doing so, I receive the following error:
2020/09/25 13:40:08 failed to connect to `host=postgres user=postgres database=postgres`: hostname resolving error (lookup postgres on 192.168.65.1:53: no such host)
Here is the docker-compose.yml:
version: "3.8"
services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
restart: always
server:
build: .
depends_on:
- postgres
ports:
- "2302:2302"
- "80:80"
restart: always
I am successfully able to connect to the Postgres database from my OS. Here are the Postgres container initialization logs:
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: starting PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-09-25 15:37:50.529 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-09-25 15:37:50.532 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-09-25 15:37:50.536 UTC [20] LOG: database system was shut down at 2020-09-25 15:37:49 UTC
postgres_1 | 2020-09-25 15:37:50.540 UTC [1] LOG: database system is ready to accept connections
Any clues would be helpful. Thank you in advance.
Try to share a network
version: "3.8"
services:
postgres:
image: postgres:alpine
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
restart: always
networks: [ "go_develop" ]
server:
build: .
depends_on:
- postgres
ports:
- "2302:2302"
- "80:80"
restart: always
networks: [ "go_develop" ]
networks:
go_develop:
driver: bridge

Postgres not accepting connection in Docker

I am running the docker project on docker toolbox windows 7 sp1 and the project does not give any error but still due to postgres not working the whole project in not functional.
The code of Docker compose file is :
version: '3'
services:
postgres:
image: 'postgres:latest'
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: "db"
POSTGRES_PASSWORD: postgres_password
POSTGRES_HOST_AUTH_METHOD: "trust"
DATABASE_URL: postgresql://postgres:p3wrd#postgres:5432/postgres
deploy:
restart_policy:
condition: on-failure
window: 15m
redis:
image: 'redis:latest'
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '3050:80'
api:
depends_on:
- "postgres"
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- ./server/copy:/usr/src/app/data
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
client:
depends_on:
- "postgres"
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- ./client/copy:/usr/src/app/data
- /usr/src/app/node_modules
worker:
build:
dockerfile: Dockerfile.dev
context: ./worker
volumes:
- ./worker/copy:/usr/src/app/data
- /usr/src/app/node_modules
depends_on:
- "postgres"
But when i run the project i get this :
redis_1 | 1:C 29 May 2020 05:07:37.909 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 29 May 2020 05:07:37.910 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 29 May 2020 05:07:37.911 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 29 May 2020 05:07:37.922 * Running mode=standalone, port=6379.
redis_1 | 1:M 29 May 2020 05:07:37.928 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 29 May 2020 05:07:37.929 # Server initialized
redis_1 | 1:M 29 May 2020 05:07:37.929 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 29 May 2020 05:07:37.933 * Loading RDB produced by version 6.0.3
redis_1 | 1:M 29 May 2020 05:07:37.934 * RDB age 8 seconds
redis_1 | 1:M 29 May 2020 05:07:37.934 * RDB memory usage when created 0.81 Mb
redis_1 | 1:M 29 May 2020 05:07:37.934 * DB loaded from disk: 0.001 seconds
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2020-05-29 05:07:38.927 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-05-29 05:07:38.928 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-05-29 05:07:38.929 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-05-29 05:07:38.933 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-05-29 05:07:38.993 UTC [24] LOG: database system was shut down at 2020-05-29 05:07:29 UTC
api_1 |
api_1 | > # dev /usr/src/app
api_1 | > nodemon
api_1 |
api_1 | [nodemon] 1.18.3
api_1 | [nodemon] to restart at any time, enter `rs`
api_1 | [nodemon] watching: *.*
With or without data volumes the same error comes and due to which the project is not running. Please Help
I would assume that once the API starts, it attempts to connect to the Postgres. If yes, this is a typical error that many Docker developers experience where the Postgres DB is not yet ready to accept connections and apps are trying to connect to it.
You can solve the problem by trying either of the following approaches:
Make your API layer wait for a certain amount of time (Enough for the Postgres DB to boot up)
Thread.Sleep(60); # should be enough so that Postgres DB can start
Implement a retry mechanism that will wait for let's say 10 seconds everytime the connection fails to establish.
If this doesn't work, I would recommend for you to check whether there is a Postgres DB installed outside of the container that owns the port you were trying to access.
Along with Allan Chua's answer, Please mention a startup dependency on Postgres service in your docker-compose file.
depends_on:
- postgres
Add this to your api service.

Categories