Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between docker BRIDGE and HOST modes?

Can you give me one guide or graph to understand the difference?

The reason why I ask this question is I can't open website with the following method:

docker network create -d bridge mybridge 
docker run -d --net mybridge --name db redis 
docker run -d --net mybridge -e DB=db -p 8000:5000 --name web chrch/web 

But I can open website with the following method:

docker run --rm -d --network host --name my_nginx nginx

I use google cloud platform VM instance and install docker by myself.

like image 235
andy Avatar asked Jun 30 '19 13:06

andy


People also ask

What is the difference between bridge and host network in Docker?

Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks. host : For standalone containers, remove network isolation between the container and the Docker host, and use the host's networking directly.

What is Docker bridge mode?

In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

What is Docker host mode?

Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine. The application inside the container can be accessed using a port at the host's IP address (e.g., port 80).

What is the difference between Docker host and Docker container?

The Host is the machine managing the containers and images, where you actually installed Docker. Show activity on this post. Docker host is the machine where you installed the docker engine. the docker container can be compared with a simple process running on that same docker host.

What are the different network modes for Docker containers?

There are really 4 docker ‘provided’ network modes in which you can run containers… Bridge mode – This is the default, we saw how this worked in the last post with the containers being attached to the docker0 bridge. Host mode – The docker documentation claims that this mode does ‘not containerize the containers networking!’.

What is host mode and bridge mode in Docker?

Bridge mode – This is the default, we saw how this worked in the last post with the containers being attached to the docker0 bridge. Host mode – The docker documentation claims that this mode does ‘not containerize the containers networking!’.

What is a bridge network in Docker?

In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network. According to the docker documentation about host networking

What is the difference between Docker network and host network?

Network driver summary User-defined bridge networks are best when you need multiple containers to communicate on the same Docker host. Host networks are best when the network stack should not be isolated from the Docker host, but you want other aspects of the container to be isolated.


2 Answers

According to the docker documentation about bridge networking:

In terms of Docker, a bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

According to the docker documentation about host networking

If you use the host network driver for a container, that container’s network stack is not isolated from the Docker host. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application will be available on port 80 on the host’s IP address.

If you want to deploy multiple containers connected between them with a private internal network use bridge networking. If you want to deploy a container connected to the same network stack as the host (and access the same networks as the host) use host networking. If you simply want to publish some ports, run the container with the --publish or -p option, such as -p 8080:80.

like image 96
Ortomala Lokni Avatar answered Sep 30 '22 18:09

Ortomala Lokni


In your first example I'd expect the application to be reachable on the host's IP address at port 8000 (the remapped port), and in the second port 5000 (there is no remapping option with host networking). If there's some sort of configuration or firewalling issue preventing this from working you should address that, rather than hack around it with --net host.


Bridge networking is Docker's standard networking mode. You should prefer it if at all possible. There are, confusingly, two different modes of it, but the form you show with an explicit docker network create is a best practice and you should use it if at all possible. Host networking completely disables Docker's network isolation. It means containers see and use exactly the same network interfaces the host has available, without an intermediate NAT layer.

With bridge networking, you need the docker run -p option to make specific ports visible outside of Docker. As an operator you can remap ports, bind to specific interfaces on a multi-homed system, or simply decline to make a service visible to other hosts at all. The explicit docker network create form lets containers connect to each other using their docker run --name as host names. If you're running multiple application stacks on the same host, they can be partially isolated from each other by using separate networks. Each container has its own separate network space, and localhost means "this container". This mode is also an easy step to the networking models in multi-host systems like Docker Swarm or Kubernetes.

With host networking, none of the above works at all; you cannot use docker run --net host -p ... and you have no choice about where or how ports are exposed. You can't reach other containers, unless they're configured to publish ports themselves. Since you're using the host's network, localhost means the host's view of itself.

For all that it's frequently recommended in SO answers, --net host is rarely necessary. The two cases I can think of off hand are for a service that needs to interrogate the host's network stack (for instance, a service-discovery system like Consul needs to know every port the host is listening on to advertise that) or for a service that has a large or inconsistent set of ports it uses. If you're using --net host because you've hard-coded localhost in your application, you're better off making that configurable..

like image 34
David Maze Avatar answered Sep 30 '22 16:09

David Maze