2

I've got a docker container which is supposed to run a (HTTP) service. This container should be able to connect to PostgresSQL running on the host machine (so it's not part of the container). The container uses the host's network settings:

docker run -e "DBHOST=localhost:5432" -e "DB=somedb" -e "AUTH=user:pw" -i -t --net="host" myservice

I'm using MacOSX, so Docker is running on a Virtualbox VM. I guess I need port forwarding to make this work. I've tried to configure that:

VBoxManage controlvm "default" natpf1 "rule1,tcp,,5432,,5432";

But this doesn't work. If I start up the service, all I get is a connection refused message and the service cannot connect to Postgres.

Postgres is running on port 5432, on the host machine. The "default" is the name of the VM created by Docker installer.

What am I doing wrong? Please help!

1
  • Why not run the postgres database in a container as well? There are many pre-built images for this. Commented Oct 22, 2015 at 22:39

1 Answer 1

1

I've had success with this using the --add-host flag, which adds an entry into the /etc/hosts in your container. Boot2docker and docker-machine both assign an ip you can use to hit your localhost from inside a container, so you just want to add an entry that points back to this.

With boot2docker, where the default host ip is 192.168.59.3, you can just do docker run --add-host=my_localhost:192.168.59.3 ...

With docker-machine, I think you'll need to lookup your localhost's mapped ip in Virtualbox, and then you can do the same: docker run --add-host=my_localhost:[localhost_mapped_ip_from_docker] ...

Try setting that up and then trying to connect to your Postgres instance through my_localhost. Make sure you correctly set access and accepted inbound ip permissions in Postgres as well, as if it's not listening on the container's ip or 0.0.0.0, it won't work no matter what.

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

1 Comment

Hi Eli, can you please elaborate on how to correctly set access and accepted inbound ip permissions in Postgres?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.