I have looked at all the answers out there to resolve this, but with no success, I have tried the following:
Get the IP of container and use it
Use local IP address (127.0.0.1)
Use host.docker.internal
I have 2 Postgres containers running, one doesn't have any PORT and IP Address (network is "host") as you can see and the other does (network "birdge" and has IP Address, see the screen shot)
When you start the docker container, you need to publish the container's port to the host using -p. Since you wanted port 7676, you can do:
docker run -p 7676:5432 postgres
And point pgadmin to localhost:7676
Related
I have a mongo on my host machine, and an ubuntu container which is also running on my machine. I want that container to connect to mongo.
I set as host url, my host ip from docker network : 172.17.0.1
and in the /etc/mongod.conf file I set the bindIp to 0.0.0.0
from the container, I can ping the host,but the mongo service is not accessible, I get that error :
Connecting to: mongodb://172.17.0.1:27017/directConnection=true&appName=mongosh+1.5.0
MongoServerSelectionError: connection timed out
More over, I can connect from host to the mongo service with that command :
mongosh mongodb://172.17.0.1:27017
Do you know why I can't access mongo service from my container ?
You can use host.docker.internal as reference to your host machine from the container. So mongo host.docker.internal:27017 should work.
From docker documentation:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.
On your local machine that has Mongo service is running, you can access by Mongo client because you expose the service at 127.0.0.1:27017.
However, it is not true if standing from your unbuntu container, there is no Mongo service is running at 172.0.0.1:27017 of the ubuntu container.
Docker-compose is the right tool for you to make containers communication to each other.
I have running widlfy docker and the postgres docker and also nginx docker, I want to connect all the three docker internally so my widlfy can connect to postgres and nginx and I have my url running. I have added the parameter of networking in the docker-compose.yml file but still not success.
Can you suggest how can I perform this activity of interconnection.
If you can't have all the containers in the same docker-compose.yml, then use the next.
The containers have assigned an internal port and external port, internal is inside the every docker's network and the external is where you can access using the computer's IP or localhost. In this case you need use your computer's IP, If the IP is 192.168.1.20 and the externals ports for the containers are:
Widlfy: 8080
Postgresql: 5432
ngnx: 80
Then you can connect them using this information:
Widlfy: 192.168.1.20:8080
Postgresql: 192.168.1.20:5432
ngnx: 192.168.1.20:80
The computer's IP use to change in a period of time, it depends of your modem, so check that your IP be the right when you use this option.
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
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
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.