I'm a little bit beginner to Docker. I couldn't find any clear description of what this option does in docker run command in deep and bit confused about it.
Can we use it to access the applications running on docker containers without specifying a port? As an example if I run a webapp deployed via a docker image in port 8080 by using option -p 8080:8080
in docker run command, I know I will have to access it on 8080 port on Docker containers ip /theWebAppName. But I cannot really think of a way how --net=host
option works.
After the docker installation you have 3 networks by default:
I'm trying to keep this simple. So if you start a container by default it will be created inside the bridge (docker0) network.
In the dockerfile of jenkins the ports
8080
and50000
are exposed. Those ports are opened for the container on its bridge network. So everything inside that bridge network can access the container on port8080
and50000
. Everything in the bridge network is in the private range of"Subnet": "172.17.0.0/16",
If you want to access them from the outside you have to map the ports with-p 8080:8080
. This will map the port of your container to the port of your real server (the host network). So accessing your server on8080
will route to your bridgenetwork on port8080
.Now you also have your host network. Which does not containerize the containers networking. So if you start a container in the host network it will look like this (it's the first one):
The difference is with the ports. Your container is now inside your host network. So if you open port
8080
on your host you will acces the container immediately.I've opened port
8080
in my firewall and when I'm now accesing my server on port8080
I'm accessing my jenkins. I think this blog is also useful to understand it better.