四十四、Docker-网络模式

1、概述

Docker默认提供了一个隔离的内网环境,启动时会建立一个docker0的虚拟网卡,每个容器都是连接到docker0网卡上的,docker0的ip为172.17.0.1。
基于Docker run创建Docker容器时,可以使用–net选项指定容器的网络模式,Docker默认有以下四种网络模式:

host模式,使用–net=host指定;
container模式,使用–net=container:NAME_or_ID指定;
none模式,使用–net=none指定;
bridge模式,使用–net=bridge指定,默认设置;

2、Host模式详解

默认Docker容器运行会分配独立的Network Namespace隔离子系统,基于host模式,容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace,容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。网络通信性能较好,不需要进行网络转发,即不存在网络损耗,但是和宿主机之间不好区分。

验证:

[root@localhost ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos7-ssh                         latest              45ef6218ff65        46 hours ago        672 MB
nginx                               latest              5a3221f0137b        7 months ago        126 MB
docker.io/ansible/centos7-ansible   latest              688353a31fde        3 years ago         447 MB
[root@localhost ~]# docker run -itd --net=host centos7-ssh:latest
a465e94ef9e1ac4ecf0e9b8ec569b55b2ad4ffa2f39fc76dfab443bae4830624
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS               NAMES
a465e94ef9e1        centos7-ssh:latest   "/bin/sh -c /usr/s..."   About a minute ago   Up About a minute                       admiring_bohr
[root@localhost ~]# docker exec -it a465e94ef9e1
[root@localhost ~]# docker exec -it a465e94ef9e1 /bin/bash
# 共享宿主机网络
[root@localhost ansible]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:8aff:fedb:f65  prefixlen 64  scopeid 0x20<link>
        ether 02:42:8a:db:0f:65  txqueuelen 0  (Ethernet)
        RX packets 30  bytes 2064 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.151  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::773:49d0:5ac7:b300  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 1480  bytes 161740 (157.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 982  bytes 139835 (136.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 宿主机22端口被占用,修改容器端口以保证不冲突
[root@localhost ansible]# vi /etc/ssh/sshd_config 

port=6022

[root@localhost ansible]# /usr/sbin/sshd
[root@localhost ansible]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:6022            0.0.0.0:*               LISTEN      50/sshd             
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -                   
tcp6       0      0 :::6022                 :::*                    LISTEN      50/sshd   

3、Container模式详解

Container模式指定新创建的容器和已经存在的一个容器共享一个Network Namespace,而不是和宿主机共享。即新创建的容器不会创建自己的网卡,配置自己的IP,而是和一个指定的容器共享IP、端口范围等。同样两个容器除了网络方面相同之外,其他的如文件系统、进程列表等还是隔离的。

验证:

[root@localhost ~]# docker ps -aq|xargs docker rm -f
a465e94ef9e1
# 启动第一个容器
[root@localhost ~]# docker run -itd centos7-ssh
2908c6be2ba68a5016d812fff608101aa28cfa250af31240bef596f4b624e05e
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
2908c6be2ba6        centos7-ssh         "/bin/sh -c /usr/s..."   9 seconds ago       Up 8 seconds        22/tcp, 80/tcp      festive_visvesvaraya
[root@localhost ~]# docker inspect 2908c6be2ba6 |grep -i ipaddr|tail -1
                    "IPAddress": "172.17.0.2",

# 启动第二个容器,和第一个容器IP相同
[root@localhost ~]# docker run -itd --net=container:2908c6be2ba6 centos7-ssh
8e3e103b5756513a7c4471613c50755e053a7619586d062988f13bc411923342
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8e3e103b5756        centos7-ssh         "/bin/sh -c /usr/s..."   4 seconds ago       Up 3 seconds                            mystifying_turing
2908c6be2ba6        centos7-ssh         "/bin/sh -c /usr/s..."   3 minutes ago       Up 3 minutes        22/tcp, 80/tcp      festive_visvesvaraya
[root@localhost ~]# docker exec -it 8e3e103b5756 /bin/bash
[root@2908c6be2ba6 ansible]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

4、None模式详解

None模式与其他的模式都不同,如果处于None模式,Docker容器拥有自己的Network Namespace,但是并不为Docker容器进行任何网络配置。也就是说该Docker容器没有网卡、IP、路由等信息,需要手工为Docker容器添加网卡、配置IP等,使用Pipework工具为Docker容器指定IP等信息。

验证:

# none模式启动后容器没有IP
[root@localhost ~]# docker run -itd --net=none centos7-ssh
118d674eed26031fe50d2e8c3884ff09b273f6a34806941db16b4f186e98bf1a
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
118d674eed26        centos7-ssh         "/bin/sh -c /usr/s..."   5 seconds ago       Up 5 seconds                            flamboyant_poitras
[root@localhost ~]# docker exec -it 118d674eed26 /bin/bash
[root@118d674eed26 ansible]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

自定义容器 IP

# 下载 pipework 脚本
[root@localhost ~]# git clone https://github.com/jpetazzo/pipework
[root@localhost ~]# mv pipework /usr/local/sbin
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
ce0886620956        centos7-ssh         "/bin/sh -c /usr/s..."   2 seconds ago       Up 1 second         22/tcp, 80/tcp      hardcore_saha
# 配置 IP ,pipework + 桥接网卡 + 容器ID + 定义的IP/掩码位数@网关(可以直接配置为物理机网关)
[root@localhost ~]# pipework br0 ce0886620956 10.0.0.159/24@10.0.0.2
[root@localhost ~]# docker exec -it ce0886620956 /bin/bash
[root@ce0886620956 ansible]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.2        0.0.0.0         UG    0      0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

[root@ce0886620956 ansible]# ifconfig

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.159  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::c4f4:2cff:fe25:d339  prefixlen 64  scopeid 0x20<link>
        ether c6:f4:2c:25:d3:39  txqueuelen 1000  (Ethernet)
        RX packets 17  bytes 1989 (1.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1094 (1.0 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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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


5、Bridge模式详解

Bridge模式是Docker默认的网络模式,该模式会为每一个容器分配Network Namespace、设置IP、路由等配置,默认会将Docker容器连接到一个虚拟网桥交换机Docker0上。
桥接模式拓扑图:
在这里插入图片描述
在这里插入图片描述
Docker Bridge创建过程:

  1. 首先宿主机上创建一对虚拟网卡veth pair设备,veth设备总是成对出现的,组成了一个数据的通道,数据从一个设备进入,就会从另一个设备出来,veth设备常用来连接两个网络设备。
  2. Docker将veth pair设备的一端放在新创建的容器中,并命名为eth0,然后将另一端放在宿主机中,以vethxxx这样类似的名字命名,并将这个网络设备加入到docker0网桥中,可以通过brctl show命令查看。
  3. 从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。
  4. 此时容器IP与宿主机能够通信,宿主机也可以访问容器中的IP地址,在Bridge模式下,连在同一网桥上的容器之间可以相互通信,同时容器也可以访问外网,但是其他物理机不能访问docker容器IP,需要通过NAT将容器IP的port映射为宿主机的IP和port。

验证:

[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:59:da:d5:8c  txqueuelen 0  (Ethernet)
        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

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.151  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::773:49d0:5ac7:b300  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 247  bytes 33738 (32.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 176  bytes 20329 (19.8 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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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@localhost ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos7-ssh                         latest              45ef6218ff65        4 days ago          672 MB
nginx                               latest              5a3221f0137b        7 months ago        126 MB
docker.io/ansible/centos7-ansible   latest              688353a31fde        3 years ago         447 MB
[root@localhost ~]# docker run -itd centos7-ssh
31f922e8b93dde32d3ee793fa881ba673e6aea9bcfed244dc6f6adf90f1ff127

# 容器启动后多出 veth8ffe1a9 网卡

[root@localhost ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:59ff:feda:d58c  prefixlen 64  scopeid 0x20<link>
        ether 02:42:59:da:d5:8c  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 432 (432.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.151  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::773:49d0:5ac7:b300  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 1217  bytes 159012 (155.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 757  bytes 92813 (90.6 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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

veth8ffe1a9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::eca7:5dff:fea7:5882  prefixlen 64  scopeid 0x20<link>
        ether ee:a7:5d:a7:58:82  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 516 (516.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1032 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 查看docker 0 网桥,多出 veth8ffe1a9 网卡
[root@localhost ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.024259dad58c	no		veth8ffe1a9

# 容器中存在 eth0 网卡
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
31f922e8b93d        centos7-ssh         "/bin/sh -c /usr/s..."   About a minute ago   Up About a minute   22/tcp, 80/tcp      jolly_hamilton
[root@localhost ~]# docker exec -it 31f922e8b93d /bin/bash
[root@31f922e8b93d ansible]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:01  txqueuelen 0  (Ethernet)
        RX packets 16  bytes 1312 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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
        
# 容器网关是 docker0
[root@31f922e8b93d ansible]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

容器上网先发送到网关docker0,docker0与物理网卡桥接,最终通过物理网卡实现上网。如下:

[root@31f922e8b93d ansible]# traceroute www.baidu.com
traceroute to www.baidu.com (36.152.44.96), 30 hops max, 60 byte packets
 1  gateway (172.17.0.1)  0.066 ms  0.037 ms  0.080 ms
 2  10.0.0.2 (10.0.0.2)  0.354 ms  0.234 ms  0.304 ms

# 如何修改 docker0 IP 
# docker-ce 版本修改/usr/lib/systemd/system/docker.service,ExecStart 后面加上 -b-br0
[root@localhost ~]# vim /etc/sysconfig/docker-network 

DOCKER_NETWORK_OPTIONS="--bip=172.17.3.3/16"

[root@localhost ~]# systemctl restart docker
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.3.3  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:59:da:d5:8c  txqueuelen 0  (Ethernet)
        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

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.151  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::773:49d0:5ac7:b300  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 546  bytes 70483 (68.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 358  bytes 47206 (46.0 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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

自定义桥接网卡br0,让容器直接桥接物理网卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 /tmp/
[root@localhost network-scripts]# vim ifcfg-ens33

# 添加
BRIDGE=“br0"

[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0 

TYPE="Bridge"
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.149

# 停止并删除docker0
[root@localhost network-scripts]# ifconfig docker0 down
[root@localhost network-scripts]# brctl delbr docker0
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.149  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::204e:beff:fe1b:8a9a  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 518  bytes 43265 (42.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 309  bytes 41358 (40.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:85:4e:cc  txqueuelen 1000  (Ethernet)
        RX packets 1130  bytes 110243 (107.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 756  bytes 114731 (112.0 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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 332 (332.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 332 (332.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethb0d08fd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::1086:3cff:fed9:3bfd  prefixlen 64  scopeid 0x20<link>
        ether 12:86:3c:d9:3b:fd  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1312 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost network-scripts]# vim /etc/sysconfig/docker-network 

DOCKER_NETWORK_OPTIONS="-b=br0"

[root@localhost network-scripts]# systemctl restart docker
[root@localhost network-scripts]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos7-ssh                         latest              45ef6218ff65        4 days ago          672 MB
nginx                               latest              5a3221f0137b        7 months ago        126 MB
docker.io/ansible/centos7-ansible   latest              688353a31fde        3 years ago         447 MB
[root@localhost network-scripts]# docker run -itd centos7-ssh
cd3c9b1e5bc5fc14e7dc651488d5d035abf3627782ecbe1dd202fb02a87c3936
[root@localhost network-scripts]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cd3c9b1e5bc5        centos7-ssh         "/bin/sh -c /usr/s..."   2 minutes ago       Up 2 minutes        22/tcp, 80/tcp      elated_shaw
[root@localhost network-scripts]# docker exec -it cd3c9b1e5bc5 /bin/bash
# 容器网关为物理网卡IP
[root@cd3c9b1e5bc5 ansible]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.149      0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
# 容器分配到与物理网卡相同网段的空闲IP
[root@cd3c9b1e5bc5 ansible]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.1  netmask 255.0.0.0  broadcast 0.0.0.0
        inet6 fe80::42:aff:fe00:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:0a:00:00:01  txqueuelen 0  (Ethernet)
        RX packets 93  bytes 9501 (9.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值