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?
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 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 theCMD
orENTRYPOINT
of your DockerfileNOTE: Requires the
iproute2
package.Might not be the prettiest solution, but it gets the job done.