Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTTP requests to my server running in a docker container?

I've set up a java application which acts as a web server and handles http requests. I've tested that it works outside the container (which it does), but when in the container my requests don't seem to get to it.

The server listens on port 3971, and the Dockerfile looks like this:

FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]

Calling to the root address should return something (normally I'd send a GET to http://localhost:3971/).

I've tried replacing 'localhost' with the the ip address of docker-machine, and also with the ip address I get when inspecting the running container for my server, but neither seem to respond. When I call to the ip address of docker-machine, I get ERR_CONNECTION_REFUSED. Is there something else I need to enable?

like image 556
Andy Avatar asked Mar 31 '16 16:03

Andy


People also ask

Can Docker communicate with localhost?

Connecting to the Host NetworkDocker provides a host network which lets containers share your host's networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself. Now your container can reference localhost or 127.0. 0.1 directly.


1 Answers

you are doing EXPOSE 3972 which exposes the port to other linked containers but NOT to the host machine

To expose the port to the host machine you do ...

docker run -p 3972:3972 ....... etc

you can also do ...

docker run -P

which exposes all ports exposed by EXPOSE to the host (this is not the default - you must use the -P flag here)

like image 63
danday74 Avatar answered Sep 30 '22 17:09

danday74