I got postgres.app running locally on my Mac and would like my local docker container to be able to connect to it. How do I best do this?
I found this post that suggests to pass the Docker host’s IP address to a container using the --add-host flag (Host port with DB to Docker container). However, my laptop's IP address changes frequently. Isn't there an easier way of doing this? Isn't there an easy way to open a local port to a container?
Few things
Use docker.for.mac.localhost as your HOST (This assumes you have the latest Docker for Mac as #Pete mentioned)
Make sure there is such a record in ~/Library/Application Support/Postgres/var-9.6/pg_hba.conf
host all all 0.0.0.0/0 trust
Change this line listen_addresses = 'localhost' in ~/Library/Application Support/Postgres/var-9.6/postgresql.conf
to
listen_addresses = '*'
or
listen_addresses = 'localhost, docker.for.mac.localhost'
If we are talking about a developers workstations, you could start your Docker Container inside the Host Network.
docker run --net=host myContainer
So your container runs in the same stack as your Host, and should be able to access your postgres.app.
From your container, try connecting to hostname docker.for.mac.localhost. For example:
psql -U my_user docker.for.mac.localhost -U my_user my_database
From the docs:
The Mac has a changing IP address (or none if you have no network
access). From 17.06 onwards our recommendation is to connect to the
special Mac-only DNS name docker.for.mac.localhost which will resolve
to the internal IP address used by the host.
Note: this requires Docker for Mac >= 17.06.0-ce-mac18, 2017-06-28
Related
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: ...
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?
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
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:
It adheres to the best practice of each container having only a single process and single responsibility.
It means that you can easily change and re-deploy your service without having to recreate the database container.
Configure the connection inside your docker container with the real ip-address of your host or as workaround with a dns name
I want to run PostgreSQL inside a Docker container. I am building my own Docker image, as I want to include PostgreSQL extensions. I should edit the pg_hba.conf configuration file to:
allow access originating from other containers
allow access originating from the host
The first is quite simple: I can add a rule for 172.17.0.0/16 if I am not mistaken.
But how can I approach the second? What does the IP (or range) looks like when the host connects to psql in a container?
Remark: I am starting the container via docker run -p 127.0.0.1:5432:5432, so in theory I could just allow all in pg_hba.conf because the port forwarding is only bound to 127.0.0.1. But I prefer this extra level of security in pg_hba.conf for the situation when I (probably by mistake) run the container via docker run -p 5432:5432. I hope this makes sense.
update
Actually, setting range 172.17.0.0/16 does not seem to be correct. For example, my container had IP 172.18.0.2 in my test. There does not seem to be a consensus on the default range or how to configure that range, according to my investigations so far.
check your docker0 bridge interface in your case it might be 172.18.0.0/16
make changes in postgresql.conf path will be same as pg_hba.conf.
listenaddress to "*"
Then in pg_hba.conf add rule as
host all all 172.18.0.0/16 md5.
run the docker with hostip : docker run -p :5432:5432
in this way other containser on same docker n/w can connect aswell as from host,but not from other hosts.
I have PostgreSQL DB running locally and a Docker container with an application that wants to connect there.
How can I access localhost DB from inside docker?
docker run --rm -e "DATABASE_URL=postgresql://postgres:postgres#127.0.0.1:5432/my_db" --network="host" -p 4000:4000 my_image
The above doesn't seem to work. Neither this one: From inside of a Docker container, how do I connect to the localhost of the machine?
I think your problem is in param net. In the documentation talk about --network
docker run --rm -e "DATABASE_URL=postgresql://postgres:postgres#127.0.0.1:5432/my_db" --network="host" -p 4000:4000 my_image
The settings you have should work correctly, so the problem might be with the configuration of PostgreSQL. The first potential configuration setting I can think of is the bind address. By default this is set to only accept connections from localhost, but since the Docker container will have its own ip address PostgreSQL won't accept traffic from the container. Try setting it to listen_addresses(0.0.0.0) to see if this fixes your problem.
Also be careful to always use 127.0.0.1 as the address because localhost does not always work.
I will plagiarize my own answer from here:
Other answers did not work well for me. My container could not resolve host ip using host.docker.internal. There are two ways
Sharing host network --net=host:
docker run -it --net=host myimage
Using docker's ip address, which is usually 172.17.0.1.
You can check it by calling ifconfig command and grabbing inet addr of docker interface
user#ubuntu:~$ ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:a4:a2:b2:f1
inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:a4ff:fea2:b2f1/64 Scope:Link
Once you have this ip address, you can pass it as an argument to docker run and then to application or as I do it, map the location of jdbc.properties via volume to the directory on host machine, so you can manage the file externally.
docker run -it -v /host_dir/docker_jdbc_config:${jetty_base}/var/config myimage
NOTE: Your database might not allow external connections. In case of postgresql, you need to edit 2 files, as described here and here:
Edit postgresql.conf to listen on all addresses. By default it will point to localhost.
listen_addresses = '*'
Edit pg_hba.conf to allow connections from all addresses. Add on the last line:
host all all 0.0.0.0/0 md5
IMPORTANT: Last step updating database access is not recommended for production use unless you are really sure what you are doing.
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.