Docker:如何控制/定义默认网关设置

Joe*_*oeG 5 docker docker-compose docker-swarm docker-stack

谁能解释一下 Docker Compose YML 文件是什么?我想做的就是能够控制各个容器的IP地址。我正在使用版本 3.1(但也尝试过 3.3,因为我最近升级到版本 17.06)。文档说:

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.
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我需要在检查网络时尊重该子网。然而,网关完全不同[请阅读上面的注释],因此容器不会启动。为什么他们在版本 3 中失去了(目前)在版本 2 中有效的功能?更糟糕的是,为什么在 3.2 或 3.3 版本中没有恢复这一点?

也许我在这里离基地很远——当然不是第一次!对我来说最重要的是:有没有办法修改撰写文件以允许命令docker stack deploy(在 a 中Docker Swarm)提供对所使用的网关和子网的控制?

Joe*_*oeG 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
Run Code Online (Sandbox Code Playgroud)

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