Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (2.3k points)

Hey there, I am new to the field of Docker, I am right now studying about docker networks. But I am having trouble understanding what exactly is the use of the --net=host option. There are a lot of confusing explanations out there. Can this be used to access an application running inside a docker container without actually specifying the port?

Fo example : What if I am running a web based application on the port 8080, the way to access it would be on <container IP>/WEBAPP. but i am still having trouble understanding what does --net=host flag do?

1 Answer

0 votes
by (6.9k points)

So whenever you install docker you create 3 default networks :

  • Bridge Network
  • Null Network
  • Host Network

Any container that is started will by default join the Default bridge network.

Let's take an example to explain this. 

So i have a nginx container running inside the default bridge network. I'll start up an image of Node and expose it at port 3000 in the dockerfile.

$ docker run -d node

Once the container starts it will be exposed at the port 3000 but only inside the default bridge network, which means that nginx can access this container but only at the port 3000 as they are both in the same network.

If the container needs to be accessed from outside( Host Server ) then it has to mapped to the host server using the -p flag like given below:

$ docker run -p 3000:3000 Node

 This allows all communication to the container from the port 3000 on the host server and port 3000 on the container.

Now instead you could use Host Network which does not isolate the container and instead actually makes it so that when you access you Host Server  you get access to the container attached to the host network.

for example if the our node container was attached to the Host network. you could access the container at the IP of you host server.

Hope this helps :)


If you are still having a bit of trouble you can always try out docker training courses or check out this docker tutorial

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...