I have a spring boot app that connects fine to my PostgreSQL server running locally in Desktop Docker.
Than I wrote a simple Dockerfile to run my app in container. Container starts but can't connect to my db server with error message:
Connection to localhost:5432 refused.
Why and how to fix this?
To access localhost from inside a docker container you can use the IP of your computer. Localhost or 127.0.0.0 donsen't work.
Use docker compose to connect the two docker containers.
https://docs.docker.com/compose/
On this page you can see an example on how to connect to containers using docker compose:
https://dev.to/mozartted/docker-networking--how-to-connect-multiple-containers-7fl
If they are on seperate Docker networks, use:
docker.host.internal
This connects to the Docker host machine. So if your PostgreSQL instance is exposed on 5432, docker.host.internal will route to that instance through the host from other containers.
Alternatively, set them up in the same network using docker compose or by creating a network and attaching both containers to them. They can then communicate with container name.
https://docs.docker.com/engine/reference/commandline/network_create/
Related
I am trying to package Java web services in Docker. I have a postgres DB hosted on a VM (non-containerised) and the code in the docker container is unable to connect to the database. How to do that?
Theoretically spring.datasource.url= jdbc:postgresql://yourIpAddress/nameOfDB should work.
But database don't run on 8080 so you need to bind the port of database (for postgres 5432, for mysql 3306) and I wouldnt bind it to 80, where everything is listening.
If you are running postgres it will be listening on 5432 so you can do docker run -p 8082:5432 postgres
Then you should be able to connect to your host computer via jdbc:postgresql://yourIpAddress:8082/nameOfDB
This all assumes nothing else jumps in the way, like firewalls or whatnot. I also don't know how you configured your virtual machine. In general you should practice connecting them on the same machine to get the idea first.
In my local environment, I am trying to bind a microservice by up it locally. But instead of bind it with the computer username it bind as below.
UP (1) - host.docker.internal:mySampleMicroService:8096
but it should bind with computer name instead of host.docker.internal part.Thought this due to docker install in my computer, then uninstall it and checked it, but the issues was same
even with docker it doesnot show any image(just check it with 'docker image ls' command)
How can I bind the locally up service with my machine name in NETFLIX EUREKA?
Assuming I have a server in my local network with ip 192.168.100.10.
There is docker container running in it with java application.
Now i want to connect to this java application with VisualVM from my computer which has ip address 192.168.100.20. I thought I had everything configured properly but it still does not work.
I have passed these JVM options:
-Dcom.sun.management.jmxremote"
-Dcom.sun.management.jmxremote.port=9010"
-Dcom.sun.management.jmxremote.authenticate=false"
-Dcom.sun.management.jmxremote.ssl=false"
-Dcom.sun.management.jmxremote.local.only=false"
-Dcom.sun.management.jmxremote.rmi.port=9010"
-Djava.rmi.server.hostname=192.168.100.10"
Then I have exposed port 9010 in Dockerfile:
EXPOSE 9010
Then added this port to docker-compose:
ports:
- "9010:9010"
I am trying to connect to remote host with JConsole or VisualVM from my local machine. In "Remote Process" input in JConsole I put "192.168.100.10:9010" but connection fails with error:
"The connection to 192.168.100.10:9010 did not succeed. Would you like to try again?"
What am I doing wrong?
The solution above is sufficient and working. I've been using env variable to set port number which was not working properly.
I am trying to build an application that can control dockers containers hosted on Docker for windows.
The application is built on top of docker-java library and it works fine.
I tried to host this application on docker itself. Hosting was OK. The issue is that, when the application tries to connect to tcp://localhost:2375 in order to access docker's api, the connection fails.
This is obviously because localhost within the container is not anymore refering to the actual host where Docker for Windows is installed.
So I used the explicit IP address to access docker's api from the container (tcp://192.168.0.10:2375), the connection was also refused!
I stopped the firewall, yet the issue was still there.
I searched for it, then I realized that I have to define "hosts" configuration variable to make docker's api accessable using the host's explicit IP address.
But when I tried to set the "hosts" configuration variable, docker said:
"hosts": Cannot be used in Docker for Windows
So does it mean that there is no way on Docker for Windows to host a container that can access its api?
Though I am not quite familiar with Docker on Windows, I found some introductions on docker website. Just try below.
I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON HOST.
But I strongly suggest you give Docker on Windows up.
Edited: 2018-9-5 19:55
Using host.docker.internal to access the host as comment suggested below.
I currently have a Java Application (.jar) in one container and I am using the docker-compose.yml to create an instance of a mysql database in a second container.
I want to know if it is possible for the container 1 to be able to interect with container 2, and be able to read/write to and from the mysql database
If it is possible, how would I go about this?
Each container is like a virtual machine running inside your actual machine. And they have a virtual network that connects all of them. They can communicate with each other just like real machines on a real network.
When you specify links in your yaml, e.g. from the example from the documentation:
web:
links:
- db
The result will be that inside the web container, the hostname db will resolve to the virtual IP of the db container. You can actually do ping db from within the web container and you should see the db container answer.
For mysql, assuming you named the mysql container db like in the example and linked your application to it like above, you'll simply have to write code that assumes this hostname. E.g. you'd connect to jdbc:mysql://db:3306/databasename. The port depends on what the image you use exposes.
It gets tricky once you want to have the containers running on actually different machines because you need a way to reach the virtual container network inside those machines. There are ways like proxies, forwarded ports, overlay networks, .. but that's beyond the capabilities of compose.
From the code point of view, the interaction is the same.
Your MySQL in Docker exposes the service on a particular hostname and port. The program using it uses that hostname and port. Docker gives you the ability to configure this outside MySQL, but the Java code is the same.
Yes, linking containers over a network is standard functionality.
As Peter Lawrey mentioned, you just configure the database connection in Java as normal with the name of service or docker container you want to connect to.
version: "2"
services:
web:
image: myapp
networks:
- myapp
db:
image: mysql
networks:
- myapp
networks:
myapp:
driver: bridge
Then you have a network to connect over
$ docker-compose start
Starting composenetworks_web_1
Starting composenetworks_db_1
$ docker exec composenetworks_web_1 ping db
PING mysql (172.22.0.3): 56 data bytes
64 bytes from 172.22.0.3: seq=0 ttl=64 time=0.093 ms