0

我有一个容器,我想拥有一个固定 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”)解决了缺乏网络定义的问题(不是我的情况)

4

2 回答 2

1

当你说在顶层

networks:
  default: { ... }

那么当您在每个服务配置中明确引用该网络时,它需要使用匹配的 Compose 名称default

networks:
  # vvv the name of the top-level `networks:` item
  #     (probably different from the `docker network create` name)
  default:
    ipv4_address: 172.19.0.254

网络的“真实”Docker 名称在这里无关紧要;在 Compose 文件中,您始终使用顶级networks:块下的名称。

(显式设置容器的私有 IPv4 地址有点不寻常,您通常不需要这样做。如果您没有每个服务networks:,它将默认连接到default网络,即使您手动重新配置了它顶级网络。)

于 2021-01-25T19:46:24.047 回答
0

caddy服务不使用网络的外部名称,而只是看到default. 尝试更新网络定义:

networks:
  srv:
    external: true

检查Compose 文件版本 3 参考

您还可以将网络名称与在 Compose 文件中用于引用它的名称分开指定:

version: "3.9"
networks:
  outside:
    external:
      name: actual-name-of-network
于 2021-01-25T19:46:06.227 回答