0

I have a docker service for postgres that is running fine:

postgres:
    restart: always
    image: postgres:13.0-alpine
    ports:
      - "5432:5432"
    environment:
      - DEBUG=false
      - POSTGRES_DB=yola
      - POSTGRES_PASSWORD=password123

I can connect fine using terminal like this:

export PGPASSWORD=password123; psql -h localhost -p 5432 -d yola -U postgres

Now when I try and run flyway I get a connection problem. I tried doing both 0.0.0.0 and localhost and the docker image name yola_postgres_1.

I still get this error running the following command:

docker run --rm -v /path/to/folder/migrations:/flyway/sql flyway/flyway -url=jdbc:postgresql://localhost:5432/yola -user=postgres -password=password123 info

ERROR: Unable to obtain connection from database (jdbc:postgresql://localhost:5432/yola) for user 'postgres': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Caused by: java.net.ConnectException: Connection refused (Connection refused)

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • What's the connection between the docker image of postgres and the docker image with flyway? Because it seems that the docker-flyway is using localhost (which means inside the docker image with flyway, not your localhost) instead of using the address of the docker-postgres. – Sergio Lema Oct 06 '22 at 15:52

1 Answers1

2

The Problem

You have PostgreSQL running in one container and Flyway in another, and localhost is per container, so Flyway isn't communicating with PostgreSQL

A Solution

You already map 5432 in PostgreSQL to 5432 on the host, so you can add --network=host to the docker run command for the Flyway container which makes localhost match that of the host

This will make your current set up work, and you could then look into container networking if you want your containers to communicate with each other directly, not through the host

DoodleBobBuffPants
  • 169
  • 1
  • 1
  • 9
  • I use docker-compose for my postgres, does it have a network option? – Blankman Oct 06 '22 at 17:19
  • 1
    @Blankman You need to set `--network=host` on the `docker run` command for the Flyway container - I'll update the answer to make this clearer. To answer your question however, yes, [here](https://docs.docker.com/compose/compose-file/#network_mode) is the documentation for setting the network in a compose file. The property is called `network_mode` – DoodleBobBuffPants Oct 06 '22 at 17:28
  • 1
    Host networking shouldn't be the normal way to communicate between containers. [Networking in Compose](https://docs.docker.com/compose/networking/) describes a more standard approach, using the database container's Compose service name `postgres` as a host name. – David Maze Oct 06 '22 at 19:18