docker网络

docker网络

一、docker网络

loopback:回环网卡、TCP/IP网卡是否生效

docker 0 :容器的网关,绑定物理网卡,负责做NAT地址转换、端口映射

docker 0 本身也是一种容器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iKgczV6D-1642329370645)(D:\北大\截图\2022\1月份\1.12\795a8a185f0c4aa79ab749b2381d34f5.png)]

veth对是成对出现的虚拟接口/网卡

作用是将两个不同的名称空间进行连接

二、docker四种网络模式

1、host模式

host容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的NetworkNamespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的I等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。

使用host模式的容器可以直接使用宿主机的IP地址与外界通信,容器内部的服务端口也可以使用宿主机的端口,不需要进行NAT,host最大的优势就是网络性能比较好,但是dockerhost 上已经使用的端口就不能再用了,网络的隔离性不好。

在这里插入图片描述

不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。与宿主机共享网络名称空间/网络协议栈,IP共享、端口范围共享

网络性能好,隔离性和安全性不好

网络之间的通讯可以使用container

2、container模式

创建的容器不会创建自己的网卡、设置IP等,而是和一个指定地容器共亨IP、端口范围

这个模式指定新创建的容器和已经存在的一个容器共享一个network namespace,而不是和宿主机共享,新创建的容器不会创建自己的网卡,配置自己的Ip,而是和一个指定地容器共享IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表还是隔离的。(☆☆两个容器的进程可以通过loo l网卡设备通信)

在这里插入图片描述

3、none模式

该模式关闭了容器的网络功能

这种网络模式下容器只有lo回环网口,没有其他的网卡。none模式可以在容器创建时通过—network=none参数指定

这种类型的网络无法联网,但是封闭的网络能很好的保证容器的安全性

在这里插入图片描述

4、bridge模式

此模式会为每一个容器分配、设置IP等,并将容器连接到一个docker虚拟网桥,通过dockero 网桥及iptables 的 nat表配置与宿主机通信

当Docker进程启动时,会在主机上创建一个名为dockerO的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上。虚拟网桥的工作方式和物理交换机类似,这样主机上的所有容器就通过交换机连在了一个二层网络中。

注:

docker中有几种网络模式,分别提供哪些功能Host container none bridge

1.Host :与宿主机共享网络名称空间/网络协议栈,IP共享、端口范围共享

2.Container:多个容器之间共享一个network namespaces,多个容器公用一个IP和端口范围

3.None :自闭空间,无网卡,无需网络连接

4.Bridge:桥接,默认模式,在不指定网络模式的情况下创建容器,默认使用此模式,通过veth对连接容器与dockero网桥,网桥分配给容器IP,同时docker o作为“局域网”内容器的网关,最后和宿主机网卡进行通讯,同时,通过IPtables规则将容器IP/port 映射出去,用于与宿主机网卡交互

三、docker自定义网络

1、查看网络模式列表

docker network ls

[root@localhost ~]#docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
209877f313b6   bridge    bridge    local
4e9ee3f5eca6   host      host      local
98a2c694c5ae   none      null      local

2、查看容器信息

包含配置、环境、网关、挂载、cmd等等信息

docker inspect 容器 ID

