docker网络模式
网络基础:
docker使用到的与LInux网络有关的技术分别有:网络名称空间,Veth,lptables,网桥,路由
veth:了解。
通过创建Veth设备对,绑定命名空间,将Veth分配给IP,为对端Veth设备设置IP,就可以实现两个 容器之间的网络通信
网桥:
查看系统网桥
docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
创建网桥
docker network create [网桥的名称]
[root@localhost ~]# docker network create oldboy
617ba09862ae61d1385c1c9af517d3d803f19e965cd4bce5a5655234c8d1bf6e
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
617ba09862ae oldboy bridge local
查看网桥的详细信息
docker network inspect [网桥名称]
[root@localhost ~]# docker network inspect oldboy
[
{
"Name": "oldboy",
"Id": "617ba09862ae61d1385c1c9af517d3d803f19e965cd4bce5a5655234c8d1bf6e",
"Created": "2020-12-03T08:58:43.354251604+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": {}
}
]
删除网桥
docker network rm [网桥名称|ID]
[root@localhost ~]# docker network rm oldboy
oldboy
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
清理网桥
docker network prune
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
[root@localhost ~]# docker network create test
efde281ecb8b00efbc55014cd077858f91944e81d21fd413d64010c7c9911565
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
efde281ecb8b test bridge local
[root@localhost ~]# docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
test
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a065ed712fea bridge bridge local
7b862ba6761e host host local
f7e824ff4e3e none null local
使用网桥
需求:nignx django redis mysql
创建网桥
docker network create oldboy
连接网桥
docker run --network [网桥名称] [镜像ID|镜像名称] [cmd]
# 第一个容器
docker run -d --name nginx --network oldboy nginx:1.19.2
# 第二个容器
docker run -d --name redis --network oldboy redis:latest
# 第三个容器
docker run -d --name mysql --network oldboy -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
# 测试
docker run -d -it --name test --network oldboy busybox:latest sh
# 要求用busybox实现类似于nignx启动时自动执行某些命令
hello world