Docker postgres connection from *inside* docker container to host machine's postgres localhost instance on a Mac - postgresql

All the questions on SO about this seem to refer to an opposite case of creating a postgres container and connecting it from Mac host. But I am trying to do the opposite, without success. I have localhost running on my Mac host machine, and despite setting port flags, I cannot get code inside my container to talk to my localhost postgres (talks to remote host postgres just fine).
docker run -it -p 5000:5000 -p 5432:5432 yard-stats
Then inside docker:
telnet 0.0.0.0 5432
Trying 0.0.0.0...
telnet: Unable to connect to remote host: Connection refused
or telnet 127.0.0.1 or localhost. Connection is refused.
Edit: I also tried with flag --network="host", which did not change anything except break inbound connections to the container on localhost:5000 as well.

If you are using docker for mac, you can use use host.docker.internal special DNS name which resolves to the internal IP address used by the host.
You can also use --network="host" with your docker run command to run the container in host network. Then the localhost interface inside the container will be same as localhost interface of the host machine when run in host network. So you should be able to use localhost:5432 to connect to postgresql. You can remove -p option as it has no effect when running with --network="host".
docker run -it --network=host yard-stats

Related

can't connect to postgres in ipv6 using docker

My scenario is the following: I have a postgres 12 installed on ubuntu 20.04 server and a docker container running in another host with ipv6 enabled.
I can't reach postgres server from inside docker container using ipv6.
these commands works from within the container:
$ ping POSTGRES_SERVER_IPv4
$ ping POSTGRES_SERVER_IPv6
$ telnet POSTGRES_SERVER_IPv4 5432
but this does not work from inside the container (but it works from the docker host):
$ telnet POSTGRES_SERVER_IPv6 5432
i've already set listen_address = '*' and "host all all md5" in postgres pg_hba.conf.
EDIT:
i've realized that my network adapter is using two IPv6, the static one that I defined in netplan and another one that is within my network range, but I didn't assigned it.
In your netplan configuration, try to add:
accept-ra: no
example:
network:
version: 2
ethernets:
ens192:
accept-ra: no
addresses: ...

Docker Rundeck + local Postgres

I'm trying to run a docker image of rundeck, using PostGres for the database.
The issue I'm having is mapping my local postgres installation to the docker rundeck image. The postgres port runs on 5432, and have confirmed using netstat that the port is open and listening. The port for rundeck needs to run on 4440.
I have tried the following command:
docker run -p 127.0.0.1:4440:4440 -e RUNDECK_DATABASE_URL=jdbc:postgresql://localhost/rundeck -e RUNDECK_DATABASE_DRIVER=org.postgresql.Driver -e RUNDECK_DATABASE_USERNAME=xxx -e RUNDECK_DATABASE_PASSWORD=xxx --name test-rundeck -t rundeck/rundeck:3.0.19
But it fails with an error: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP
I'm guessing it's because the internal 5432 port isnt mapped to the docker container port 5432?
I tried mapping the ports with -p 127.0.0.1:5432:5432 but that fails with the error:
Error starting userland proxy: listen tcp 127.0.0.1:5432: bind: address already in use
At this point I might just resort to running both PostGres and Rundeck as docker images, but I would rather like to resolve this problem.
Any ideas on how to map a local PostGres to a docker ran Rundeck?
Found the answer.
Had to edit the pg_hba.conf file to allow the docker0 ip address through.
host all all 172.23.0.0/16 md5

Unable to connect containerized app to containerized postgres, receiving dial tcp connection refused error

This community is my last resort for this problem, as I have been fighting with this for several hours now.
I have a go app running in one container, in the other container I am running a postgres db. I am able to connect to the postgres db from my go application as long as only my postgres is within a container, and my go app is running locally as usual. However, when my go app is trying to access the postgres from within a docker container i am getting the following error:
dial tcp 127.0.0.1:8080: connect: connection refused
Below I try to provide enough information, but will gladly add more if requested.
I have 2 docker containers running with the following ports:
go application, port info: 8081/tcp -> 0.0.0.0:8081
postgres db, port info: 5432/tcp -> 0.0.0.0:8080
I am running the go app with:
docker run -it --rm --name gographqlserver --link postgresdb:postgres -d -p 8081:8081 gogogopher;
and the postgres db with:
docker run -it --rm --name postgresdb -e POSTGRES_PASSWORD=hello123 -d -p 8080:5432 postgresimage;
both containers can be started without any problems.
I have also tried connecting both containers within a docker network, which did not help.
help would be immensely appreciated!
You are using localhost address within the container which is not the same as your host's address. You should do one of the following instead:
Use your actual host's IP from app's container
Use postgresdb container's IP with the native port (5432). You can discover this IP using docker inspect postgresdb.
Use postgresdb as host name and the native port (5432) when connecting both containers to the same network

Docker port mismatch from inside another container

I have a simple setup of 2 docker containers, one for the database and one for the web service.
I start the DB docker container like so:
docker run -d --name dbs.service -p 5434:5432 -e POSTGRES_DB=my_app -e POSTGRES_USER=my_user -e POSTGRES_PASSWORD=my_password postgres:9.6.2
This works fine. And from localhost, i can connect to it fine as well (using pgcli for connection)
pgcli postgres://my_user:my_password#dbs.service:5434/my_app
Now I start the web service container, which works fine
docker run -d --name web.service --link dbs.service:dbs.service web-service:latest
However here's the problem. From inside the container, I cannot connect to DB using port 5434 but I can connect to DB using port 5432.
So I login to container using
docker exec -it web.service bash
Now this works
pgcli postgres://my_user:my_password#dbs.service:5432/my_app
but this does not
pgcli postgres://my_user:my_password#dbs.service:5434/my_app
I can't understand why it can connect to 5432 but not 5434. Any suggestions?
-p 5434:5432
This option publishes the port for access from outside of the docker host to your container. The host will listen on 5434 and route the traffic through to the container's port 5432.
However, container-to-container traffic doesn't use that. Container to container traffic simply needs a common docker network. From there, any container can talk to any other container on the same network. The port used is the container listening port, not the published port on the host. You don't even need to publish the port for it to be accessible by other containers.

Allowing Docker container to access Postgres running on localhost

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!
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.