Docker Networking using Host network
Host Network
描述:使用Docker host的网络作为container的网络。Docker容器使用namespace和cgroup技术,命名空间将会把资源(网络,存储,进程命名空间)进行隔离,使用这种方式,network将不会做隔离,直接使用host的网络。
How to use host network?
  • 查看目前docker host中的网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
2b65f9fa598b bridge bridge local
726cbc66240c host host local
f80e6afec2c4 none null local
  • 运行一个容器使用host network
[root@localhost ~]# docker run --rm -dit --network host --name my_nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
2a72cbf407d6: Pull complete
eccc107d7abd: Pull complete
76aa3935d77c: Pull complete
Digest: sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Status: Downloaded newer image for nginx:latest
9966b671587a12c53885898d0b1ff724aef8edec910055efa9f57042bb214ee6
  • 查看主机的网络信息
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens9: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.206/24 brd 192.168.122.255 scope global dynamic ens9
valid_lft 3264sec preferred_lft 3264sec
inet6 fe80::a473:1249:7f61:f5ea/64 scope link
valid_lft forever preferred_lft forever
3: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:97:1d:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.122.51/24 brd 192.168.122.255 scope global dynamic eth0
valid_lft 3179sec preferred_lft 3179sec
inet6 fe80::5054:ff:fe97:1d2e/64 scope link
valid_lft forever preferred_lft forever
4: docker0: mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:fb:73:ee:25 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever

[root@localhost ~]# netstat -pln|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16413/nginx: master

[root@localhost ~]# ps -ef|grep 16413
root 16413 16398 0 03:39 pts/0 00:00:00 nginx: master process nginx -g daemon off;
101 16434 16413 0 03:39 pts/0 00:00:00 nginx: worker process
  • 在主机上访问web
[root@localhost ~]# curl http://localhost:80

[root@localhost libvirt]# curl http://192.168.122.206:80
10-07 15:42