How to connect host 'localhost' from docker container - java

I am facing one issue where I am trying to connect localhost:3306 from a microservice which is running in docker container. I have multiple microservices hence written docker-conpose file.
My issue is: one microservice is trying to connect mysql which is running in my machine, means its available on 'localhost:3306' however, when microservice is trying to connect its throwing message connection refused.
There are couple of solutions I have found
Trying to connect with 172.17.0.1 in instead of localhost
Since my sercices are running in ubuntu i tried using 'host.docker.internal'
Nothing worked for me. I am looking for solution here how I can access
I already tried solution mentioned in below thread.
From inside of a Docker container, how do I connect to the localhost of the machine?

You have to consider docker containers as another computer or device running on an internal network. If MySQL is only listening on 127.0.0.1 IP, the containers will not be able to communicate with it on the external IP address. When I query host.docker.interal from a docker container, I get the following: 192.168.10.2. So you have to make sure MySQL is listening on not only the 127.0.0.1 localhost. IP address.
You can fix this by setting the bind-address to 0.0.0.0 in the my.cfg.
Resources: https://linuxize.com/post/mysql-remote-access/

Related

Dockerized tomcat webapp throwing 404 exception when connecting to another localhost URL

I've got an application running in tomcat 8.5 in a docker container. Java version is jdk 8. There is a properties file that the app uses that points to another running service. In this case, I have the service running on my host machine and the property is set to point to my localhost:
my.external.service.url=http://localhost:8080/my-api-service
When my tomcat app is also running on the host machine, this works fine. But when my app runs in the docker container, I get a 404 error when it tries to call this service.
I tried switching the URL to point to my machine name:
my.external.service.url=http://my.pc.url.com:8080/my-api-service
But in this case, even though it still works if the tomcat app is running on the host instead of the docker container, I get a different error:
java.net.UnknownHostException: my.pc.url.com: No address associated with hostname
How do I configure the container so that I can get this to work?
Looking at the specific error you got - No address associated with the hostname - that means it cannot resolve the hostname to an IP. So you can find you local host IP and then pass an argument to the docker run command to add that host to the container lookup. Eg: -add-host="my.pc.url.com:X.X.X.X"
Docker networking is a potentially complex thing. Basically, if you run two separate containers they will not 'see' each other. You could have them both expose ports on the host machine and have then 'connect' that way, but you are better using the networking features of docker to ensure the containers talk to each other.
For a simple example like you have - say just two containers running tomcats that you would like to talk to each other - you might be best to just run them both via docker-compose so that they are in the same network.
There are many resources on the internet to explain the further details of docker networking if you wish to explore.

WiFi conflicting with local mysql connection (Java/Spring)

I'm using java/spring to connect to a local mysql database and run some integration tests. Sometimes I get Hibernate errors saying that a connection cannot be established. I've noticed this only happens when WiFi is turned on. If I turn off WiFi, and plug in an ethernet cable, the connection established just fine and all integration tasks pass.
I found sources for this issue regarding PHP, where localhost and 127.0.0.1 are treated differently (one uses TCP, while the other uses the local socket), but none of those solutions helped. I have ensured that IPV6 is disabled and the only parameters in the /etc/hosts file is "127.0.0.1 localhost"
Also, simply running mysql from the command line connects just fine, regardless of whether i use
mysql -u<username> -p -h127.0.0.1 //or
mysql -u<username> -p -hlocalhost
Does anyone have any idea why my WiFi connection is preventing me from connecting to my local DB from spring?

Connecting to remote virtual machine using sockets

I am trying to connect to a virtual machine using sockets in Java. I have my client and server applications working with no issues on localhost. When I run the Server code and try to connect from a separate machine with the IP address and port number, my client connection times out.
Here is a link to my virtual machine network settings page https://wiki.redbrick.dcu.ie/mw/RBVM_Networking
Do I need to specify any particular parameters in Java to make my server accept connections? Is this an issue I have to take up with the VM network admins?
Any suggestions would be appreciated !
p.s I am running ubuntu server and can access phpmyadmin remotely with no problems.
Edit
I have run nmap and the port I am using for the server side application shows up as open.
My provider was blocking all but a select few ports, got a list of open ones and now it works. derp!

java tcp connection with public i.p

I am trying to tcp connect to a server machine in java, by using its public i.p. but when i run the client application i constantly getting a connection refused error. if i used localhost instead of the public ip, it works perfectly.
i search the internet for several causes of the issue but i couldnt fix it.
i forwarded the port to my machines' local i.p address(192.168.1.3) in routers settings. then i checked if port is listening when i ran the server application using netstat -an. i saw lines like,
0.0.0.0:19999 or []:19999 .
19999 is the port number i am trying to listen to. Then i changed my ServerSocket constructor to the 3 parameter one, which also binds the local address.
InetAddress miad = InetAddress.getByAddress(addr);
ServerSocket socket1 = new ServerSocket(port,10,miad);
addr is the InetAddress of my machines local i.p. After these modifications, when i start the server application, i run netstat and it shows:
TCP 192.168.1.3:19999 0.0.0.0 LISTENING
Here i think that my server is listening on the port specified properly.
I have also disabled my firewall and antivirus software.
I have seen several threads and discussions on the net about the issue, and tried most of the things mentioned there, but i keep getting the connection refused error.
What can i be doing wrong? It runs without any errors when i test with localhost.
This is because of the router (not very sure, but almost). Try to see if a webservice like www.canyouseeme.org can connect to your server.
The main idea is that an internal machine (inside the LAN) cannot connect to a machine inside the same LAN by using the external (public) IP address.
I'm pretty sure that it will work, using you internal ip (192.168.1.3).
And if you are sure that you forwarded ports correctly, CanYouSeeMe will say your server is reachable. If it doesn't, make sure you ISP isn't blocking the ports for some kind of "safety reasons".
To figure out if your problem relates to Java and programming please do
telnet 192.168.1.3 19999
If it can't connect then superuser.com would be a better place to discuss this issue.

SSH tunneling with JDBC on Mac OS X - Problems

I am currently developing a java application that uses JDBC inorder to connect to MySQL on a computer running Mac OS 10.6.7. The server that I connect to suddenly decided to use SSH. I've managed to connect to the database using MySQLWorkbench so there is nothing wrong with the connection.
My next attempt was to start the terminal and type ssh username#sshserver.com -L 3305:databaseserver:3306 and then start Eclipse and run my application. The terminal prompted me for my password but JDBC could not connect. I tried SSH Tunnel Manager with the following settings: http://imageshack.us/photo/my-images/146/sshb.jpg/ and it also prompts me for the password and then gives a green light. Still no success. The application worked fine before when i just connected without SSH. I would be very grateful for any help, I googled and tested every suggestion I found but nothing worked.
Two issues to keep in mind using tunnels:
Remember that when you use a tunnel, your JDBC client should connect to localhost port 3305 (or 3306, whatever you're actually using). It shouldn't continue trying to connect directly to the remote host.
SSH will tunnel the connection to the remote end and try to connect to "databaseserver" port 3306 from there. If ssh on the remote host can't resolve the hostname that you gave it, it won't be able to make the connection. Since you are tunneling to the database server, you could try connecting to "localhost" or "127.0.0.1" instead.

Categories

Resources