I have Spring Boot app inside docker and I am running my app like this:
docker run --rm --network host --name myapp1 myapp
But when i am trying to access it from host machine it fails:
my_machine:~ root$ curl localhost:8081/someendpoint -v
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8081 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connection failed
* connect to 127.0.0.1 port 8081 failed: Connection refused
* Failed to connect to localhost port 8081: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 8081: Connection refused
It is not clear for me - why it is not working ? It works fine from inside of docker. Also myapp have no problems with connection to external docker images/internet. Please help.
From https://docs.docker.com/network/host/
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Since you said you were using macOS, --network host
will not work properly. I believe the underlying reason is that, outside of Linux, a virtual machine is used to host the containers. The host whose network the container shares is the VM's, not the physical host's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With