Score:2

Docker compose - disable default gateway route

us flag

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?

Michael Hampton avatar
cz flag
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?
Boris avatar
us flag
@MichaelHampton Containers are running their own routing software
Michael Hampton avatar
cz flag
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.
in flag
If the containers are going to run their own routing software, why not just have an entrypoint remove the default route.
Score:0
in flag

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.

Paul avatar
cn flag
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).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.