[root@localhost ~]#docker inspect 589d79c12b09
[
    {
        "Id": "589d79c12b093068b85f2e0cc4de7926d84bfeb29df48a300998252398f499d8",
        "Created": "2022-01-16T10:02:07.650411076Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "/bin/bash"
        ],
        "State": {
            "Status": "created",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "0001-01-01T00:00:00Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
。。。。。

3、指定分配容器IP地址

docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:7 /bin/bash
会报错,不能使用自己定义的IP地址,只能遵循它自定义的IP地址。状态是created

[root@localhost ~]#docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:7 /bin/bash 
1a1b67cfd408fb38d3e0754c67b813baf95d39e3c82aa52f1fce68cd6db2145a
docker: Error response from daemon: user specified IP address is supported on user defined networks only.

4、自定义网络固定IP

docker network create --subnet=172.18.0.0/16 mynetwork

docker run -itd --name test2 --network mynetwork --ip 172.18.0.85 centos:7 /bin/bash

[root@localhost ~]#docker network create --subnet=172.18.0.0/16 mynetwork
2821be21e60c65f263af725746e583a7df7ed9b1b3597e76525778c391b85ffe
[root@localhost ~]#docker run -itd --name test2 --network mynetwork --ip 172.18.0.85 centos:7 /bin/bash
76816b6e513cfba5464baece338c3274268ec24c8aa45ca12a0972798f22daae
[root@localhost ~]#docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
209877f313b6   bridge      bridge    local
4e9ee3f5eca6   host        host      local
2821be21e60c   mynetwork   bridge    local
98a2c694c5ae   none        null      local

5、暴露端口

-P:随机端口(随机端口范围49153–65535)

-p:自定义端口(宿主机端口:容器内端口)

5.1自定义端口

docker run -itd -p 333:80 nginx:latest /bin/bash

[root@localhost ~]#docker run -itd -p 333:80 nginx:latest /bin/bash
dca42ac2a063a56436b9ac8fca0eb7aceffae2d6f6aafbcf2eb8e8052b199622
[root@localhost ~]#docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                 NAMES
dca42ac2a063   nginx:latest   "/docker-entrypoint.…"   14 seconds ago   Up 12 seconds   0.0.0.0:333->80/tcp, :::333->80/tcp   fervent_gauss
76816b6e513c   centos:7       "/bin/bash"              4 minutes ago    Up 4 minutes                                          test2
1a1b67cfd408   centos:7       "/bin/bash"              10 minutes ago   Created                                               test1
589d79c12b09   nginx:latest   "/docker-entrypoint.…"   13 minutes ago   Created                                               eager_kepler
[root@localhost ~]#curl 192.168.223.106:333
curl: (7) Failed connect to 192.168.223.106:333; 拒绝连接

拒绝连接:进入容器开启服务

docker exec -it 容器ID /bin/bash -c nginx

[root@localhost ~]#docker exec -it dca42ac2a063 /bin/bash -c nginx
2022/01/16 10:18:59 [notice] 6#6: using the "epoll" event method
2022/01/16 10:18:59 [notice] 6#6: nginx/1.21.5
2022/01/16 10:18:59 [notice] 6#6: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/01/16 10:18:59 [notice] 6#6: OS: Linux 3.10.0-693.el7.x86_64
2022/01/16 10:18:59 [notice] 6#6: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/01/16 10:18:59 [notice] 12#12: start worker processes
2022/01/16 10:18:59 [notice] 12#12: start worker process 13
2022/01/16 10:18:59 [notice] 12#12: start worker process 14
2022/01/16 10:18:59 [notice] 12#12: start worker process 15
2022/01/16 10:18:59 [notice] 12#12: start worker process 16
[root@localhost ~]#curl 192.168.223.106:333
<!DOCTYPE html>
<html>
<head>
<title>Welcome 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>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For 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>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

5.2随机端口
[root@localhost ~]#docker run -itd -P --name nginx01 nginx:latest /bin/bash
03a8fa17408c239874d76640624d5e2f91255583788354334ab60b0d0afb6e8d
[root@localhost ~]#docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                     NAMES
03a8fa17408c   nginx:latest   "/docker-entrypoint.…"   19 seconds ago   Up 18 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   nginx01
dca42ac2a063   nginx:latest   "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   0.0.0.0:333->80/tcp, :::333->80/tcp       fervent_gauss
76816b6e513c   centos:7       "/bin/bash"              15 minutes ago   Up 15 minutes                                             test2
1a1b67cfd408   centos:7       "/bin/bash"              20 minutes ago   Created                                                   test1
589d79c12b09   nginx:latest   "/docker-entrypoint.…"   23 minutes ago   Created                                                   eager_kepler

##端口从49153开始
root@localhost ~]#docker exec -it 03a8fa17408c /bin/bash -c nginx
2022/01/16 10:28:56 [notice] 7#7: using the "epoll" event method
2022/01/16 10:28:56 [notice] 7#7: nginx/1.21.5
2022/01/16 10:28:56 [notice] 7#7: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/01/16 10:28:56 [notice] 7#7: OS: Linux 3.10.0-693.el7.x86_64
2022/01/16 10:28:56 [notice] 7#7: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/01/16 10:28:56 [notice] 13#13: start worker processes
2022/01/16 10:28:56 [notice] 13#13: start worker process 14
2022/01/16 10:28:56 [notice] 13#13: start worker process 15
2022/01/16 10:28:56 [notice] 13#13: start worker process 16
2022/01/16 10:28:56 [notice] 13#13: start worker process 17
[root@localhost ~]#curl 192.168.223.106:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome 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>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For 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>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

四、总结

Host : 与 宿主机共享网络名称空间/网络协议栈,IP 共享、端口范围共享

Container: 多个容器之间共享一个network namespaces,多个容器公用一个IP和端口范围

None : 自闭空间,无网卡,无需网络连接

Bridge: 桥接, 默认模式,在不指定网络模式的情况下创建容器,默认使用此模式, 通过Veth对连接容器与docker0网桥,网桥分配IP给容器,同时docker0作为“局域网”内容器的网关,最后和宿主机网卡进行通讯, 同时,通过IPtables规则将容器IP/port映射出去,用于与宿主机网卡交互
st : 与 宿主机共享网络名称空间/网络协议栈,IP 共享、端口范围共享

Container: 多个容器之间共享一个network namespaces,多个容器公用一个IP和端口范围

None : 自闭空间,无网卡,无需网络连接

Bridge: 桥接, 默认模式,在不指定网络模式的情况下创建容器,默认使用此模式, 通过Veth对连接容器与docker0网桥,网桥分配IP给容器,同时docker0作为“局域网”内容器的网关,最后和宿主机网卡进行通讯, 同时,通过IPtables规则将容器IP/port映射出去,用于与宿主机网卡交互

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值