Docker Community Forums

Share and learn in the Docker community.

Docker and postgres: Error: connect ETIMEDOUT

Hello I have a problem with my docker compose postgres and pgadmin

Error:

Error: connect ETIMEDOUT 172.19.0.2:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) { errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect',
address: '172.19.0.2', port: 5432 }

my docker compose:

version: "3.7"
services:
  postgres:
    image: postgres:12
    restart: always
    container_name: "postgres_container"
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: spirit
      POSTGRES_PASSWORD: emasa
      POSTGRES_DB: emasa
  pgadmin:
    image: dpage/pgadmin4
    container_name: "pgadmin_container"
    depends_on:
      - postgres
    ports:
      - 5050:80
    environment:
      PGADMIN_DEFAULT_EMAIL: emasa@emasa.com
      PGADMIN_DEFAULT_PASSWORD: admin
volumes:
  db_data:

i’m not able to imagine the reason for my error i give a docker-compose log and my postgres works normal:

postgres_container | 2020-03-19 02:48:12.162 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_container | 2020-03-19 02:48:12.162 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_container | 2020-03-19 02:48:12.184 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_container | 2020-03-19 02:48:12.256 UTC [24] LOG:  database system was shut down at 2020-03-19 02:48:04 UTC
postgres_container | 2020-03-19 02:48:12.290 UTC [1] LOG:  database system is ready to accept connections

I think the problem is this: listening on IPv4 address “0.0.0.0”, port 5432

and this is my config:

{
  "type": "postgres",
  "host": "172.19.0.2",
  "port": 5432,
  "username": "spirit",
  "password": "emasa",
  "database": "emasa",
  "synchronize": true,
  "logging": false,
  "entities": ["src/entity/**/*.ts"],
  "migrations": ["src/migration/**/*.ts"],
  "subscribers": ["src/subscriber/**/*.ts"],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

Whatever you do, never ever use a container’s ip to connect to its containerized service. usualy people trying to access a container by it’s ip, do something not indendet to be done like this. A container can and will change its ip when re-created.

Please replace the container’s ip with its servicename and try again.

I tried with the name of the container and had this new error:

Error: getaddrinfo ENOTFOUND postgres_container
at GetAddrInfoReqWrap.onlookup

Please share your exact configuration, why would the error message say “postgres_container”?! Did you change the service name from postgres to postgres_container? the service name is NOT the container name.

yes my docker-compose:

version: "3.7"

services:

  postgres:

    image: postgres:12

    restart: always

    container_name: "postgres_container"

    ports:

      - "5432:5432"

    volumes:

      - db_data:/var/lib/postgresql/data

    environment:

      POSTGRES_USER: spirit

      POSTGRES_PASSWORD: emasa

      POSTGRES_DB: emasa

  pgadmin:

    image: dpage/pgadmin4

    container_name: "pgadmin_container"

    depends_on:

      - postgres

    ports:

      - 5050:80

    environment:

      PGADMIN_DEFAULT_EMAIL: emasa@emasa.com

      PGADMIN_DEFAULT_PASSWORD: admin

volumes:

  db_data:

and my config:

{

  "type": "postgres",

  "host": "postgres_container",

  "port": 5432,

  "username": "spirit",

  "password": "emasa",

  "database": "emasa",

  "synchronize": true,

  "logging": false,

  "entities": ["src/entity/**/*.ts"],

  "migrations": ["src/migration/**/*.ts"],

  "subscribers": ["src/subscriber/**/*.ts"],

  "cli": {

    "entitiesDir": "src/entity",

    "migrationsDir": "src/migration",

    "subscribersDir": "src/subscriber"

  }

}

my postgres contianer has this name

The service name of your postgres service is postgres, NOT postgres_container.

1 Like