需要安装sudo apt install bridge-utils
Docker 网络很重要,重要的,我们在上面学到的所有东西都依赖于网络才能工作。我们从两个方面来学习网络:
端口映射和网络模式 为什么先学端口映射呢? 在一台主机上学习网络,学习端口映射最简单,避免过多干扰。
2.4.1 端口映射详解
默认情况下,容器和宿主机之间网络是隔离的,我们可以通过端口映射的方式,将容器中的端口,映射到宿主机的
某个端口上。这样我们就可以通过宿主机的ip+port的方式来访问容器里的内容
Docker的端口映射
1、随机映射 -P(大写)—>一般情况下会先映射32768
2、指定映射 -p 宿主机ip:宿主机端口:容器端口
注意: 生产场景一般不使用随机映射,但是随机映射的好处就是由docker分配,端口不会冲突, 不管哪种映射都会
有所消耗,影响性能,因为涉及到映射的操作
2.4.2 随机映射实践
随机映射我们从两个方面来学习: 1、默认随机映射 2、指定主机随机映射
默认随机映射
#命令格式:
docker run -d -P [镜像名称]
#命令效果:
#先启动一个普通的nginx镜像
$ docker run -d nginx
#查看当前宿主机开放了哪些端口
$ netstat -tnulp
#启动一个默认随机映射的nginx镜像
$ docker run -d -P nginx
#查看当前宿主机开放了哪些端口
$ netstat -tnulp
这个是没有加-P的
chengpeng@chengpeng:~$ netstat -tnulp
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
激活Internet连接 (仅服务器)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.53:53 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:631 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN -
tcp6 0 0 ::1:6379 :::* LISTEN -
tcp6 0 0 :::139 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
tcp6 0 0 ::1:6010 :::* LISTEN -
tcp6 0 0 ::1:6011 :::* LISTEN -
tcp6 0 0 :::445 :::* LISTEN -
udp 0 0 0.0.0.0:5353 0.0.0.0:* -
udp 0 0 127.0.0.53:53 0.0.0.0:* -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 172.17.255.255:137 0.0.0.0:* -
udp 0 0 172.17.0.1:137 0.0.0.0:* -
udp 0 0 192.168.175.255:137 0.0.0.0:* -
udp 0 0 192.168.175.129:137 0.0.0.0:* -
udp 0 0 0.0.0.0:137 0.0.0.0:* -
udp 0 0 172.17.255.255:138 0.0.0.0:* -
udp 0 0 172.17.0.1:138 0.0.0.0:* -
udp 0 0 192.168.175.255:138 0.0.0.0:* -
udp 0 0 192.168.175.129:138 0.0.0.0:* -
udp 0 0 0.0.0.0:138 0.0.0.0:* -
udp 0 0 0.0.0.0:45436 0.0.0.0:* -
udp 0 0 0.0.0.0:631 0.0.0.0:* -
udp6 0 0 :::5353 :::* -
udp6 0 0 :::42027 :::* -
chengpeng@chengpeng:~$ docker run -d -P nginx
8e3fa64d2166dad267ca1b49b8b004c5a0654d7e9f8ef244aa6e6dd4f50f5991
这个是加了-P的情况下的多了一个32768
chengpeng@chengpeng:~$ netstat -tnulp
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
激活Internet连接 (仅服务器)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.53:53 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:631 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:32768 0.0.0.0:* LISTEN -
tcp6 0 0 ::1:6379 :::* LISTEN -
tcp6 0 0 :::139 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
tcp6 0 0 ::1:6010 :::* LISTEN -
tcp6 0 0 ::1:6011 :::* LISTEN -
tcp6 0 0 :::445 :::* LISTEN -
tcp6 0 0 :::32768 :::* LISTEN -
udp 0 0 0.0.0.0:5353 0.0.0.0:* -
udp 0 0 127.0.0.53:53 0.0.0.0:* -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 172.17.255.255:137 0.0.0.0:* -
udp 0 0 172.17.0.1:137 0.0.0.0:* -
udp 0 0 192.168.175.255:137 0.0.0.0:* -
udp 0 0 192.168.175.129:137 0.0.0.0:* -
udp 0 0 0.0.0.0:137 0.0.0.0:* -
udp 0 0 172.17.255.255:138 0.0.0.0:* -
udp 0 0 172.17.0.1:138 0.0.0.0:* -
udp 0 0 192.168.175.255:138 0.0.0.0:* -
udp 0 0 192.168.175.129:138 0.0.0.0:* -
udp 0 0 0.0.0.0:138 0.0.0.0:* -
udp 0 0 0.0.0.0:45436 0.0.0.0:* -
udp 0 0 0.0.0.0:631 0.0.0.0:* -
udp6 0 0 :::5353 :::* -
udp6 0 0 :::42027 :::* -
chengpeng@chengpeng:~$
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e3fa64d2166 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp angry_hellman
0b21309fe92b nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 80/tcp eloquent_buck
注意: 宿主机的32768被映射到容器的80端口 -P 自动绑定所有对外提供服务的容器端口,映射的端口将会从没有
使用的端口池中自动随机选择, 但是如果连续启动多个容器的话,则下一个容器的端口默认是当前容器占用端口号
+1
注意: 浏览器输入的格式是: docker容器宿主机的ip:容器映射的端口
#命令格式
:~$ docker run -d -p [宿主机ip]::[容器端口] --name [容器名称][镜像名称]
#命令效果
:~$ docker run -d -p 192.168.8.14::80 --name nginx-1 nginx
#检查效果
:~$ docker ps
chengpeng@chengpeng:~$ docker run -d -p 192.168.175.129::80 --name nginx11 nginx
78a6c9d190915cc679a15b886f7202ab0c69dcbe3b48a4d6bcf5b009df26e747
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78a6c9d19091 nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 192.168.175.129:32773->80/tcp nginx11
指定端口映射我们从二个方面来讲:
指定端口映射
指定多端口映射
指定端口映射
#命令格式:
docker run -d -p [宿主机ip]:[宿主机端口]:[容器端口] --name [容器名字][镜像名称]
#注意:
#如果不指定宿主机ip的话,默认使用 0.0.0.0,
#命令实践:
#现状我们在启动容器的时候,给容器指定一个访问的端口 1199
docker run -d -p 192.168.8.14:1199:80 --name nginx-2 nginx
#查看新容器ip
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}
{{end}}' 0ad3acfbfb76
#查看容器端口映射
docker ps
chengpeng@chengpeng:~$ docker run -d -p 192.168.175.129:10240:80 --name nginx22 nginx
13bf1f194764c6ad0847539183aa461be7d10fc7ec734c9f2340c4f665154b6a
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13bf1f194764 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 192.168.175.129:10240->80/tcp nginx22
#命令格式
docker run -d -p [宿主机端口1]:[容器端口1] -p [宿主机端口2]:[容器端口2] --name [容器名
称][镜像名称]
#开起多端口映射实践
docker run -d -p 520:443 -p 6666:80 --name nginx-3 nginx
#查看容器进程
docker ps
chengpeng@chengpeng:~$ docker run -d -p 10086:80 -p 10000:80 -p 520:1314 -p 18119:2046 --name nginx-4 nginx
a23f52f973f23d31b32be44cc21b587d0f1697a6218fbd0bfddb0378d9df856a
chengpeng@chengpeng:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a23f52f973f2 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:10000->80/tcp, 0.0.0.0:10086->80/tcp, :::10000->80/tcp, :::10086->80/tcp, 0.0.0.0:520->1314/tcp, :::520->1314/tcp, 0.0.0.0:18119->2046/tcp, :::18119->2046/tcp nginx-4
其中-p 520:1314 -p 18119:2046无法访问-->根本就没有业务
docker网络命令
#查看网络命令帮助
:~$ docker network help
。。。。。。
connect Connect a container to a network
#将一个容器连接到一个网络
create Create a network
#创建一个网络
disconnect Disconnect a container from a network
#从网络断开一个容器
inspect Display detailed information on one or more networks
#在一个或多个网络上显示详细信息
ls List networks
#网络列表
prune Remove all unused networks
#删除所有未使用的网络--->不建议使用-->删除以后docker用不了
rm Remove one or more networks
#删除一个或多个网络。
#查看当前主机网络
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
#网络id #网络名称 #网络驱动 #网络范围
c2dcffa83a29 bridge bridge local
c4deefdaf53b host host local
57942890c6d6 none null local
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7298495e6e7 bridge bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
#查看bridge的网络内部信息–>映射就使用了bridge模式
:~$ docker network inspect bridge
[
{
"Name": "bridge",
"Id": "d7298495e6e74a6be87f43127e54eb9334dd0001e1fdd074f91423b7e3649e53",
"Created": "2023-11-01T09:53:55.444071235+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"02ddf20f81b19433fc3fcab9dda1c35df3d4c39882a1e17c14f3387393c723ed": {
"Name": "thirsty_greider",
"EndpointID": "89634886ca6ab765929ff11f30a34539dae19107831c0c584e75c5e44ae12c36",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"0b21309fe92b37a40f8f6471196ebacc95391eb9ec33122c493d7414b951a547": {
"Name": "eloquent_buck",
"EndpointID": "686abf7d173f2da87d57335683f4db51e506aebd5888bdd10fa90214f059f4f2",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"13bf1f194764c6ad0847539183aa461be7d10fc7ec734c9f2340c4f665154b6a": {
"Name": "nginx22",
"EndpointID": "3d30e7f165d53ac732916eb72c6757ce30c784b521e7616700a491a2dea095cf",
"MacAddress": "02:42:ac:11:00:09",
"IPv4Address": "172.17.0.9/16",
"IPv6Address": ""
},
"78a6c9d190915cc679a15b886f7202ab0c69dcbe3b48a4d6bcf5b009df26e747": {
"Name": "nginx11",
"EndpointID": "b956b154bee74933fefb2a01cf42b6b0ea4f9e5abb0a7b8d31b44e942fe6c01d",
"MacAddress": "02:42:ac:11:00:08",
"IPv4Address": "172.17.0.8/16",
"IPv6Address": ""
},
"80edc43a6e530ca9f43316d0fb373339ad272f906a7c5b28c03fadb9336e7bd5": {
"Name": "kind_poincare",
"EndpointID": "5264aa6f00477b4bed40905189b8b73d820b28f3542f1723aa92c1e0e92e2dcd",
"MacAddress": "02:42:ac:11:00:07",
"IPv4Address": "172.17.0.7/16",
"IPv6Address": ""
},
"8dcc02812704ccdf4b6092fe39c4e9c2bb813a35650d345a9615921066222ffd": {
"Name": "relaxed_lichterman",
"EndpointID": "9b5a87ed9d3868bafb74bcdca0446429ec21619d9fd61a3c4ad036f748106b1d",
"MacAddress": "02:42:ac:11:00:06",
"IPv4Address": "172.17.0.6/16",
"IPv6Address": ""
},
"8e3fa64d2166dad267ca1b49b8b004c5a0654d7e9f8ef244aa6e6dd4f50f5991": {
"Name": "angry_hellman",
"EndpointID": "b14ce4be77c63e22fea7212592a5c1d55c185e66c3e3fd5db8525380ec626b5b",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"a23f52f973f23d31b32be44cc21b587d0f1697a6218fbd0bfddb0378d9df856a": {
"Name": "nginx-4",
"EndpointID": "2d4a66cebec22b452ec13c348b7e4d458a3e0059cf438c349f6d4b5ed5a606f0",
"MacAddress": "02:42:ac:11:00:0a",
"IPv4Address": "172.17.0.10/16",
"IPv6Address": ""
},
"b7c3175d9f89cbaf7ef45ef94098a016b4d3fc938243fd30127667cab3b05dd3": {
"Name": "objective_vaughan",
"EndpointID": "3e1e503e88dd5dd2da191cb22c5640bd16ae8efdbb0724326fcd3b56c686821a",
"MacAddress": "02:42:ac:11:00:05",
"IPv4Address": "172.17.0.5/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
回忆一下
查看容器详细信息
#命令格式:
docker inspect [容器id]
#命令效果:
查看容器全部信息:
:~$ docker inspect 930f29ccdf8a
查看容器网络信息:
:~$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}
{{end}}' 930f29ccdf8a
查看容器端口信息
#命令格式:
docker port [容器id]
#命令效果:
:~$ docker port 930f29ccdf8a
chengpeng@chengpeng:~$ docker port nginx-4
80/tcp -> 0.0.0.0:10000
80/tcp -> 0.0.0.0:10086
80/tcp -> [::]:10000
80/tcp -> [::]:10086
1314/tcp -> 0.0.0.0:520
1314/tcp -> [::]:520
2046/tcp -> 0.0.0.0:18119
2046/tcp -> [::]:18119
从1.7.0版本开始,**Docker正式把网络跟存储这两个部分的功能实现都以插件化的形式剥离出来,允许用户通过指
令来选择不同的后端实现。**这也就是Docker希望构建围绕着容器的强大生态系统的一些积极尝试。 剥离出来的独
立网络项目叫做libnetwork,libnetwork中的网络模型(Container Networking Model ,CNM)十分简洁,可以
让上层的大量应用容器最大程度上不去关心底层实现。
bridge模式: 简单来说:就是穿马甲,打着宿主机的旗号,做自己的事情。 Docker的默认模式,它会在docker容
器启动时候,自动配置好自己的网络信息,同一宿主机的所有容器都在一个网络下,彼此间可以通信。类似于我们
vmware虚拟机的桥接模式。 利用宿主机的网卡进行通信,因为涉及到网络转换,所以会造成资源消耗,网络效率
会低。
host模式: 简单来说,就是鸠占鹊巢,用着宿主机的东西,干自己的事情。容器使用宿主机的ip地址进行通信。
特点:容器和宿主机共享网络
container模式: 新创建的容器间使用,使用已创建的容器网络,类似一个局域网。 特点:容器和容器共享网络
none模式: 这种模式最纯粹,不会帮你做任何网络的配置,可以最大限度的定制化。 不提供网络服务,容器启动
后无网络连接。
overlay模式: 容器彼此不再同一网络,而且能互相通行。
其实我们在端口映射的部分就是bridge模式的简单演示了,因为他们使用的是默认bridge网络模式,现在我们来自
定义桥接网络。 这一部分我们从三个方面来演示: 创建桥接网络
使用自定义网络创建容器
容器断开、连接网络
创建网络
#命令格式:
docker network create --driver [网络类型][网络名称]
#参数
create 创建一个网络
--driver 指定网络类型
chengpeng@chengpeng:~$ docker network create --driver bridge bridge-test1
c5cc006d05644969498bfcf413b23199b5a397d82034a3e64c9ca610a6b13e51
查看主机网络类型:
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7298495e6e7 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
查看新建网络的网络信息
chengpeng@chengpeng:~$ docker network inspect bridge-test1
[
{
"Name": "bridge-test1",
"Id": "c5cc006d05644969498bfcf413b23199b5a397d82034a3e64c9ca610a6b13e51",
"Created": "2023-11-02T03:38:15.874540278+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
宿主机又多出来一个网卡设备:
chengpeng@chengpeng:~$ ifconfig
br-c5cc006d0564: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:77:ba:ac:bd txqueuelen 0 (以太网)
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
#自定义网段与网关
#查看关于网段和网关的相关命令
:~$ docker network create --help
--gateway strings IPv4 or IPv6 Gateway for the master subnet
主子网的IPv4或IPv6网关。
--subnet strings Subnet in CIDR format that represents a network segment
表示网络段的CIDR格式的子网。
创建自定义网段与网关信息
chengpeng@chengpeng:~$ docker network create --driver bridge --gateway 172.99.0.1 --subnet 172.99.0.0/16 bridge-test2
30cfaad0a1569f3491ce6143bcbdb7d313c07d543d8d5bb8a2cc038a18ac29fc
查看网络列表
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7298495e6e7 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
30cfaad0a156 bridge-test2 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
查看自定义网络的网关与网络信息
chengpeng@chengpeng:~$ docker network inspect bridge-test2
[
{
"Name": "bridge-test2",
"Id": "30cfaad0a1569f3491ce6143bcbdb7d313c07d543d8d5bb8a2cc038a18ac29fc",
"Created": "2023-11-02T04:03:51.48921799+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.99.0.0/16", -->自己设置的 网段
"Gateway": "172.99.0.1" -->自己设置的 网关
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
宿主机又多出来一个网卡设备:
chengpeng@chengpeng:~$ ifconfig
br-30cfaad0a156: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.99.0.1 netmask 255.255.0.0 broadcast 172.99.255.255
ether 02:42:29:7c:30:0e txqueuelen 0 (以太网)
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
#命令格式:
docker run --net=[网络名称] -itd --name=[容器名称][镜像名称]
#使用效果:
#查看创建的网络bridge-test
:~$ docker network inspect bridge-test
"Containers": {},#容器是空的
#查看创建的网络bridge-test1
:~$ docker network inspect bridge-test1
"Containers": {},#容器也是是空的
创建启动1个使用网络为bridge-test1 名为nginx–1的容器
chengpeng@chengpeng:~$ docker run --net=bridge-test1 -itd --name nginx-1 nginx
b79b96c37b0a30396274083af0fc6326ca687a049a3a359191c3b07097d6b528
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b79b96c37b0a nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 80/tcp nginx-1
#查看bridge-test1的网络信息
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7298495e6e7 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
30cfaad0a156 bridge-test2 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
chengpeng@chengpeng:~$ docker network inspect c5cc006d0564
[
{
"Name": "bridge-test1",
"Id": "c5cc006d05644969498bfcf413b23199b5a397d82034a3e64c9ca610a6b13e51",
"Created": "2023-11-02T03:38:15.874540278+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"b79b96c37b0a30396274083af0fc6326ca687a049a3a359191c3b07097d6b528": {
"Name": "nginx-1",
"EndpointID": "20658eb322313da11594ad715f4a62e82496808d8d9253b26d50e92416bb1997",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
查看容器的信息
chengpeng@chengpeng:~$ docker inspect nginx-1
"Networks": {
"bridge-test1": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"b79b96c37b0a"
],
"NetworkID": "c5cc006d05644969498bfcf413b23199b5a397d82034a3e64c9ca610a6b13e51",
"EndpointID": "20658eb322313da11594ad715f4a62e82496808d8d9253b26d50e92416bb1997",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null
}
}
创建启动1个使用网络为bridge-test2 名为nginx–2的容器
chengpeng@chengpeng:~$ docker run --net=bridge-test2 -itd --name nginx-2 nginx
b32823a83886c2ae776cee5c4dca6262279e4783c1c77b46dafb1f418452a2e6
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7298495e6e7 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
30cfaad0a156 bridge-test2 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b32823a83886 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp nginx-2
#查看bridge-test2的网络信息
chengpeng@chengpeng:~$ docker network inspect 30cfaad0a156
[
{
"Name": "bridge-test2",
"Id": "30cfaad0a1569f3491ce6143bcbdb7d313c07d543d8d5bb8a2cc038a18ac29fc",
"Created": "2023-11-02T04:03:51.48921799+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.99.0.0/16",
"Gateway": "172.99.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"b32823a83886c2ae776cee5c4dca6262279e4783c1c77b46dafb1f418452a2e6": {
"Name": "nginx-2",
"EndpointID": "ff94418f37222dfeb7a6ae8f5f681287047fa9fd31c074bd21a3dc5f38822943",
"MacAddress": "02:42:ac:63:00:02",
"IPv4Address": "172.99.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
查看容器的信息
chengpeng@chengpeng:~$ docker inspect nginx-2
"Networks": {
"bridge-test2": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"b32823a83886"
],
"NetworkID": "30cfaad0a1569f3491ce6143bcbdb7d313c07d543d8d5bb8a2cc038a18ac29fc",
"EndpointID": "ff94418f37222dfeb7a6ae8f5f681287047fa9fd31c074bd21a3dc5f38822943",
"Gateway": "172.99.0.1",
"IPAddress": "172.99.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:63:00:02",
"DriverOpts": null
}
}
#注意部分
#使用默认的桥接模型创建的容器是可以直接联网的。
#使用自定义的桥接模型创建的容器不可以直接联网,但是可以通过端口映射来实现联网
#命令格式:
docker network disconnect [网络名][容器名]
#命令演示:
docker network disconnect bridge-test nginx1
效果展示:
断开容器nginx-1网络bridge-test1和断开容器nginx-2网络bridge-test2
chengpeng@chengpeng:~$ docker network disconnect bridge-test1 nginx-1
chengpeng@chengpeng:~$ docker network disconnect bridge-test2 nginx-2
#查看下容器
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b32823a83886 nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp nginx-2
b79b96c37b0a nginx "/docker-entrypoint.…" 26 minutes ago Up 26 minutes 80/tcp nginx-1
发现nginx-1和nginx-1的网络消失
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b32823a83886 nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes nginx-2
b79b96c37b0a nginx "/docker-entrypoint.…" 27 minutes ago Up 27 minutes nginx-1
分别查看网络bridge-test1 bridge-test2的网络
:~$ docker network inspect bridge-test1
:~$ docker network inspect bridge-test2
发现容器内容消失
“Containers”: {},#已经没有容器了
#分别查看两个容器的信息发现容器网络信息消失–>“Networks”: {}
:~$docker inspect nginx--1
:~$docker inspect nginx--2
#命令格式:
docker network connect [网络名][容器名]
命令演示:
将容器nginx–2连接到bridge-test1网络和容器nginx–1连接到bridge-test2网络
chengpeng@chengpeng:~$ docker network connect bridge-test1 nginx-2
chengpeng@chengpeng:~$ docker network connect bridge-test2 nginx-1
查看容器nginx-1相关信息–>nginx-2同理
chengpeng@chengpeng:~$ docker inspect nginx-1
"Networks": {
"bridge-test2": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"b79b96c37b0a"
],
"NetworkID": "30cfaad0a1569f3491ce6143bcbdb7d313c07d543d8d5bb8a2cc038a18ac29fc",
"EndpointID": "f55f260e07d5bfa70e3b713c51077875ca930156b5ed525e9d981c6737cec8f3",
"Gateway": "172.99.0.1",
"IPAddress": "172.99.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:63:00:02",
"DriverOpts": {}
}
}
查看bridge-test1网络是否包含容器–>bridge-test2同理
chengpeng@chengpeng:~$ docker network inspect bridge-test1
"Containers": {
"b32823a83886c2ae776cee5c4dca6262279e4783c1c77b46dafb1f418452a2e6": {
"Name": "nginx-2",
"EndpointID": "c23e269dec27c14653c264d83731b8ff451635597139a97fd988fbe09c4ba2b3",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
}
之前我们创建的容器,它们的ip都是从docker0自动获取的,接下来我们自己定义一个br0网桥,然后启动的容器就
用这个
网桥是什么? 他是一种设备,根据设备的物理地址来划分网段,并传输数据的,docker0就是默认的网桥。
需求: 定制docker网桥
分析: 1、网桥的创建
2、docker服务使用新网桥
3、测试
知识点: 1、bridge-utils软件的brctl工具可以实现创建网桥 2、 配置/etc/default/docker文件 编辑systemctl的
配置文件使用该docker文件 重载systemctl配置 重启docker 3、创建容器,查看容器信息即可
实施:
#1、网桥环境部署
#1.1 网桥软件部署
#ubuntu默认不自带网桥管理工具,安装网桥软件
:~$ sudo apt-get install bridge-utils -y
#查看网卡
:~$ brctl show
bridge name bridge id STP enabled interfaces
#网卡名称 网卡id STP启用 物理接口
chengpeng@chengpeng:~$ brctl show
bridge name bridge id STP enabled interfaces
br-30cfaad0a156 8000.0242297c300e no vetha89aa2f
br-c5cc006d0564 8000.024277baacbd no vethfe6178e
docker0 8000.0242dc083583 no
:~$ sudo brctl addbr br0
:~$ brctl show
chengpeng@chengpeng:~$ sudo brctl addbr br0
[sudo] chengpeng的密码:
chengpeng@chengpeng:~$ brctl show
bridge name bridge id STP enabled interfaces
br-30cfaad0a156 8000.0242297c300e no vetha89aa2f
br-c5cc006d0564 8000.024277baacbd no vethfe6178e
br0 8000.000000000000 no
docker0 8000.0242dc083583 no
给网桥设置网段
:~$ sudo ifconfig br0 192.168.99.1 netmask 255.255.255.0
:~$ ifconfig
chengpeng@chengpeng:~$ sudo ifconfig br0 192.168.99.1 netmask 255.255.255.0
chengpeng@chengpeng:~$ ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.99.1 netmask 255.255.255.0 broadcast 192.168.99.255
inet6 fe80::6c65:5eff:fe87:9e24 prefixlen 64 scopeid 0x20<link>
ether 6e:65:5e:87:9e:24 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 42 bytes 6266 (6.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.1 配置docker文件
:~$ sudo vim /etc/default/docker
#最末尾添加
DOCKER_OPTS="-b=br0"
#创建服务依赖文件
:~$ sudo mkdir -p /etc/systemd/system/docker.service.d
:~$ sudo vim /etc/systemd/system/docker.service.d/Using_Environment_File.conf
Using_Environment_File.conf内容如下:
[Service]
EnvironmentFile=-/etc/default/docker
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
重载服务配置文件
:~$ systemctl daemon-reload
2.3 重启docker
重启前效果
:~$ ps aux |grep docker root 32949 0.1 1.4 783160 59632 ? Ssl 2月24 1:01 /usr/bin/dockerd -H fd://
重启
:~$ systemctl restart docker
重启后效果
:~$ ps aux |grep docker root 45737 4.3 1.2 527600 50572 ? Ssl 09:32 0:00 /usr/bin/dockerd -H fd:// -b=br0
3.1 创建容器并测试
创建默认网络的容器
:~$ docker run -itd --name nginx--3 nginx
:~$ docker ps
chengpeng@chengpeng:~$ docker run -itd --name nginx-3 nginx
acfd55ca3a1205009a1ea0412a366ad0d312b2c6693fe946dea6304880c08d42
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acfd55ca3a12 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 80/tcp nginx-3
查看信息已经使用了br0的网卡的网络
chengpeng@chengpeng:~$ docker inspect nginx-3
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "bd7beb26e3f06279a869d5b83e35cc72a433f583111c7a5c9250b477667a2794",
"EndpointID": "7e5cd7cbf16a84565289c2e24b32d6257db3960ecd176381be0687969be13e2f",
"Gateway": "192.168.99.1",
"IPAddress": "192.168.99.2",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:63:02",
"DriverOpts": null
}
}
查看下网络
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
bd7beb26e3f0 bridge bridge local
查看网络下的容器
chengpeng@chengpeng:~$ docker network inspect bridge
{
"Name": "bridge",
"Id": "bd7beb26e3f06279a869d5b83e35cc72a433f583111c7a5c9250b477667a2794",
"Created": "2023-11-02T07:22:15.071406696+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.99.0/24",
"Gateway": "192.168.99.1"
}
]
},
host模型我们知道,容器使用宿主机的ip地址进行对外提供服务,本身没有ip地址。
命令格式:
docker run --net=host -itd --name [容器名称] 镜像名称
命令示例:
查看下网络情况
:~$ docker network ls
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
bd7beb26e3f0 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
30cfaad0a156 bridge-test2 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
查看host下有哪些容器
:~$ docker network inspect host
#发现是空的
"Containers": {}
查看宿主机启动网络
:~$ netstat -tnulp
#发现没有80端口
根据host网络创建启动容器
chengpeng@chengpeng:~$ docker run -itd --net=host --name nginx-4 nginx
72271d4a0facdfe8acae1c088eb8c15d8e02f6f255902a6bc61693e94712aa9e
发现没有端口映射
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72271d4a0fac nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds nginx-4
查看宿主机启动网络
:~$ netstat -tnulp
#多出了80端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
查看host下有哪些容器
:~$ docker network inspect host
#发现网络下包含了容器 nginx-4
"Containers": {
"72271d4a0facdfe8acae1c088eb8c15d8e02f6f255902a6bc61693e94712aa9e": {
"Name": "nginx-4",
"EndpointID": "658cc5d6f0b461dc8cdde0ec53a54205d12ec8c33388a83d12ee2b5daf5a0ad6",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
#查看nginx-1这个容器的全部信息
:~$ docker inspect nginx-1
#发现网络信息为空
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "14be585b3947d9508e49c38fa8d088f0a60464ee2f4acd425b0f9ebbaec884e1",
"EndpointID": "658cc5d6f0b461dc8cdde0ec53a54205d12ec8c33388a83d12ee2b5daf5a0ad6",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
此处IP为对应宿主机ip并不固定
host特点: host模型比较适合于,一台宿主机跑一个固定的容器,比较稳定,或者一个宿主机跑多个占用不同端
口的应用的场景,他的网络性能是很高的。 host模型启动的容器不会有任何地址,他其实是使用了宿主机的所有
信息
none网络模式,是一种自由度非常高的网络模式,我们可以最大化的自定义我们想要的网络
#命令格式:
docker run --net=none -itd --name [容器名称] 镜像名称
命令示例:
查看网络
chengpeng@chengpeng:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
bd7beb26e3f0 bridge bridge local
c5cc006d0564 bridge-test1 bridge local
30cfaad0a156 bridge-test2 bridge local
14be585b3947 host host local
bc8cc94e30a5 none null local
查看网络none的信息
:~$ docker network inspect none
发现包含容器为空
"Name": "none",
"Containers": {},
根据none网络创建nginx2容器
chengpeng@chengpeng:~$ docker run -itd --net=none --name nginx2 nginx
bcbfd3d9cdc8684e626bd66479d9d613ec8b64c2cb4bc2b3c3e8fbe669878308
###==>没有端口
itcast@itcast:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcbfd3d9cdc8 nginx "/docker-entrypoint.…" 40 seconds ago Up 39 seconds nginx2
查看nginx2的全部信息
chengpeng@chengpeng:~$ docker inspect nginx2
发现网络信息皆为空
"Networks": {
"none": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "bc8cc94e30a5e2135e3a799f230cf1462da015618d9ad53750ba9667f9b35f84",
"EndpointID": "2e1bc9c4de68eab1b90ac1c0d1255de390d9f2b8b6acc1e33819945f39920b01",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
:~$ docker network inspect none
none网络下有none的网络下包含容器 nginx2并且没有网络信息
"ConfigOnly": false,
"Containers": {
"bcbfd3d9cdc8684e626bd66479d9d613ec8b64c2cb4bc2b3c3e8fbe669878308": {
"Name": "nginx2",
"EndpointID": "2e1bc9c4de68eab1b90ac1c0d1255de390d9f2b8b6acc1e33819945f39920b01",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
netstat -tnulp
发现并没有80端口的网络启动
配置自定义桥接网络案例 为了使本地网络中和Docker容器更方便的通信,我们经常会有将Docker容器配置到和主
机同一网段,而且还要指定容器的ip地址。
需求: 自定义容器网络和宿主机为同一网段,容器ip可以指定。
案例分析: 1、自定义容器网络段和宿主机一样
2、自定义容器ip地址 知识关键点:
1、网络配置
容器创建使用none模式
2、使用pipwork工具实现定制docker容器ip地址
注释:pipwork的命令格式
pipework [桥接设备][容器id或者名字] [容器ip]/[ip掩码]@[宿主机网关]
例子:
pipework br0 ubuntu-test1 192.168.8.201/[email protected]
3、映射虚拟机软件源进入到容器,替换掉容器内部软件源后进行软件源更新与安装
注释:docker上pull下来的Ubuntu,使用apt-get install 命令下载速度奇慢无比,需要修改其软件源,进入etc/apt
目录欲修改sources.list 发现vi,vim,gedit都没有,再下这些软件也非常慢.
解决方法:
3.1启动容器时,挂载本地Linux系统的etc/apt文件
docker run -ti -v /etc/apt/:/home/etc ubuntu
3.2删除容器下的
sources.lis rm /etc/apt/sources.list
3.3将本地sources.list 复制过来
cp /home/etc/sources.list /etc/apt/
#1、网络环境部署
#1.1 网卡环境部署
#1.1.1 网桥软件部署
:~$ sudo apt-get install bridge-utils -y
:~$ brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242a6e980f2 no
#1.1.2 桥接网卡配置
#编辑网卡信息编辑Ubuntu的网卡信息文件
#对源文件进行备份
:~$ sudo cp /etc/network/interfaces /etc/network/interfaces-old
:~$ sudo vim /etc/network/interfaces
#与源文件内容进行1行的空行
auto br0
iface br0 inet static
address 192.168.110.14
netmask 255.255.255.0
gateway 192.168.110.2
dns-nameservers 192.168.110.2
bridge_ports ens33
#重启
service networking restart
#1.2 docker服务配置
#1.2.1 配置docker文件
:~$ sudo vim /etc/default/docker
#最末尾添加
DOCKER_OPTS="-b=br0"
#1.2.2 systemctl使用docker文件
#创建服务依赖文件
:~$ sudo mkdir -p /etc/systemd/system/docker.service.d
:~$ sudo vim /etc/systemd/system/docker.service.d/Using_Environment_File.conf
#内容如下:
[Service]
EnvironmentFile=-/etc/default/docker
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
#重载服务配置文件
:~$ systemctl daemon-reload
#1.2.3 重启docker 第一次配置的时候需要重启linux虚拟机:reboot
systemctl restart docker
#注意查看网卡信息
chengpeng@chengpeng:~$ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c296ed976 no ens33
docker0 8000.0242b63e17e0 no
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.175.129 netmask 255.255.255.0 broadcast 192.168.175.255
inet6 fe80::20c:29ff:fe6e:d976 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:6e:d9:76 txqueuelen 1000 (以太网)
RX packets 268 bytes 30919 (30.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 210 bytes 57044 (57.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:6e:d9:76 txqueuelen 1000 (以太网)
RX packets 273 bytes 35804 (35.8 KB)
RX errors 0 dropped 1 overruns 0 frame 0
TX packets 225 bytes 66451 (66.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
此时br0代替了ens33
#广播运行多播
#验证dns解析是否正常
ping www.baidu.com-->ping不同 但是ping百度ip是能ping通的
#网络可能会没有dns解析所以我们需要进行dns的配置
#16.04:
:~$ sudo vim /etc/resolvconf/resolv.conf.d/base
#18.04:
:~$ sudo vim /etc/resolv.conf
#增加内容
nameserver 223.5.5.5
nameserver 114.114.114.114
nameserver 8.8.8.8
#注意如果重启后网络并未生效则
sudo /etc/init.d/networking restart
#1.3 容器创建
#基于ubuntu镜像创建一个容器,网络模式使用none ,启动容器时,挂载本地Linux系统的etc/apt文件
:~$ docker run -itd --net=none --name ubuntu-test1 -v /etc/apt/:/home/etc ubuntu /bin/bash
5f7b976ddfdf60dbc08cb81569488b70da15bc183d7f21da7030c316cd6ec96b
chengpeng@chengpeng:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a98c80578696 ubuntu "/bin/bash" 21 seconds ago Up 21 seconds ubuntu-test1
#2、定制容器ip
#2.1 pipwork软件部署
#安装pipwork
#方法1:
git clone https://github.com/jpetazzo/pipework
#方法2:将软件直接拖入ubuntu虚拟机
#直接解压安装包
:~$unzip pipework-master.zip
#将文件拷贝到bin下
sudo cp pipework-master/pipework /usr/local/bin/
#2.2 定制容器ip
:~$ sudo pipework br0 ubuntu-test1 192.168.175.128/[email protected]
#2.3 测试效果
#进入容器查看ip地址信息
:~$ docker exec -it ubuntu-test1 /bin/bash
#删除容器下的sources.lis
:~# rm /etc/apt/sources.list
#将本地sources.list 复制过来
:~# cp /home/etc/sources.list /etc/apt/
#进行软件源更新
:~# apt-get update
#安装ping命令
:~# apt-get install inetutils-ping -y
#安装ifconfig命令
:~# apt-get install net-tools -y
宿主机ping命令测试
> ping 192.168.175.129--->主机的ip(windows)
BUG:18的ubuntu去docker下载2版本根本玩不了
查看版本号
apt-get install net-tools -y
容器网络拓扑图
主机信息:
主机1:ubuntu 18.04 192.168.8.14
主机2:ubuntu 16.04 192.168.8.15
均安装 bridge-utils软件
分析: 1、自定义br0
2、docker服务使用br0
3、创建容器使用br0
4、跨主机间网络测试
知识点: 1、使用手工方式定制ubuntu的网卡
2、 配置/etc/default/docker文件
编辑systemctl的配置文件使用该docker文件
重载systemctl配置
重启docker或者重启虚拟机
3、创建容器,查看容器信息即可
4、两台主机分别测试
注意: 1、2、3 这三条在两台主机的配置大部分一模一样 ip地址划分不一样
方案: 1、ubuntu桥接网卡配置
1.1 软件安装
1.2 编辑网卡
2、docker配置网桥
2.1 配置docker文件
2.2 systemctl使用docker文件
2.3 重启主机
3、容器测试
3.1 创建容器
3.2 容器间测试
#1、ubuntu桥接网卡配置
#1.1 软件安装
apt-get install bridge-utils -y
#1.2 编辑网卡
:~$ sudo vim /etc/network/interfaces
#与文件源内容进行1行的空行
#主机1
auto br0
iface br0 inet static
address 192.168.110.14
netmask 255.255.255.0
gateway 192.168.110.2
dns-nameservers 192.168.110.2
bridge_ports ens33
#主机2
auto br0
iface br0 inet static
address 192.168.110.15
netmask 255.255.255.0
gateway 192.168.110.2
dns-nameservers 192.168.110.2
bridge_ports ens33
#2、docker配置网桥
#2.1 配置docker文件
#修改docker的守护进程文件
vim /etc/default/docker
#末尾添加:
#主机1
DOCKER_OPTS="-b=br0 --fixed-cidr=192.168.110.99/26"
#主机2
DOCKER_OPTS="-b=br0 --fixed-cidr=192.168.110.170/26"
#注释:
#-b 用来指定容器连接的网桥名字
#--fixed-cidr用来限定为容器分配的IP地址范围
#192.168.110.99/26地址范围:192.168.110.64~192.168.110.127
#192.168.110.170/26地址范围:192.168.110.128~192.168.110.191
#网段的计算可以参考网址:http://help.bitscn.com/ip/
#2.2 systemctl使用docker文件
创建服务依赖文件
:~$ sudo mkdir -p /etc/systemd/system/docker.service.d
:~$ sudo vim /etc/systemd/system/docker.service.d/Using_Environment_File.conf
内容如下:
[Service]
EnvironmentFile=-/etc/default/docker
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
#重载服务配置文件
:~$systemctl daemon-reload
#2.3 重启主机
reboot
#注意如果重启后网络并未生效则
sudo /etc/init.d/networking restart
#注意查看网卡信息
:~$ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2960060c no ens33
docker0 8000.02427c11f899 no
br0 Link encap:以太网 硬件地址 00:0c:29:60:06:0c
inet 地址:192.168.110.14 广播:192.168.110.255 掩码:255.255.255.0
inet6 地址: fe80::20c:29ff:fe60:60c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
ens33 Link encap:以太网 硬件地址 00:0c:29:60:06:0c
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
#广播运行多播
广播运行多播
#验证dns解析是否正常
ping www.baidu.com
#网络可能会没有dns解析所以我们需要进行dns的配置
#16.04:
:~$ sudo vim/etc/resolvconf/resolv.conf.d/base
#18.04:
:~$ sudo vim/etc/resolv.conf
#增加内容
nameserver 223.5.5.5
nameserver 114.114.114.114
nameserver 8.8.8.8
#注意如果重启后网络并未生效则
sudo /etc/init.d/networking restart
#3、容器测试
#3.1 创建容器
#主机1:
:~$ docker run -itd --name ubuntu-test1 -v /etc/apt/:/home/etc ubuntu
/bin/bash
:~$ docker run -itd --name ubuntu-test2 -v /etc/apt/:/home/etc ubuntu
/bin/bash
#主机2
:~$ docker run -itd --name ubuntu-test3 -v /etc/apt/:/home/etc ubuntu
/bin/bash
:~$ docker run -itd --name ubuntu-test4 -v /etc/apt/:/home/etc ubuntu
/bin/bash
#3.2 容器间测试
进入容器
#主机1
:~$docker exec -it ubuntu-test1 /bin/bash
:~$docker exec -it ubuntu-test2 /bin/bash
#主机2
:~$docker exec -it ubuntu-test3 /bin/bash
:~$docker exec -it ubuntu-test4 /bin/bash
:~# rm /etc/apt/sources.list
#容器内部将本地sources.list 复制过来
:~# cp /home/etc/sources.list /etc/apt/
#容器内部进行软件源更新
:~# apt-get update
#容器内部安装ping命令
:~# apt-get install inetutils-ping -y
#容器内部安装ifconfig命令
:~# apt-get install net-tools -y
#四个容器之间相互ping通
宿主机ping命令测试
> ping 192.168.110.14
> ping 192.168.110.15
总结: 优点: 配置简单,不依赖第三方软件 缺点: 容器依赖于主机间的网络 容器与主机在同网段,注意ip地址分
配 生产中不容易实现、不好管理