多个静态 IP - docker-compose

Multiple static IP - docker-compose

这是我的第一个 post 所以:你好:)

我想让一个容器连接到同一主机上的两个 docker 网络。 我想为每个网络设置静态 IP。 网络已经创建。 我可以用一个网络设置一个静态 IP

https://pastebin.com/jnpFHSND

version: '3.3'
services:
    homer:
        ports:
            - '7070:8080'
        volumes:
            - '/docker/homertest:/www/assets'
        restart: always
        hostname: homer-test
        networks:
          #default
            #ipv4_address: 172.19.0.222
          frontend:
            ipv4_address: 172.18.0.111
          # - kuma:
          #     ipv4_address: 172.20.0.222
          #- smtp
        container_name: homer
        image: 'b4bz/homer:latest'
networks:
  default:
    external:
      name: nginx-proxy-manager_default
  frontend:
    external:
      name: portainer_default
  kuma:
    external:
      name: uptime-kuma_default
  smtp:
    external:
      name: smtp-relay_default

但有两个或更多

https://pastebin.com/axSaa0iJ

version: '3.3'
services:
    homer:
        ports:
            - '7070:8080'
        volumes:
            - '/docker/homertest:/www/assets'
        restart: always
        hostname: homer-test
        networks:
          #default
            #ipv4_address: 172.19.0.222
          - frontend:
              ipv4_address: 172.18.0.111
          - kuma:
              ipv4_address: 172.20.0.222
          #- smtp
        container_name: homer
        image: 'b4bz/homer:latest'
networks:
  default:
    external:
      name: nginx-proxy-manager_default
  frontend:
    external:
      name: portainer_default
  kuma:
    external:
      name: uptime-kuma_default
  smtp:
    external:
      name: smtp-relay_default

我收到一个错误

services.homer.networks contains {"frontend": {"ipv4_address": "172.18.0.111"}}, which is an invalid type, it should be a string

我做错了什么?

您在第一个版本中使用的是映射,但在第二个版本中您使用的是列表。保持映射。

networks:
  frontend:
    ipv4_address: 172.18.0.111
  kuma:
    ipv4_address: 172.20.0.222

您还可以查看 json schema for compose files


列表和映射是什么意思?

list:
  - a
  - b

mapping:
  a:
  b:

这将转换为 json,如下所示,这可能会更清楚:

{
   "list": [
      "a",
      "b"
   ],
   "mapping": {
      "a": null,
      "b": null
   }
}

只需删除 frontend 和 kuma 前面的连字符即可。这将使它从列表映射并且它将起作用。