如何在创建网络的情况下在docker-compose中映射容器的接口名称?

时间:2019-04-27 09:49:33

标签: docker-compose ipv6

我正在设置docker-compose(版本1.21.1)3个docker容器和两个网络:

version: '2.1'
services:
  app1:
    build:
      context: .
      dockerfile: "Dockerfile"
    depends_on:
      - redis
    networks:
      - pub
      - default

  redis:
    build:
      context: "tests/redis"
    networks:
      - default

  app2:
    build:
      context: "tests/app2"
    networks:
      - pub
      - default

networks:
  pub:
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: "172.16.238.0/24"
        - subnet: "fe80::42:acff:fe10:ee04/64"
  default:

在app1中:

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN     group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
696: eth0@if697: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:10:ee:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0    
    inet 172.16.238.3/24 brd 172.16.238.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe10:ee03/64 scope link
       valid_lft forever preferred_lft forever
    inet6 fe80::3/64 scope link nodad
       valid_lft forever preferred_lft forever
698: eth1@if699: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:c0:a8:f0:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.240.4/20 brd 192.168.255.255 scope global eth1
       valid_lft forever preferred_lft forever

但是,我希望eth1支持IPv6,或者同时支持eth0和eth1。 documentation没有提及任何内容,我也无法在network options中找到一个选项。 有办法吗?

1 个答案:

答案 0 :(得分:0)

我必须为两个网络都启用ipv6,并且必须对IPv6子网进行子网划分。 对于CIDR部分,我使用了用于IPv6的在线子网计算器,但是我还不确定为什么它起作用:p

这是有效的配置:

networks:
  default:
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: "2001:db8:8000::/33"
  pub:
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: "2001:db8::/33"