docker

Docker Network Configuration

Synology Mar 16, 2022

Docker networks allow you to organize your containers on different networks. It also allows you to assign static IP numbers to your containers. I will teach you how to create a docker network in this guide. I have written this guide for my other Synology posts to be the basis for how and where I run all my containers.

Goals

We want to create a docker environment which will be a combination of docker containers that will use a static IP and containers that will get an IP dynamically assigned by docker; however, we also want containers within this network to be able to communicate between each other regardless if the IP is manually assigned or dynamically. In conclusion, we can use this to communicate to your containers with a reverse proxy to make your containers externally available without opening ports on localhost.

Guide

We will create custom docker networks to run all of our containers to achieve our goals.

Prerequisites

TIP: IP Plan

As any IT professional will know, it’s essential to have documentation. Most IT professionals don’t like it, but it’s a necessary evil. Please write down your IP plan because when you are required to make changes, either now or somewhere in the future, you would like to see where you want to assign your new container quickly.

IP Plan

This guide will also be a prerequisite for other guides I’m writing; the plan I will lay out here will include this and some future articles I will note. The default docker network is of type ‘bridge.’ It will automatically assign IP numbers from the 172.17.0.0/16 range.

To avoid conflicts, we will create our docker network with different subnets. This will allow up to set static IP numbers to docker containers. Secondly, these networks will also have a dynamic range built-in so that if a container is started on this network without a static IP, the network will dynamically assign one.

You will see some /<number> notations in this guide after an IP number, these notations are called CIDR. This is for allocating IP ranges. I just wanted to let you know that you do not need to understand this to complete this guide. If you want to know more about it, you can read it on Wikipedia.

The networks I use and have described here follow a straightforward setup. I divide them in half. Meaning the first half of their IP range is for static containers. And the second half is for dynamically assigned containers.

IP RangeLabelDescription
172.20.0.xsystemThis will be the IP range for docker containers providing global services like management or core services which other containers will use. I like to call these my system services.
172.20.10.xgitlabThis will be the range where we are going to host our GitLab containers; I’m currently writing a complete series on how to host your own entire GitLab environment.
172.20.30.xmediaThis will be the range for media containers. I’m also in the process of writing an entire series about this.
172.20.50.xdevelopmentThis will be the range for all development containers.
IP Plan

System network

NameIP Range
IP Block172.20.0.0
IP Range Static (128 IP Addresses; CIDR: /25)172.20.0.0 – 172.20.0.127
IP Range Dynamic (128 IP Addresses; CIDR: /25 )172.20.0.128 – 172.20.0.255
Subnet172.20.0.0/24
IP Range172.20.0.127/25
Gateway172.20.0.1
System network details

GitLab network

NameIP Range
IP Block172.20.10.0
IP Range Static (128 IP Addresses; CIDR: /25)172.20.10.0 – 172.20.10.127
IP Range Dynamic (128 IP Addresses; CIDR: /25 )172.20.10.128 – 172.20.10.255
Subnet172.20.10.0/24
IP Range172.20.10.127/25
Gateway172.20.10.1
System network details

Media network

NameIP Range
IP Block172.20.30.0
IP Range Static (128 IP Addresses; CIDR: /25)172.20.30.0 – 172.20.30.127
IP Range Dynamic (128 IP Addresses; CIDR: /25 )172.20.30.128 – 172.20.30.255
Subnet172.20.30.0/24
IP Range172.20.30.127/25
Gateway172.20.30.1
Media network details

You will be able to add more networks by changing the IP numbers in the commands below.

Development network

NameIP Range
IP Block172.20.50.0
IP Range Static (128 IP Addresses; CIDR: /25)172.20.50.0 – 172.20.50.127
IP Range Dynamic (128 IP Addresses; CIDR: /25 )172.20.50.128 – 172.20.50.255
Subnet172.20.50.0/24
IP Range172.20.50.127/25
Gateway172.20.50.1
Development network details

You will be able to add more networks by changing the IP numbers in the commands below.

Create a docker network

Our IP plan has been created, so now we are ready to create our docker network. You must choose a name for your network, which we will use in our docker containers or docker-compose configuration files. In addition, this is lowercase by intention because of the future configuration of containers. If you need help with names here are some suggestions.

  • The hostname of your machine (Get this by running the hostname command on your terminal)
  • system
  • localhost
  • network

Now that we are ready to create our network, please remember that the CIDR in this command is meant to split the network in half. You can replace the SUBNET, IPRANGE, GATEWAY and NAME with the values from the IP Plan, or read on and copy over the command you need to create a certain network.

docker network create --subnet=SUBNET --ip-range=IPRANGE --gateway=GATEWAY NAME

Command Explanation

PartDescription
docker network createCommand to create a new docker network
--subnet=SUBNETThe subnet of the network
--ip-range=IPRANGEIP range from which to assign IP numbers dynamically
--gateway=GATEWAYGateway address to communicate with the rest of docker and the internet
NAMEName of the network
Command Explanation

Create system network

The command is a single line

docker network create --subnet=172.20.0.0/24 --ip-range=172.20.0.127/25 --gateway=172.20.0.1 system

Create GitLab network

The command is a single line

docker network create --subnet=172.20.10.0/24 --ip-range=172.20.10.127/25 --gateway=172.20.10.1 gitlab

Create media network

The command is a single line

docker network create --subnet=172.20.30.0/24 --ip-range=172.20.30.127/25 --gateway=172.20.30.1 media

Create development network

The command is a single line

docker network create --subnet=172.20.50.0/24 --ip-range=172.20.50.127/25 --gateway=172.20.50.1 development

Conclusion

In this guide, we have created a user-defined docker network which we can now use for all of my other guides. Please comment if you liked this, and let me know what you think about this guide and how you want me to improve it. In the same way, you can also send me an email with suggestions for topics.

Tags