容器网络管理
查看容器网络
docker network ls [选项]
选项有:
- -f ,--filter filter
过滤条件
- --format string
格式化打印结果
- --no-trunc
不缩略显示
- -q,--quiet
只显示网络对象id号
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
226f69535fb5 bridge bridge local
bf4d7d7885df host host local
2581b9b26638 none null local
#添加条件,选出host网络
[root@docker ~]# docker network ls -f 'driver=host'
NETWORK ID NAME DRIVER SCOPE
bf4d7d7885df host host local
#不缩略显示
[root@docker ~]# docker network ls --no-trunc
NETWORK ID NAME DRIVER SCOPE
226f69535fb572774d88d7d96c58d3ee80738dd1f4db47b13575870812fa7765 bridge bridge local
bf4d7d7885df532e2240356102a63393fb2b0d75902dca6c09aae6a8f702a9e4 host host local
2581b9b26638871acb1218d52b5a6f0955abb5c8300784ec5427ea285137b2b2 none null local
#查看网络id
[root@docker ~]# docker network ls -q
226f69535fb5
bf4d7d7885df
2581b9b26638
创建容器网络
docker network create [选项] network
选项:
- -d,--driver string
指定网络模式(默认bridge)
- --subnet strings
指定子网网段
- --ip-range strings
指定容器ip地址范围,格式同subnet参数
- --gateway strings
子网ipv4或v6网关
[root@docker ~]# docker network create -d bridge test-bridge
f97a19be7e41b55624c52c5032413b8dc436ecb44283ed2cdaaa8fc4ec0f7112
#查看网络
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
226f69535fb5 bridge bridge local
bf4d7d7885df host host local
2581b9b26638 none null local
f97a19be7e41 test-bridge bridge local
删除容器
docker network rm 网络id
#查看网络
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
226f69535fb5 bridge bridge local
bf4d7d7885df host host local
2581b9b26638 none null local
f97a19be7e41 test-bridge bridge local
[root@docker ~]# docker network rm f97a19be7
f97a19be7
容器网络详细信息
docker network inspect [选项]
- -f,--format string
使用给定的模板格式输出
[root@docker ~]# docker network inspect none
[
{
"Name": "none",
"Id": "2581b9b26638871acb1218d52b5a6f0955abb5c8300784ec5427ea285137b2b2",
"Created": "2024-02-27T18:40:43.571987303+08:00",
"Scope": "local",
"Driver": "null",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": null
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
配置容器网络
在运行容器命令中添加指定的网络参数
docker run/create --network NETWORK
例如指定host网络
docker run \
-v ~/Centos-8.repo:/root/Centos-8.repo \
--network=host \
-it centos /bin/bash
容器网络连接与断开
docker network connect [选项] network container
docker network disconnect [选项] network container
Comments NOTHING