1

When I use the host mode to run mycontainer, it doesn't work,but the bridge mode works.I'd like to ask what's the different between the two modes?

Run with host mode: docker run --name=zhiwenyi --net=host -d [image]

Run with bridge mode:docker run --name=zhiwenyi -d -p 35229:35229 [image]

Dockerfile:

FROM java:8
VOLUME /data/log/upload
COPY target/upload_V6_20220722.jar upload.jar
EXPOSE 35229
RUN bash -c "touch /upload.jar"
ENTRYPOINT ["java","-Xmx512m","-Xms512m","-jar","-Duser.timezone=GMT+08","-Dfile.encoding=utf-8","upload.jar"]

In bridge mode, I send a post request to myip:35229/path ,it works well.

In host mode, the same request ,it shows me connection time out.

Supply:

  1. OS:centos 7.9
  2. Docker:20.10.17
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LInkerH
  • 36
  • 3
  • Related: [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); [What is the use of Docker 'host' and 'none' Networks?](https://stackoverflow.com/questions/41083328/what-is-the-use-of-docker-host-and-none-networks) – David Maze Jul 25 '22 at 09:28
  • You should not normally need `--net=host`, so if your second form works (either without a `--net` option at all, or `--net=something-you-created`) I'd use that without worrying about it. – David Maze Jul 25 '22 at 09:29
  • Thanks for your advice,I've learnd the two modes. General speaking, the programe can work in the two ways above.But it only works in `bridge mode`,what's wrong with `host mode`? – LInkerH Jul 25 '22 at 10:33
  • It disables Docker's core networking features; so you can't hide or remap a container port on the host, and you can't communicate between containers using container names as host names – David Maze Jul 25 '22 at 10:35
  • For example using `nginx`: 1. `docker run --name=nginx1 -d -p 80:80 nginx` 2. `docker run --name=nginx2 --net=host nginx`. I could have access the container on `ip:80`. But I can't acess the container with host mode,why? – LInkerH Jul 25 '22 at 10:54

1 Answers1

0

If you use host mode it disables the container's network namespace.

A bridge network in the context of Docker employs a software bridge to provide isolation from containers that are not linked to that bridge network while enabling communication between containers that are connected to the same bridge network.

The network stack of a container that uses the host network driver is not separated from the Docker host. For instance, if you utilize host networking and run a container that binds to port 80, the application for the container will be accessible on port 80 on the host's IP address.