12

I have a server with installed PostgreSQL. All my services work in containers (docker-compose). I want to use my Host PostgreSQL from containers. Buy I have the error:

  Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/shop-bd) for user 'shop-bd-user': 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.

  Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  Connection refused (Connection refused)

My docker-compose is using host network_mode, like this:

version: '3'
services:
  shop:  
    container_name: shop
    build: ./shop
    hostname: shop
    restart: always   
    ports:
      - 8084:8084
    network_mode: "host"

My database connection URL is: jdbc:postgresql://localhost:5432/shop-bd

5
  • you are asking docker to port 8084:8084 and you don't say anything about port 5432 Commented Jan 19, 2018 at 23:22
  • @Krishna 8084 is port of this service. Other services can communicate with this one with http://localhost:8084. But It does not work for host DB. Commented Jan 19, 2018 at 23:35
  • show how your config for db service looks like. It should have 5432:5432 under ports. Commented Jan 19, 2018 at 23:40
  • Which OS are you using docker on? Commented Jan 20, 2018 at 3:14
  • 1
    @TarunLalwani I develop on Windows Commented Jan 20, 2018 at 9:59

4 Answers 4

30

According to the documentation, from version 18.03 onwards, using host.docker.internal as DNS works for me, without specifying neither --net=host or network_mode: "host"

Sign up to request clarification or add additional context in comments.

2 Comments

Cautionary note: using the --net=host, or network_mode: "host" will prevent your container from registering with Eureka and will break any kind of API gateway you've got unless they all run on the same network.
This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac
6

This would have worked if you were on linux OS. But this won't work on Mac or Windows. The thing is that when you use --net=host, you still are not on host network. That is the limitation of how docker for windows work

Instead of localhost you need to use docker.for.win.localhost as the host. This is a special DNS name which is available on Docker for Windows when you want to refer to the host localhost

Comments

4

If you are mac user, use "host.docker.internal:5432" to connect to localhost Postgres, docs.

Comments

-3

1) ensure that your postgresql instance is listening to all addresses and not only localhost.

In postgresql.conf set listen_addresses as follows:

listen_addresses='*'

2) you are trying to connect from your container to localhost:5432 . That is NOT your host's IP address, but the address of your container's loopback device. You need to use your docker bridge IP address instead.

2 Comments

I use --net=host, so I can use localhost. I think, that the problem occurs because of Windows.
Yeah, you're right I missed that you are using host networking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.