其他分享
首页 > 其他分享> > docker容器网络

docker容器网络

作者:互联网

docker容器网络

docker在安装后自动提供3种网络

 # docker network ls 查看
[root@node0 ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
ca3ffc857d6b   bridge    bridge    local
a3c6e9e82d4c   host      host      local
68e0702fcaa6   none      null      local

docker使用linux桥接,会虚拟一个docker容器网桥(docker0)

[root@node0 ~]# ip a|grep docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
19: vethb965cf0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
23: veth022ea6e@if22: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 

[root@node0 ~]# docker run -it --rm busybox
/ # 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

docker4种网络模式

网络模式 配置 说明
host --network host 容器和宿主机共享Network namespace
container --network container:NAME_OR_ID 容器和另外一个容器共享Network namespace
none --network none 容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair 和网桥连接,配置IP等
bridge --network bridge 默认模式

bridge模式

docker启动时创建docker0虚拟网桥,

从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。在主机上创建一对虚拟网卡veth pair设备,Docker将veth pair设备的一端放在新创建的容器中,并命名为eth0(容器的网卡),另一端放在主机中,以vethxxx这样类似的名字命名,并将这个网络设备加入到docker0网桥中。

[root@node0 ~]# docker run fxx013/web:v0.3
[root@node0 ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
ca3ffc857d6b   bridge    bridge    local
a3c6e9e82d4c   host      host      local
68e0702fcaa6   none      null      local
[root@node0 ~]# nmcli device 
DEVICE       TYPE      STATE      CONNECTION 
ens160       ethernet  connected  ens160     
docker0      bridge    connected  docker0    
vethf26accc  ethernet  unmanaged  --         
lo           loopback  unmanaged  --         

[root@node0 ~]# ip a|grep veth
31: vethf26accc@if30: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 

[root@node0 ~]# docker run -it --rm fxx013/web:v0.3
/ # ip a|grep eth0
18: eth0@if31: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0

如果容器希望外部访问能够访问到,可以通过映射容器端口到宿主主机(端口映射),即docker run创建容器时候通过 -p 或 -P 参数来启用,访问容器的时候就通过[宿主机端口]:[容器端口]访问容器。

docker run -p

//映射容器端口到宿主机端
[root@node0 ~]# docker run -it -p 80:80 --rm fxx013/web:v0.3
[root@node0 ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  
LISTEN  0       128            0.0.0.0:80          0.0.0.0:*     
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*     
LISTEN  0       128               [::]:22             [::]:*

[root@node0 ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS              PORTS                NAMES
c87b0434bf11   fxx013/web:v0.3   "/bin/httpd -f -h /w…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   gracious_pasteur
29258754a787   fxx013/web:v0.3   "/bin/httpd -f -h /w…"   14 minutes ago       Up 14 minutes                            kind_mclean
[root@node0 ~]# docker inspect c87b0434bf11
。。。
 "IPAddress": "172.17.0.3",
//主机访问
[root@node0 ~]# curl 172.17.0.3
hello world
//外部访问(如下图)

container模式

特点:

container模式如下图所示:

[root@node0 ~]# docker run -it --rm fxx013/web:v0.3
[root@node0 ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                NAMES
d78098f34642   fxx013/web:v0.3   "/bin/httpd -f -h /w…"   9 seconds ago    Up 8 seconds
[root@node0 ~]# docker inspect d78098f34642
...
 "IPAddress": "172.17.0.4",
 
[root@node0 ~]# docker run  -it --rm --network container:d78098f34642 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
34: eth0@if35: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

host模式

特点:

[root@node0 ~]# docker run  -it --rm --network host 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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq qlen 1000
    link/ether 00:0c:29:bb:6f:ff brd ff:ff:ff:ff:ff:ff
    inet 192.168.94.142/24 brd 192.168.94.255 scope global dynamic ens160
       valid_lft 1025sec preferred_lft 1025sec
    inet6 fe80::c566:2591:64e:9b7c/64 scope link 
       valid_lft forever preferred_lft forever
[root@node0 ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:bb:6f:ff brd ff:ff:ff:ff:ff:ff
    inet 192.168.94.142/24 brd 192.168.94.255 scope global dynamic noprefixroute ens160
       valid_lft 989sec preferred_lft 989sec
    inet6 fe80::c566:2591:64e:9b7c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

none模式

[root@node0 ~]# docker run  -it --rm --network none 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

标签:容器,00,网络,lft,ff,node0,docker,root
来源: https://www.cnblogs.com/fangxinxin/p/14466182.html