四种网络类型:

None:不为容器配置任何网络功能,--net=none
Container:与另一个运行中的容器共享Network Namespace,--net=container:containerID(K8S)
Host:与宿主机共享Network Namespace,--net=host
Bridge:Docker设计的NAT网络模型

下面分别讲解下:

none:

[root@docker1 centos_zabbix]# docker run -it --network none centos6.9_ssh:v2 /bin/bash
[root@6ae597015723 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> 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

  这种没有ip,可以在容器里随便玩,没有多大意义

container类型的网络:

[root@1c21de6080bb html]# [root@docker1 centos_zabbix]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS      PORTS               NAMES
1c21de6080bb        centos6.9_nginx_ssh:v3   "/bin/bash"         26 seconds ago      Up 25 seconds                          vigorous_payne
[root@docker1 centos_zabbix]# docker run -it --network container:1c21de6080bb centos6.9_nginx_ssh:v4 /bin/bash

共用了主机名的ip地址

可以查看属性
[root@docker1 centos_zabbix]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS      PORTS               NAMES
41c457f6b89d        centos6.9_nginx_ssh:v4   "/bin/bash"         5 minutes ago       Up 5 minutes                          loving_curie
1c21de6080bb        centos6.9_nginx_ssh:v3   "/bin/bash"         6 minutes ago       Up 6 minutes                          vigorous_payne

[root@docker1 centos_zabbix]# docker inspect 41c457f6b89d|grep -i network
            "NetworkMode": "container:1c21de6080bb8c83e800fa8b5fb15782a18bb61ab292050ca1172fbb866b1afc",
        "NetworkSettings": {
            "Networks": {}

 要注意的是这种公用container类型的端口是谁先用某个端口,谁就占了,后来的没法使用这个端口

host类型

[root@docker1 centos_zabbix]# docker run -it --network=host centos6.9_nginx_ssh:v2 /bin/bash

  这种就是和宿主机共用端口,谁先占谁用,但是性能最高,所以我们在构建镜像的时候使用了这个参数,就是为了统一网络类型,提高构建镜像的效率

05-29 00:29