Setting the MTU on a docker network

in #docker7 years ago

At work and for my playground projects at home I fairly often use docker now. If I think about it, I use docker for almost everything I want to try - from cpuminer setup for cryptotokens to quick throwaway containers when I want to try software.

But today I wanted to know if - and how - I can set the maximum transmission unit (MTU) in a docker network. Sometimes this is necessary when you're dealing with VPN tunnels and other arcane network settings. So, how do you actually do it?

Create a new network

I don't want to interfere with other containers on my docker hosts, so I create a new network. MTU is specified via the option com.docker.network.driver.mtu on the command line:

$ docker network create --opt com.docker.network.driver.mtu=1452 testnet
f4b7d12052d393cd4deca48ed602c3af08c4dfb6184d7aee947de72a263e3ba6

If you need more information about docker networks, I suggest starting with the official documentation. It's really good and full of helpful advice.

Now I can verify if the setting actually sticks:

$ docker network inspect testnet                                        
[
    {
        "Name": "testnet",
        "Id": "f4b7d12052d393cd4deca48ed602c3af08c4dfb6184d7aee947de72a263e3ba6",
        "Created": "2018-01-31T08:02:28.205271074+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.driver.mtu": "1452"
        },
        "Labels": {}
    }
]

The custom option "com.docker.network.driver.mtu": "1452" is shown in the inspect output. All containers on this network should now get the new MTU size.

Run a container on the new network

Now, let's start a new container running Debian 9 and verify that the setting actually makes it through into the container:

$ docker run --rm -it --network testnet debian:9                   
root@d49d8ebdcf27:/# ip -4 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
57: eth0@if58: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1452 qdisc noqueue state UP group default  link-netnsid 0
    inet 172.22.0.2/16 brd 172.22.255.255 scope global eth0
       valid_lft forever preferred_lft forever

And there it is: mtu 1452 in the interface description. Now I'm set to use a container with custom MTU size in a restricted network environment.