wlaem
wlaem

Reputation: 324

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?

Upvotes: 0

Views: 536

Answers (1)

JohnXF
JohnXF

Reputation: 1097

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.

Upvotes: 1

Related Questions