我有一个容器,我想拥有一个固定 IP。
我的相关设置(docker-compose)如下:
version: '3'
networks:
default:
external:
name: srv
services:
caddy:
image: caddy
container_name: caddy
volumes:
- /etc/docker/data/caddy/Caddyfile:/etc/caddy/Caddyfile
- /etc/docker/data/caddy/data:/data
- /etc/docker/data/caddy/config:/config
ports:
- 80:80
- 443:443
- 2015:2015
environment:
- ACME_AGREE=true
restart: unless-stopped
# networks:
# srv:
# ipv4_address: 172.19.0.254
当最后几行被注释掉时,一切都很好。当我取消注释它们时,重新启动caddy
容器会产生错误
ERROR: Service "caddy" uses an undefined network "srv"
caddy
正确运行时检查容器:
~ # docker inspect caddy
[
{
"Id": "37904d87c82c4612f48bcc9952b6505d871a816336c6b242b4ab3fae51cbfa95",
"Created": "2021-01-25T18:27:29.686179872Z",
"Path": "caddy",
(...)
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1a4360ebfe25eff6dffec0f9ec049389da6e53e84eaf76db23bf6028868b31f3",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"2015/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "2015"
}
],
"2019/tcp": null,
"443/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "443"
}
],
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/1a4360ebfe25",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"srv": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"caddy",
"37904d87c82c"
],
"NetworkID": "36b7471fa0f16a74531b011ce6762cf66a802fda7622d70d66b6fb130a2398ea",
"EndpointID": "8dea6be732eda01a9b4d6cc0a26df2eca8a6df1a6f6d7a961fb46820a6ef9d58",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.8",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:08",
"DriverOpts": null
}
}
}
}
]
如何强制该容器使用固定 IP?
注意:一个类似标题的问题(错误:服务“xxx”使用未定义的网络“xxx”)解决了缺乏网络定义的问题(不是我的情况)