2

Is it possible to prevent docker from defining default route when using docker-compose yaml file?

If my docker-compose.yaml defines network ipam with default driver and any subnet, seams like docker (or docker compose) automatically assigns default route to the routing table of the docker that is attached to this network). Is there any way to disable it?

Boris
  • 173
  • 1
  • 10
  • Your containers will not be able to communicate with the outside world. Are you sure this is what you want? I suspect it is not. Why are you trying to do this? – Michael Hampton Sep 03 '21 at 15:30
  • @MichaelHampton Containers are running their own routing software – Boris Sep 03 '21 at 15:34
  • From a quick look at the docs I don't see any obvious way to do this. Maybe someone else will have a good idea. – Michael Hampton Sep 03 '21 at 15:40
  • If the containers are going to run their own routing software, why not just have an entrypoint remove the default route. – Zoredache Sep 03 '21 at 17:26

1 Answers1

0

Unfortunately: no.

A similar feature request (#20179) has been open at the GitHub repository for almost 6 years, so I truly believe that this feature is not being implemented any time soon.

My current workaround is, similar to what @Zoredache mentioned, to add a bash script to your containers and set the desired gateway IP address through environment variables. The script deletes the default route and adds it back with the custom IP as the gateway.

Edit: the essence of my script:

if [ -n "$GW" ]; then
    ip route delete default ;
    ip route add default via $GW ;
fi

If you do want to change the default gateway of your container you run the container with the appropriate environment variable GW=192.168.0.1 and the script takes care of the rest. Furthermore, make sure to include the script in either the CMDor ENTRYPOINT of your Dockerfile
NOTE: Requires the iproute2 package.

Might not be the prettiest solution, but it gets the job done.

derEddy
  • 1
  • 1
  • Welcome to Server Fault! It looks like you may have a workable solution to the question. Please consider [editing](https://serverfault.com/posts/1084873/edit) your answer to include the steps required to resolve the issue. And don't forget to take the [site tour](http://serverfault.com/tour). – Paul Nov 29 '21 at 19:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 30 '21 at 18:17