4

Can anyone shed some light on what is what with the Docker Compose YML file? All I want to do is to be able to control the IP addresses of the various containers. I am using version 3.1 (but also tried 3.3 as I recently upgraded to version 17.06). The documentation says:

A full example:

ipam:
  driver: default
  config:
    - subnet: 172.28.0.0/16
Note: Additional IPAM configurations, such as gateway, are only honored for version 2 at the moment.

When I do this, I need that subnet honored when I inspect the network. However the gateway is completely different [read the Note: above], so the containers do not start. Why did they lose capability (at the moment) in version 3 for something that worked in version 2? Worse, why wasn't that restored in version 3.2 or 3.3?

Maybe I am way off base here - certainly wouldn't be first time! What is most important to me: is there a way to modify a compose file to allow a docker stack deploy command (in a Docker Swarm) to provide control of the gateway and subnets used?

4

2 回答 2

3

终于弄清楚了这一点,我发布了我所做的事情,希望它可以帮助其他人。虽然我开始时并不知道这一点,但我真正想要的是[需要?:) ] 要做的是重新定义docker_gwbridge网络的默认设置。

我就是这样做的:

docker swarm init     # I am assuming this was already done, this creates the network with default settings
docker swarm leave -f  # only if you did an 'init'
docker network ls      # just to see the docker_gwbridge network
docker network rm docker_gwbridge

# if you never created/initialized a swarm, you can start here
SUBNET=172.19.0.0/16   # my defaults were always 172.18, using 19 only to test that this works
GATEWAY=172.19.0.1
docker network create --subnet=$SUBNET --gateway $GATEWAY \
  -o com.docker.network.bridge.name=docker_gwbridge \
  -o com.docker.network.bridge.enable_icc=false \
  -o com.docker.network.bridge.enable_ip_masquerade=true \ 
  docker_gwbridge
docker swarm init      # now start the swarm
docker network inspect docker_gwbridge   # if you want to see your changes
docker stack deploy --compose-file yourFile.yml YOURSTACKNAME

现在,您的所有容器都在您定义的子网以及您指定的网关上启动。

于 2017-08-10T14:53:39.727 回答
0

compose v3 中的网络配置是:

networks:
  my_network:
    driver: overlay
    ipam:
      driver: default
      config:
      -
        subnet: 172.28.0.0/16

然后,您可以将容器分配到此网络并指定 IP

services
    my_service:
        networks:
            my_network:
                 ipv4_address: 172.28.0.100

对于已定义的网络,请使用:

networks:
  default:
    external:
      name: my-pre-existing-network
于 2017-08-10T12:18:18.183 回答