Docker – Set fixed subnet in nat network

Docker and navcontainerhelper do an excellent work setting a dns so that you don’t need to use an ip address to connect to your docker containers but use the containers name instead (be sure to use the -updateHosts when creating the container).

But, what if you want to have a network with fixed subnet and a fixed ip for your containers.

To have a network with fixed subnet, you have two options:

  1. Create a custom docker network and connect your containers to that network. Although this works, there are some caveats:
    • Whenever you restart your docker host, the custom docker networks are deleted and are not recreated (at least for Windows containers, in a Win 10 or Server 2019 host);
  2. Use the default nat network. This network is automatically created when the docker host is restarted so there’s no need to recreate it every time.
    • There’s a caveat though. Every time the host is restarted, the nat network usually is created in a different subnet.

Is it possible to setup the nat network to be created on a fixed ip subnet? Sure!

Setup fixed subnet in Docker’s default nat network

Add the following config to Docker’s daemon.json file (feel free to change the cidr to whatever subnet you want):

{
  "fixed-cidr": "172.16.1.0/24"
}

If the file does not exists then you have to create it. It should be created in the host machine, in the following path: C:\ProgramData\docker\config\daemon.json

You may do this using the following Powershell command, as long as the daemon.json file doesn’t exists yet.

Add-Content -path C:\ProgramData\docker\config\daemon.json -Value "{`r`n `"fixed-cidr`": `"172.16.1.0/24`"`r`n}"

You need to restart the server after this change, so that the nat network is recreated with the new subnet.

After the restart you can check if the docker network was created with the new subnet.

docker inspect nat

The docker containers are connected to this network, by default. So if you restart your docker containers, the container ip address will be in the new subnet.

Setup fixed ip for container

What if you want to configure a fixed ip address for the container?

If you’re creating a new container using navcontainerhelper you just need to add the following additional parameters flag:

-additionalParameters @("--network=<network>", "--ip <fixed ip address>")
Example: -additionalParameters @("--network=nat", "--ip 172.16.1.2")

The new container will have the ip 172.16.1.2.

What if you already have a container created?

Then you will have to run the following docker commands:

# If the container is connected to a different network, you'll have to disconnect it:
docker network disconnect <other network> <container name>
Example: docker network disconnect custom_nat bccontainer
# Then connect it to the network you want ("nat" in this case) with the fixed ip setting:
docker network connect --ip <fixed ip address> <network> <container name>
Example: docker network connect --ip 172.16.1.2 nat bccontainer

After this, restart the docker container:

docker restart <container name>
Example: docker restart bccontainer

Then check if the docker ip is correct:

docker inspect network <container name>
Example: docker inspect network bccontainer

The output is:

Please feel free to leave your comments.

businesscentral #navcontainerhelper #powershell #docker

Share this

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.