Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker with "--network host" does not allow to access to port of application

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.

like image 402
Alfred Moon Avatar asked Nov 16 '22 08:11

Alfred Moon


1 Answers

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.

like image 82
Lorrin Avatar answered May 07 '23 02:05

Lorrin