码头工人容器网络配置
docker容器网络配置
ip netns(网络名称空间)命令
[root @ Aimmi ~]# rpm-QA | grep iproute
iproute-5.9.0-4.el8.x86_64
[root@Aimmi ~]# ip netns帮助
Usage: ip网络列表/列出所有的名称空间
互联网协议(Internet Protocol的缩写)网络添加名称/添加名称空间
互联网协议(Internet Protocol的缩写)网络附加名称PID
互联网协议(Internet Protocol的缩写)网络集名称网络标识
ip [-all]网络删除[名称]
互联网协议(Internet Protocol的缩写)网络标识符
互联网协议(Internet Protocol的缩写)网络名称
ip [-all] netns exec [NAME] cmd.
互联网协议(Internet Protocol的缩写)网络监视器
ip网络列表-id[target-nsid PRIVATE-INT][nsid PRIVATE-INT]
NETNSID :=自动|正数-整数
创建Network Namespace
通过命令创建一个名为ns0的命名空间:
[root@Aimmi ~]# ip netns添加ns0
[root@Aimmi ~]# ip netns列表
ns0
通过命令删除一个名为ns0的命名空间:
[root@Aimmi ~]# ip netns删除ns0
如果相同名字的命名空间已经存在,命令会报无法创建命名空间文件"/var/run/netns/ns0":文件已存在的错误。
[root@Aimmi ~]# ls /var/run/netns
ns0
[root@Aimmi ~]# ip netns添加ns0
无法创建命名空间文件/var/run/netns/ns0':文件存在
错误示范:手动创建一个名称空间
[root @ Aimmi ~]# touch/var/run/netns/NS1
[root@Aimmi ~]# ip netns列表
错误:对等网络引用无效。
错误:对等网络引用无效。
ns1
ns0
对于每个网络命名空间来说,它会有自己独立的网卡、路由表、ARP表、iptables等和网络相关的资源。
操作Network Namespace
p命令提供了ip netns exec子命令可以在对应的网络命名空间中执行命令。
查看新创建网络命名空间的网卡信息
[root @ Aimmi ~]# IP netns exec ns0 IP a进入ns0这个名称空间里面去执行ip a这条命令
1: loo : LOOPBACK MTU 65536 qdisc noop状态向下组默认qlen 1000
链路/回送00:0000:0000:003360000336000 brd 0033600003360003360003360000003360000000336000000336000033600003、360003、3600003、3600003、36000003、3600003、3600003、3600003、360
可以看到,新创建的网络命名空间中会默认创建一个瞧回环网卡,此时网卡处于关闭状态。此时,尝试去砰该瞧回环网卡,会提示网络不可达
[root @ Aimmi ~]# IP netns exec ns0 ping 127。0 .0 .一
无法连接:网络
通过下面的命令启用瞧回环网卡:
[root@Aimmi ~]# ip netns exec ns0 ip链接设置瞧
[root @ Aimmi ~]# IP netns exec ns0 IP a
1: lo: LOOPBACK,UP,LOWER _ UP mtu 65536 qdisc noqueue状态未知的组默认qlen 1000
链路/回送00:0000:0000:003360000336000 brd 0033600003360003360003360000003360000000336000000336000033600003、360003、3600003、3600003、36000003、3600003、3600003、3600003、360
inet 127.0.0.1/8范围主机瞧
有效_lft永远首选_lft永远
inet6 :1/128范围荡妇,妓女
st
valid_lft forever preferred_lft forever
[root@Aimmi ~]# ip netns exec ns0 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.060 ms
转移设备
我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。
其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。
容器一运行,veth就会自动出现
[root@Aimmi ~]# docker run -it --rm busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
3aab638df1a9: Pull complete
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
4: eth0@if5: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@Aimmi ~]# ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ff:ac:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.145.188/24 brd 192.168.145.255 scope global dynamic noprefixroute ens33
valid_lft 1120sec preferred_lft 1120sec
inet6 fe80::20c:29ff:feff:ac9f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:78:55:12:e9 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
inet6 fe80::42:78ff:fe55:12e9/64 scope link
valid_lft forever preferred_lft forever
5: vethb2c2075@if4: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue master docker0 state UP group default
Error: Peer netns reference is invalid.
link/ether 4a:47:02:f4:f0:d4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::4847:2ff:fef4:f0d4/64 scope link
valid_lft forever preferred_lft forever //第五个
veth pair
veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。 引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来。
创建veth pair
[root@Aimmi ~]# ip link add type veth
[root@Aimmi ~]# ip link show
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:ff:ac:9f brd ff:ff:ff:ff:ff:ff
3: docker0: NO-CARRIER,BROADCAST,MULTICAST,UP mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:78:55:12:e9 brd ff:ff:ff:ff:ff:ff
6: veth0@veth1: BROADCAST,MULTICAST,M-DOWN mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether e6:e0:e1:28:47:a6 brd ff:ff:ff:ff:ff:ff
7: veth1@veth0: BROADCAST,MULTICAST,M-DOWN mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 7e:19:0b:08:a6:e5 brd ff:ff:ff:ff:ff:ff
可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。
实现Network Namespace间通信
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为ns0的 Network Namespace,下面再创建一个信息Network Namespace,命名为ns1
[root@Aimmi ~]# ip netns add ns1
[root@Aimmi ~]# ip netns list
ns1
ns0
然后我们将veth0加入到ns0,将veth1加入到ns1
[root@Aimmi ~]# ip link set veth0 netns ns0
[root@Aimmi ~]# ip netns exec ns0 ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
6: veth0@if7: BROADCAST,MULTICAST mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether e6:e0:e1:28:47:a6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@Aimmi ~]# ip link set veth1 netns ns1
[root@Aimmi ~]# ip netns exec ns1 ip a
1: lo: LOOPBACK mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
7: veth1@if6: BROADCAST,MULTICAST mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 7e:19:0b:08:a6:e5 brd ff:ff:ff:ff:ff:ff link-netns ns0
然后我们分别为这对veth pair配置上ip地址,并启用它们
[root@Aimmi ~]# ip netns exec ns0 ip link set veth0 up
[root@Aimmi ~]# ip netns exec ns0 ip addr add 10.0.0.1/24 dev veth0
[root@Aimmi ~]# ip netns exec ns1 ip link set lo up
[root@Aimmi ~]# ip netns exec ns1 ip link set veth1 up
[root@Aimmi ~]# ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1
查看这对veth pair的状态
[root@Aimmi ~]# ip netns exec ns0 ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
6: veth0@if7: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether e6:e0:e1:28:47:a6 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 10.0.0.1/24 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::e4e0:e1ff:fe28:47a6/64 scope link
valid_lft forever preferred_lft forever
[root@Aimmi ~]# ip netns exec ns1 ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
7: veth1@if6: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 7e:19:0b:08:a6:e5 brd ff:ff:ff:ff:ff:ff link-netns ns0
inet 10.0.0.2/24 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::7c19:bff:fe08:a6e5/64 scope link
valid_lft forever preferred_lft forever
从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址:
[root@Aimmi ~]# ip netns exec ns1 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.041 ms
veth设备重命名
[root@Aimmi ~]# ip netns exec ns0 ip link set veth0 down
[root@Aimmi ~]# ip netns exec ns0 ip link set dev veth0 name eth0
[root@Aimmi ~]# ip netns exec ns0 ip link set eth0 up
[root@Aimmi ~]# ip netns exec ns0 ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
6: eth0@if7: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether e6:e0:e1:28:47:a6 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 10.0.0.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::e4e0:e1ff:fe28:47a6/64 scope link
valid_lft forever preferred_lft forever
[root@Aimmi ~]# ip netns exec ns1 ip link set veth1 down
[root@Aimmi ~]# ip netns exec ns1 ip link set dev veth1 name eth0
[root@Aimmi ~]# ip netns exec ns1 ip link set eth0 up
[root@Aimmi ~]# ip netns exec ns1 ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
7: eth0@if6: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 7e:19:0b:08:a6:e5 brd ff:ff:ff:ff:ff:ff link-netns ns0
inet 10.0.0.2/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::7c19:bff:fe08:a6e5/64 scope link
valid_lft forever preferred_lft forever
[root@Aimmi ~]# ip netns exec ns1 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.080 ms
四种网络模式配置
bridge模式配置
[root@Aimmi ~]# docker run -it --rm busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
8: eth0@if9: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
[root@Aimmi ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
在创建容器时添加--network bridge与不加--network选项效果是一致的
[root@Aimmi ~]# docker run -it --rm --network bridge busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
10: eth0@if11: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
none模式配置
不用来通信,只用来做一些简单的操作
[root@Aimmi ~]# docker run -it --rm --network none busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
/ # exit
container模式配置
启动第一个容器
[root@Aimmi ~]# docker run -it --rm busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
12: eth0@if13: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
启动第二个容器
[root@Aimmi ~]# docker run -it --rm busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
14: eth0@if15: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
可以看到第二个容器的IP地址是172.17.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使第二个容器的IP与第一个容器的IP一致,也即共享IP,但不共享文件系统。
前两个不要退出 重新开个页面
[root@Aimmi ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bba83a5b7c03 busybox "sh" 17 seconds ago Up 16 seconds thirsty_franklin
f9fc77e4709f busybox "sh" 4 minutes ago Up 4 minutes sharp_sanderson
[root@Aimmi ~]# docker run -it --rm --network container:f9fc77e4709f busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
12: eth0@if13: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # hostname
f9fc77e4709f
此时我们在第一个容器上创建一个目录
/ # mkdir /tmp/data
/ # ls /tmp
data
到第二个容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
/ # ls
bin dev etc home proc root sys tmp usr var
在第二个容器上部署一个站点
ls /var/www/
/ # echo 'test page' /var/www/index.html
/ # /bin/httpd/ -f -h /var/www/
sh: /bin/httpd/: not found
/ # /bin/httpd -f -h /var/www/
[root@Aimmi ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3342de98128c busybox "sh" 2 minutes ago Up 2 minutes wizardly_
brahmagupta
bba83a5b7c03 busybox "sh" 4 minutes ago Up 4 minutes thirsty_f
ranklin
f9fc77e4709f busybox "sh" 7 minutes ago Up 7 minutes sharp_san
derson
[root@Aimmi ~]# docker exec -it 3342de98128c /bin/sh
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
在第一个容器上用本地地址去访问此站点
/ # wget -O - -q 127.0.0.1
test page
由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
host模式配置
启动容器时直接指明模式为host
[root@Aimmi ~]# docker run -it --rm --network host busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc fq_codel qlen 1000
link/ether 00:0c:29:ff:ac:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.145.188/24 brd 192.168.145.255 scope global dynamic noprefixroute ens33
valid_lft 968sec preferred_lft 968sec
inet6 fe80::20c:29ff:feff:ac9f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: NO-CARRIER,BROADCAST,MULTICAST,UP mtu 1500 qdisc noqueue
link/ether 02:42:78:55:12:e9 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
inet6 fe80::42:78ff:fe55:12e9/64 scope link
valid_lft forever preferred_lft forever
/ # ip link show
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc fq_codel qlen 1000
link/ether 00:0c:29:ff:ac:9f brd ff:ff:ff:ff:ff:ff
3: docker0: NO-CARRIER,BROADCAST,MULTICAST,UP mtu 1500 qdisc noqueue
link/ether 02:42:78:55:12:e9 brd ff:ff:ff:ff:ff:ff
此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
[root@Aimmi ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
/ # echo 'hello world' /var/www/index.html
/ # /bin/httpd -f -h /var/www/
[root@Aimmi ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 9 *:80
容器的常用操作
查看容器的主机名
[root@Aimmi ~]# docker run -it --rm busybox
/ # hostname
1d183d992db1
在容器启动时注入主机名
[root@Aimmi ~]# docker run -it --rm --hostname zs busybox
/ # hostname
zs
/ # cat /etc/hostname
zs
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 zs
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.145.2
/ # ping www.baidu.com
PING www.baidu.com (14.215.177.38): 56 data bytes
64 bytes from 14.215.177.38: seq=0 ttl=127 time=31.057 ms
64 bytes from 14.215.177.38: seq=1 ttl=127 time=39.754 ms
64 bytes from 14.215.177.38: seq=2 ttl=127 time=25.531 ms
手动指定容器要使用的DNS
[root@Aimmi ~]# docker run -it --rm --hostname zs --dns 114.114.114.114 busybox
/ # hostname
zs
/ # cat /etc/resolv.conf
search localdomain
nameserver 114.114.114.114
/ # nslookup -type=a www.baidu.com
Server: 114.114.114.114
Address: 114.114.114.114:53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com
Name: www.a.shifen.com
Address: 14.215.177.38
Name: www.a.shifen.com
Address: 14.215.177.39
手动往/etc/hosts文件中注入主机名到IP地址的映射
[root@Aimmi ~]# docker run -it --rm --hostname zs --dns 114.114.114.114 --add-host zs1:172.17.0.3 busybox
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 zs1
172.17.0.2 zs
开放容器端口
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-
-p选项的使用格式:
-p containerPort
将指定的容器端口映射至主机所有地址的一个动态端口
-p hostPort:containerPort
将容器端口containerPort映射至指定的主机端口hostPort
-p ip::containerPort
将指定的容器端口containerPort映射至主机指定ip的动态端口
-p ip:hostPort:containerPort
将指定的容器端口containerPort映射至主机指定ip的端口hostPort
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
第一种 - p 的使用方式
[root@Aimmi ~]# docker run -d --name web --rm -p 80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e5ae68f74026: Pull complete
21e0df283cd6: Pull complete
ed835de16acd: Pull complete
881ff011f1c9: Pull complete
77700c52c969: Pull complete
44be98c0fab6: Pull complete
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
ed8bf7174213a1195fd8993b0d48a18817727d61a9be715f2fc431b92d35441e
[root@Aimmi ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed8bf7174213 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:49153-80/tcp, :::49153-80/tcp web
以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上
[root@Aimmi ~]# docker port web
80/tcp - 0.0.0.0:49153
80/tcp - :::49153
由此可见,容器的80端口被暴露到了宿主机的32769端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
[root@Aimmi ~]# curl http://127.0.0.1:49153
!DOCTYPE html
html
head
titleWelcome to nginx!/title
style
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
/style
/head
body
h1Welcome to nginx!/h1
pIf you see this page, the nginx web server is successfully installed and
working. Further configuration is required./p
pFor online documentation and support please refer to
a href="http://nginx.org/"nginx.org/a.br/
Commercial support is available at
a href="http://nginx.com/"nginx.com/a./p
pemThank you for using nginx./em/p
/body
/html
第二种 - p 的方式:
将容器端口映射到宿主机的指定端口
[root@Aimmi ~]# docker run -d --name web --rm -p 80:80 nginx
ed8bf7174213a1195fd8993b0d48a18817727d61a9be715f2fc431b92d35441e
[root@Aimmi ~]# docker port web
80/tcp - 0.0.0.0:80
80/tcp - :::80
iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
第三种 - p 的方式:
将容器端口映射到指定IP的随机端口
[root@Aimmi ~]# docker run --name web --rm -p 192.168.145.188::80 nginx
在另一个终端上查看端口映射情况
[root@Aimmi ~]# docker port web
80/tcp - 192.168.145.188:49153
第四种 - p 的方式:
将指定的容器端口映射至主机指定的端口
[root@Aimmi ~]# docker run -d --name web --rm -p 127.0.0.1:80:80 nginx
ed8bf7174213a1195fd8993b0d48a18817727d61a9be715f2fc431b92d35441e
在另一个终端上查看端口映射情况
[root@Aimmi ~]# docker port web
80/tcp - 127.0.0.1:80
自定义docker0桥的网络属性信息
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件
官方文档相关配置 https://docs.docker.com/network/bridge/
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件
[root@Aimmi ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
"big": "192.168.145.188"
}
[root@Aimmi ~]# systemctl restart docker
[root@Aimmi ~]# ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:a8:8f:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.47.161/24 brd 192.168.47.255 scope global noprefixroute dynamic ens33
valid_lft 1169sec preferred_lft 1169sec
inet6 fe80::c7db:4c9f:7bcc:55f5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: NO-CARRIER,BROADCAST,MULTICAST,UP mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:5b:66:73:8e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 192.168.1.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:5bff:fe66:738e/64 scope link
valid_lft forever preferred_lft forever
核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出。
docker远程连接(不常用)
dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件
docker创建自定义桥
创建一个额外的自定义桥,区别于docker0
[root@Aimmi ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
bfef3a5a370d bridge bridge local
6ceb6009fa51 host host local
722f6ab96f26 none null local
[root@Aimmi ~]# docker network create -d bridge --subnet "192.168.2.0/24" --gateway "192.168.2.1" br0
a298744eb8b168ee11c122a301dfd44d699c9cca7bfb9177944aae51fa7cef0e
[root@Aimmi ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a298744eb8b1 br0 bridge local
bfef3a5a370d bridge bridge local
6ceb6009fa51 host host local
722f6ab96f26 none null local
使用新创建的自定义桥来创建容器:
[root@Aimmi ~]# docker run -it --rm --network br0 busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
23: eth0@if24: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:02:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.2/24 brd 192.168.2.255 scope global eth0
valid_lft forever preferred_lft forever
再创建一个容器,使用默认的bridge桥:
[root@Aimmi ~]# docker run -it --rm busybox
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
25: eth0@if26: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
试想一下,此时的b2与b1能否互相通信如果不能该如何实现通信
/ # hostname
1d183d992db1
[root@Aimmi ~]# docker network connect br0 bb1a030ba094 1d183d992db1
/ # ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue 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
25: eth0@if26: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
27: eth1@if28: BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:02:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.3/24 brd 192.168.2.255 scope global eth1
valid_lft forever preferred_lft forever
/ # ping 192.168.2.2
PING 192.168.2.2 (192.168.2.2): 56 data bytes
64 bytes from 192.168.2.2: seq=0 ttl=64 time=0.204 ms
64 bytes from 192.168.2.2: seq=1 ttl=64 time=0.069 ms
64 bytes from 192.168.2.2: seq=2 ttl=64 time=0.079 ms
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/135144.html