postgresqldockerliquibase

Docker, Postgres, Liquibase: Connection could not be created


I am trying to run migrations on postgres via liquibase in docker. I am getting Connection refuse error. However, I am able to connect to the postgres DB running in docker container via pgAdmin, using same credentials. I looked over stack overflow and google but failed to fix the issue. Error looks as below:

app-liquibase  | Liquibase Version: 4.30.0
app-liquibase  | Liquibase Open Source 4.30.0 by Liquibase
app-liquibase  | ERROR: Exception Details
app-liquibase  | ERROR: Exception Primary Class:  ConnectException
app-liquibase  | ERROR: Exception Primary Reason:  Connection refused
app-liquibase  | ERROR: Exception Primary Source:  4.30.0
app-liquibase  | 
app-liquibase  | Unexpected error running Liquibase: Connection could not be created to jdbc:postgresql://localhost:5432/shopping with driver org.postgresql.Driver.  Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
app-liquibase  |   - Caused by: Connection refused
app-liquibase  | 
app-liquibase  | For more information, please use the --log-level flag
app-liquibase exited with code 1
Aborting on container exit...

This is my docker-compose file

services:
  app:
    build: ./
    container_name: app-server
    volumes:
      - ./:/app-server
    ports:
      - 8080:8080
    expose:
      - 8080
    networks:
      - ktor-network
    depends_on:
      - database
  database:
    image: postgres:latest
    container_name: app-db
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    ports:
      - 5432:5432
    expose:
      - 5432
    networks:
      - ktor-network
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5
  database-migration:
    image: liquibase/liquibase:latest
    container_name: app-liquibase
    volumes:
      - ./app/src/main/resources/liquibase/:/liquibase/changelog
    command: ["--url=jdbc:postgresql://localhost:5432/mydb", "--username=postgres", "--password=postgres", "--changelog-file=liquibase/changelog/changelog.xml", "update"]
    depends_on:
      database:
        condition: service_healthy
    networks:
      - ktor-network
networks:
  ktor-network:
    driver: bridge

Solution

  • The troube is usual here, the question asked year by year every day, and the answer still same: --url=jdbc:postgresql://localhost:5432/mydb - you used localhost here insted of service/container name, and that's mistake. Just use service or container name here instead of localhost.