On a server I have an environment with different network namespaces that are created with the command sudo ip netns add ns_name. The physical interfaces of the server get assigned to different namespaces, in the "default" network namespace remains no physical interface.
On startup the docker daemon creates the docker0 bridge in the "default" network namespace. How to tell docker to create the docker0 bridge not in the default one but in one of the ones created by me?
My system is a (2023) recent install of Archlinux, but these instructions should be similar to your distribution:
Make sure docker is stopped when you start this:
Edit your docker systemd unit file, you can find it via
systemctl status docker.serviceModify the
[Service]part of the unit file/usr/lib/systemd/system/docker.service:Now refresh the configs and start docker with
If you're lucky, you can now try running a docker container and it will just work. There's a bit of weird shenanigans going on so you might get an error whenever you start any container like the one below:
This error appears to be from a misconfiguration between containerd and docker. Open the
containerdunit file and replace itsExecStartwith the following:This will tell containerd to use docker's configuration and your containers should work after you reload the configs restart all the services. Frankly, it is easier to just reboot the machine at this point.
As for what this does, we get an interesting series of effects.
dockerexecutable talks todockerdvia a unix socket, so it does not matter what namespace you rundockerordocker-composecommands from, as long as the socket is accessible.containerdis running in the default network namespace, which has all of the proper cgroups to create containers.dockerdis running inside your namespace, so it will create all network interfaces inside this namespace and all container networks will be generated inside this namespace by default.As a final note, unless you setup a service to create your network namespace on boot, you will probably want to leave docker disabled and have a startup script create the namespace and then start docker. In addition, you may need to run
chattr +ion your unit files to prevent updates from overwriting your config changes.