skip to Main Content

Relevant part of the docker-compose.yaml:

version: "3.5"

services:

  Database:

    image: postgres
    container_name: Example-Local-Database
    ports:
      - "${DATABASE_PORT}:${DATABASE_PORT}"

    environment:
      - "POSTGRES_PASSWORD=${DATABASE_PASSWORD}"

    volumes:
      - DatabaseData:/var/lib/postgresql/data

  # ...

volumes:
  DatabaseData:
    name: Example-Local-DatabaseData
    driver: local

ENV variables (calm down, no sensitive information to steal, just default settings for the local development mode):

DATABASE_PORT=5432
DATABASE_PASSWORD=pass1234

DataSource configuration:

{
    type: "postgres",
    host: "Database",
    port: 5432,
    username: "postgres",
    password: "pass1234",
    entities: [ MemberModel ]
}

The connection from IntelliJ IDEA is fine:

enter image description here

Same for my NestJS application.

However, the typeorm CLI could not connect with this database.
For example, the command typeorm migration:generate test/test2 -- -d typeorm-cli.config.ts
causes the error:

Error during migration generation:
Error: getaddrinfo ENOTFOUND Database
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'Database'
}

I have checked the trying to connect typeorm with mysql container in docker-compose but i get this error Error: getaddrinfo EAI_AGAIN mysql topics: in my case, the host refers to Database the service name mentioned in docker-compose.yaml.

2

Answers


  1. As you can see in your IntelliJ screenshot, you’re using localhost as the host name.

    In your code, you use Database which only works on the Docker network. Not from the host.

    Change your code to use localhost as well.

    Login or Signup to reply.
  2. You exposed ports to host so you should use localhost as db_host to connect.
    If you trying to connect from another container you should use host.docker.internal as db_host to connect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search