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

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.

Related

How to connect host 'localhost' from docker container

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/

How to access REST API from a remote machine?

I am writting spring service(rest) for mobile app.I am writing test service with localhost and running on virtual machine(windows) but i can see http://localhost:8080/restonly on the same computer.What should i do to use my rest api from another machine?ip
aplication properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=
spring.data.mongodb.database=mongoTest
You are talking about two different things.
Your property file is about connection to your mongodb instance.
localhost is only a shortcut "saying" that the application can be found on your local machine.
having localhost in this property file means that the application server and the database server are on the same physical machine.
If you want to access your API from another machine, you can use the IP address of the server.
On Windows, you can know this address by typing ipconfig in a command line.
On Linux, it would be ifconfig.
Later, you can try to work with machine name instead of the IP address.

Docker host IP inside java program running in the container

is it possible to get the docker host IP inside the java program running in the docker container?
From my understanding that docket from it’s initial design is to isolate the host from containers. Although this is doable, such as passing in a env variable to the container at startup, it is not a good practice. Try to containerize everything, and use a network and network links instead. [Docker Compose maybe helpful]
If you really need to do so for whatever reason here is a link for reference
How to get the IP address of the docker host from inside a docker container

How can I access a service installed on Kubernetes from anywhere?

I am working on a mac machine and installed the latest Kubernetes and followed the example here (this is for dev’t purpose). All went smooth but I was hoping that Kubernetes provide me an ip address and port number where my service will be listening to so that I can access it from anywhere.
Please correct me if I am wrong.
I was able to run ifconfig as well as curl $(minikube service hello-minikube --url) and I was able to see the ip address and port but I wasn’t able to access it outside command line where Kubernetes lives in.
The reason I am trying to access it outside the VM is because we have other projects that run on other machines and I wanted to call the REST service I installed while we are on dev env. This way we don’t have to wait until the service is pushed to production.
FYI: This is my first micro service project and I would appericiate your feedback.
I followed the steps in the article you linked and it works as expected.
Just do:
minikube service hello-minikube --url
You will get a url like http://192.168.99.100:32382/ - the port and IP could and will change for you. Also note that the exposed port will be a random port like the 32382 and not 8080 that the pod uses.
Use the url in your browser, say and you should be able to see the output of the service.

Run Jetty Website on Azure Virtual Machine

I have created a Java Web Application using Jetty (in Eclipse, using OSGI etc.). The application itself runs quite well (when being tested locally), so I wanted to run it on an Azure virtual machine in order to be accessible for external users (for testing reasons).
What I did so far:
created an Azure account
create a virtual machine with Windows Server running in it
downloaded all my eclipse files etc. to the virtual machine
started the application (in fact in eclipse, not the compiled jar) in the virtual machine; the application is published to port 8080
so, when i run a webbrowser in the VM and connect to localhost:8080, everything works well
but when I try to access the website from external (using my assigned domain of the VM, something.cloudapp.net:8080), it does not work
I also created endpoints in the azure management console for this VM (80, 8080, etc.)
Does anyone ever tried to run a java webapp on Azure or has a hint what could go wrong here?
By default, windows servers in Azure have the windows firewall enabled. This would block external connections to port 8080 by default. Try adding an appropriate exception to the windows firewall rules.
According to your description, I think you have correctly configured the new endpoints for the network traffic of Java Webapp. If not or incorrectly does, please refer to the article https://azure.microsoft.com/en-us/documentation/articles/virtual-networks-create-nsg-arm-pportal/ to configure again.
Then, as #CtrlDot said, you need to configure the firewall for allowing the inbound traffic on Windows Server.
As reference, please see the article about allowing inbound traffic to a specified TCP or UDP port on Windows Server to do it.

Categories

Resources