5

When i run a container for a web-application that listens on port 8090

With

docker run -p 8090:8090 -h=%ComputerName% mycontainer

Then i can access the services on http://localhost:8090

If i started the container with

docker run --net="host" -h=%ComputerName% mycontainer

Then i can't access to the services on http://localhost:8090

Why ??

Is not supposed that with -net="host" the container shares the network of the host, then why i can't access to http://localhost:8090 with --net="host"

  • Possible duplicate of [What does --net=host option in Docker command really do?](https://stackoverflow.com/questions/43316376/what-does-net-host-option-in-docker-command-really-do) – lvthillo Apr 03 '18 at 13:49
  • Did you open the firewall manually for 8090? – lvthillo Apr 03 '18 at 13:50

1 Answers1

4

This is not what --net=host does.

In your first example; you are mapping the ports of the container to your host - which allows you to via the services of the container.

In the second example; you remove the -p option so no ports are now mapped.

What the --net=host does - is allows your container to view port on the host machine as if they were local to the container. So say you had a database running on port 5000 of your host machine, and it was not in a Docker container - you would be able to access this on the container via localhost:5000. (Note - there are some caveats to this; such as Docker for Mac would actually need docker.for.mac.localhost)

TJ Biddle
  • 6,024
  • 6
  • 40
  • 47
  • 1
    This answer is not correct, you are right that without the -p no ports are mapped, but as the docker container with --net=host now runs on the host network, there is no need to map the port and actually, if you use both --net=host and -p port:port you get a log message which explains that -p has no effect when --net=host is used. So normally OP should be able to connect to localhost:8090 and there must be another reason why it is not working. – maxeh Jan 09 '23 at 16:57