我们在使用docker run创建Docker容器时,可以用 --net 参数指定容器的网络模式
Docker有以下4种网络模式:
1、Bridge 模式:是 Docker 的默认网络模式,可以使用docker run –net=bridge
指定;
[root@wzx-docker ~]# docker run -it --rm alpine:3.10.3 /bin/sh
/ # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
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
32: eth0@if33: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:c0:a8:04:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.2/24 brd 192.168.4.255 scope global eth0
valid_lft forever preferred_lft forever
2、None模式,使用docker run --net=none
[root@wzx-docker ~]# docker run -it --rm --net=none alpine:3.10.3 /bin/sh
/ # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
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
3、Host(使用宿主机的网络) 使用docker run --net=host
[root@wzx-docker ~]# docker run -it --rm --net=host alpine:3.10.3 /bin/sh
/ # ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:16:3e:2c:31:be brd ff:ff:ff:ff:ff:ff
inet 172.25.45.191/18 brd 172.25.63.255 scope global dynamic eth0
valid_lft 313790220sec preferred_lft 313790220sec
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:fc:af:27:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.20/24 brd 192.168.4.255 scope global docker0
valid_lft forever preferred_lft forever
13: vethb3612e9@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 state UP
link/ether fa:d1:94:05:0f:22 brd ff:ff:ff:ff:ff:ff
4、Container 模式 docker run --net=container:${container id}
[root@wzx-docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38e36c448e12 zhenxu1995/nginx:v1.2 "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp mynginx
0e2115c02cb4 zhenxu1995/httpd:test "httpd -D FOREGROUND" 4 days ago Up 4 days 0.0.0.0:83->80/tcp myhttpd
f7033445b2bb hello-world "/hello" 2 weeks ago Exited (0) 2 weeks ago cool_benz
[root@wzx-docker ~]# docker exec -it 38e36c448e12 /bin/bash
root@38e36c448e12:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.2 netmask 255.255.255.0 broadcast 192.168.4.255
ether 02:42:c0:a8:04:02 txqueuelen 0 (Ethernet)
RX packets 11085 bytes 18186380 (17.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10944 bytes 767446 (749.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
测试
[root@wzx-docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhenxu1995/nginx baidu a6df8dadb9b8 2 hours ago 133MB
zhenxu1995/nginx mynginx 83edcff7991f 7 hours ago 437MB
zhenxu1995/httpd test bce6d03b1a57 4 days ago 397MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
centos 7 eeb6ee3f44bd 6 months ago 204MB
zhenxu1995/nginx v1.2 81fae8882e35 8 months ago 133MB
alpine 3.10.3 965ea09ff2eb 2 years ago 5.55MB
[root@wzx-docker ~]# docker run -it --rm --name lhwl-wzx --net=container:38e36c448e12 zhenxu1995/nginx:baidu /bin/bash
root@38e36c448e12:/usr/share/nginx/html# apt-get update && apt-get install net-tools -y
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://security.debian.org/debian-security buster/updates/main amd64 Packages [316 kB]
Get:3 http://deb.debian.org/debian buster InRelease [122 kB]
Get:4 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7911 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [8796 B]
Fetched 8475 kB in 3min 27s (40.9 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
Need to get 248 kB of archives.
After this operation, 1002 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1 [248 kB]
Fetched 248 kB in 52s (4810 B/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 7638 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1_amd64.deb ...
Unpacking net-tools (1.60+git20180626.aebd88e-1) ...
Setting up net-tools (1.60+git20180626.aebd88e-1) ...
root@38e36c448e12:/usr/share/nginx/html# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.2 netmask 255.255.255.0 broadcast 192.168.4.255
ether 02:42:c0:a8:04:02 txqueuelen 0 (Ethernet)
RX packets 5085 bytes 9063150 (8.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4950 bytes 344257 (336.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0