'Docker compose Connection refused database and migration

I spend hours and i'm not able to make it working. The target is:

  • Start database
  • perform sql-migration to create tables and default-data via sqls

See error i get:

object-modeller-db           | PostgreSQL init process complete; ready for start up.
object-modeller-db           |
object-modeller-db           | 2022-03-04 11:10:05.883 UTC [1] LOG:  starting PostgreSQL 13.4 (Debian 13.4-4.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
object-modeller-db           | 2022-03-04 11:10:05.883 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
object-modeller-db           | 2022-03-04 11:10:05.883 UTC [1] LOG:  listening on IPv6 address "::", port 5432
object-modeller-db           | 2022-03-04 11:10:05.887 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
object-modeller-db           | 2022-03-04 11:10:05.892 UTC [63] LOG:  database system was shut down at 2022-03-04 11:10:05 UTC object-modeller-db           | 2022-03-04 11:10:05.896 UTC [1] LOG:  database system is ready to accept connections
objectmodeller-init-service  | Migration failed: dial tcp 127.0.0.1:5432: connect: connection refused
objectmodeller-init-service exited with code 1
objectmodeller-init-service exited with code 1

See my docker-compose.yml:

version: "3.1"

services:
  db:
    container_name: object-modeller-db
    image: postgres:13.4
    restart: always
    env_file:
      - ./.env
    environment:
      POSTGRES_DB: ${OBJECT_MODELLER_DATABASE_NAME}
      POSTGRES_USER: ${OBJECT_MODELLER_DATABASE_USER}
      POSTGRES_PASSWORD: ${OBJECT_MODELLER_DATABASE_PASSWORD}
    volumes:
      - object-modeller-postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  migration:
    container_name: objectmodeller-init-service
    restart: on-failure
    build:
      context: .
      dockerfile: ./migrations.Dockerfile
    env_file:
      - ./.env
    environment:
      OBJECT_MODELLER_DATABASE_HOST: 'db'
      OBJECT_MODELLER_DATABASE_PORT: '5432'
    volumes:
      - ./migrations/dbconfig.yml:/dbconfig.yml
    depends_on:
      - db
  
volumes:
  object-modeller-postgres-data:

the dbconfig.yml:

development:
  dialect: postgres
  datasource: host=localhost port=5432 dbname=${OBJECT_MODELLER_DATABASE_NAME} user=${OBJECT_MODELLER_DATABASE_USER} password=${OBJECT_MODELLER_DATABASE_PASSWORD} sslmode=disable
  dir: .
  table: migrations

Can anybody help or have an idea?

I also tried to connect them in the same network, or links them... but it doesn't work.

Thanks alot



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source