This guide will help you configure a docker environment in which you will be able to run all of your containers on different subnets.

Goals

We want to create a docker environment which will be a combination of docker containers who will use a static IP and containers who will get an IP dynamically assigned by docker.

We also want containers within this network to be able to communicate between each other regardless if the IP is manually assigned or dynamically.

Guide

In order to achive what we want we are going to create a custom docker network on which we are going to run all of our containers.

TIP: Write down all your assigned IP ranges, for which you are planning to use them when you are required to make changes in the future you want to assign your container to the correct range.

Configuration Plan

Subnet: 172.20.0.0/16

Range Usage
172.20.0.x Containers which provide global services, like management or core services
172.20.10.x Gitlab containers
172.20.20.x + Other containers dynamically assigned by docker

If we are playing with new containers or services, we want them to be able to connect to our global services, which live in the IP range 172.20.0.x.

In order to have the containers which get a dynamically assigned IP on this network talk to the the other services we have to configure a Gateway on this network which provides this functionality.

When you do not provide a docker network to a container it will be started on the the default docker network. You can have these containers access the global services we are going to configure by using there statically assigned IP number.

I'm going to use 3 ranges, one for my main services, one for all my gitlab containers and the third range will be the rest.

If you want to have more ranges assigned,
then simply increase the last range 172.20.20.x

Example

Range Usage
172.20.0.x Containers which provide global services, like management or core services
172.20.10.x Gitlab containers
172.20.11.x Additional Range, has space for 100 static assigned containers
172.20.12.x Additional Range, has space for 100 static assigned containers
172.20.30.x Additional Range, has space for 100 static assigned containers
172.20.50.x + Other containers dynamically assigned by docker

The ranges we have defined are administrative only, meaning that we have to keep an administration to make sure we keep everything nice and organized.

Guide

We have to assign a name to our docker network. We will be using the hostname of the server in question. Because I'm running this on a Synology,
I will be using the name synology for my docker network.

Synology: docker commands has to be executed as root

Now we are ready to create our network.

docker network create --subnet=172.20.0.0/16 --ip-range=172.20.20.1/24 --gateway=172.20.0.1/24 synology

Command Explanation

Description
docker network create Command to create new docker network
--subnet=172.20.0.0/16 Subnet of the network
--ip-range=172.20.20.1/24 IP range from which to assign IP numbers
--gateway=172.20.0.1 Gateway address
synology Name of the network

The name of the network will be used in our docker configurations.

You are now ready to setup you global docker services.