我有一个通过 docker-compose 启动的 Docker 容器。配置文件指定它应该是两个网络的一部分。然而,有时(大约五分之一的运行),容器只是其中一个网络的成员(即docker network inspect <network_id>
不显示容器)。这是我的 MRE:
Dockerfile
FROM alpine:latest
ENTRYPOINT ["sleep", "infinity"]
docker-compose.yaml
version: "3.7"
services:
foo:
image: foo
init: true
networks:
- network1
- network2
networks:
network1:
network2:
docker network inspect foo_network2
节目
[
{
"Name": "foo_network2",
"Id": "2ef317904b46eddc3dcd8242f1ee41eff02756e016c701c2f2366bbbb166670e",
"Created": "2024-03-12T04:15:24.57501242Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.144.0/20",
"Gateway": "192.168.144.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {
"com.docker.compose.network": "network2",
"com.docker.compose.project": "foo",
"com.docker.compose.version": "2.24.6"
}
}
]
而docker network inspect foo_network1
显示
[
{
"Name": "foo_network1",
"Id": "a505fd52cd34c9ecdad1aa27058cc7cb47243d82283a7c7949b211be422a10e8",
"Created": "2024-03-12T04:15:24.535686504Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.128.0/20",
"Gateway": "192.168.128.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"823f84eff688b9bc8e207b208cfb23852a2bc777b121107ab9d0ee48248e0b1d": {
"Name": "foo-foo-1",
"EndpointID": "37606efb0ea637c686edac7ccebab033dfd36f0cce9599d162a540a81d687b2f",
"MacAddress": "02:42:c0:a8:80:02",
"IPv4Address": "192.168.128.2/20",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "network1",
"com.docker.compose.project": "foo",
"com.docker.compose.version": "2.24.6"
}
}
]
我在 Mac 上运行 Docker Desktop 4.28.0(引擎 25.0.3,Compose:2.24.6)。我无法在 Ubuntu 23.10(引擎 25.0.2,docker-compose 1.29.2)上重现此行为。