5

I am running Postgres on a Windows 10 computer, and I want to connect to it from a Docker container. I've followed instructions from many sources and things should be working, but they're not.

Command line used to create Docker container:

docker run --rm -d --network=host --name mycontainer myimage

In postgresql.conf:

listen_addresses = '*'  

In pg_hba.conf:

host    all             all             172.17.0.0/16           trust

In the bash shell of my container, I run:

psql -h 127.0.0.1

and I get the error:

psql: could not connect to server: Connection refused

Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

Needless to say, Postgres is definitely running on my computer and I am able to query it from local applications. What am I missing?

Community
  • 1
  • 1
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

3 Answers3

4

THIS WON'T WORK FOR DOCKER v18.03 AND ONWARDS
The answer is already there - From inside of a Docker container, how do I connect to the localhost of the machine?

This question is related to a mysql setup, but it should work for your case too.

FOR DOCKER v18.03 ONWARDS

Use host.docker.internal to refer to the host machine.

https://docs.docker.com/docker-for-windows/networking/#i-cannot-ping-my-containers

Mahesh H Viraktamath
  • 818
  • 3
  • 14
  • 34
  • 1
    Hi Mahesh, it's best if you include the proposed answer inside your own answer, rather than linking to external sources, since the link may break or change over time. In this case, are you suggesting that I should set `network="host"`? If you look at my question, I've already done that, and it didn't work... – Shaul Behr Oct 15 '18 at 10:54
  • Then you can use `host.docker.internal` on the docker container to refer to localhost on the your machine (if your docker version is 18.03+). More details here - https://docs.docker.com/docker-for-windows/networking/ – Mahesh H Viraktamath Oct 15 '18 at 12:06
  • Please accept this answer if it has solved your problem, thank you. – Mahesh H Viraktamath Oct 22 '18 at 08:59
  • Afraid it didn't solve the problem. as @MJRousos pointed out, [the host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.](https://docs.docker.com/network/network-tutorial-host/) – Shaul Behr Oct 23 '18 at 09:16
  • You can use `host.docker.internal` (if your docker version is => v18.03) without `network="host"` while running the container. – Mahesh H Viraktamath Oct 23 '18 at 10:14
2

Configure the connection inside your docker container with the real ip-address of your host or as workaround with a dns name

OkieOth
  • 3,604
  • 1
  • 19
  • 29
  • You are correct, that works. But I can't have my appsettings refer to my actual ip address or DNS name, since it's shared in source control with other developers who will also want to run on their computers. I need it to point at "localhost" or 127.0.0.1 or some other such universal address. – Shaul Behr Oct 11 '18 at 17:04
  • 1
    localhost and 127.0.0.1 are always complicated ... because from container pov there are inside the container – OkieOth Oct 11 '18 at 21:36
  • OK, so failing that is there a way of mapping some other universal address to the host machine? – Shaul Behr Oct 14 '18 at 07:38
  • I don't know anything ... I would also put the postgres in a container and let both run in one compose or stack scenario. In that you are more flixible and you can access postgresql over the container name – OkieOth Oct 14 '18 at 19:18
2

As you've discovered, --network-host doesn't work with Docker for Windows or Docker for Mac. It only works on Linux hosts.

One option for this scenario might be to host PostgreSql in a container, also. If you deploy them with a docker-compose file, you should be able to have two separate Docker containers (one for the database and one for your service) that are networked together. By default, docker-compose will expose containers to others in the same compose file using the container name as its DNS name.

You could also consider including the database in the same container as your service, but I think the docker-compose solution is better for several reasons:

  1. It adheres to the best practice of each container having only a single process and single responsibility.
  2. It means that you can easily change and re-deploy your service without having to recreate the database container.
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
MJRousos
  • 381
  • 1
  • 5