Search code examples
dockerdocker-composedocker-swarmdocker-stack

Docker: How to control/define default gateway settings


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?


Solution

  • Finally figured this out and I am posting what I did in the hopes that it might help someone else. While I did not know this when I started, what I really wanted [needed? :) ] to do was to redefine the default settings of the docker_gwbridge network.

    This is how I did that:

    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
    

    Now all your containers start on the subnet you defined as well as using the gateway you specified.