有没有办法告诉 docker 在运行 docker-compose up 时不要使用某些特定网络?
我正在使用一些开箱即用的示例(hyperledger
),每次 docker 获取一个地址都会中断我的远程连接。
[xxx@xxx fabric]$ docker network inspect some_network
[
{
"Name": "some_network",
"Id": "xxx",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.30.0.0/16",
"Gateway": "172.30.0.1/16"
}
]
},
"Internal": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
我想要告诉 docker(无需编辑 docker-compose.yaml 文件)永远不要使用网络 172.30.0.0/16
。
最佳答案
当你在docker-compose.yml
中创建网络时,你可以指定IP范围、子网、网关等。你可以这样做。
version: "3"
services:
service1:
build: .
...
networks:
default:
subnet: 172.28.0.0/16
ip_range: 172.28.5.0/24
gateway: 172.28.5.254
要在 Docker 守护进程中执行此操作,您需要传递 --bip
和 --fixed-cidr
参数。
来自 Docker 文档:https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/
--bip=CIDR
: supply a specific IP address and netmask for thedocker0
bridge, using standard CIDR notation. For example:192.168.1.5/24
.
--fixed-cidr=CIDR
: restrict the IP range from thedocker0
subnet, using standard CIDR notation. For example:172.16.1.0/28
. This range must be an IPv4 range for fixed IPs, such as10.20.0.0/16
, and must be a subset of the bridge IP range (docker0 or set using --bridge). For example, with --fixed-cidr=192.168.1.0/25
, IPs for your containers will be chosen from the first half of addresses included in the192.168.1.0/24
subnet.
要使此更改永久生效,请创建或编辑 /etc/docker/daemon.json
并添加选项 bip
和 fixed-cidr
。
{
"bip": "192.168.1.5/24",
"fixed-cidr": "192.168.1.0/25"
}
关于docker - 禁止docker使用特定网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930707/