postgresqldockernetworking

connection to server at "localhost" (::1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?


I have running postgresql docker container with next command docker run -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -v /var/lib/postgresql/data:/var/lib/postgresql/data postgis/postgis

pg_hba.conf file include next lines:

# warning trust is enabled for all connections
host all all all trust

Connection request from host machine: psql -h localhost -U fake

Response in DB logs: FATAL: role "fake" does not exist

Test Dockerfile:

FROM postgres:latest
RUN apt-get update && apt-get install -y postgresql-client
CMD ["psql", "-U", "fake", "-h", "localhost", "-p", "5432"]

Test docker container failed with next error:

psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?

I don't see anything in DB logs.

Why I can't make a request from the test docker container?


Solution

  • To connect two different docker containers I have to provide next parameter --network="host".

    Correct command would be docker run --network="host" test