Demystifying Docker Networking: A Clear Guide

Docker containers are great! Nobody can deny that, right? When you have used docker, have you ever wondered how the networking works behind the scenes. Usually, you just create a container and it magically has access to all the networking, but what is really happening behind the scenes? Did you know you can actually configure this networking?! Not only that, there are SIX ways you can do it!!! Isn't that insane?! Let's see what those six ways are.

Intro to Docker Networking

Docker networking is insane, but at the most basic level, it works like this. Each container has is in a docker network. Each network has a network driver. This is basically a networking type. There are several drivers you can pick from. That is what we are going to cover here shortly.

Bridge

The first network driver is bridge. This is the most basic driver. This is what you have been using without realizing. Each time you create a docker container and don't specify the network, it goes into something called the default network. This is a network that docker has already created from the moment you installed it. This default network has the bridge driver.

The bridge driver uses IP Masquerading to connect docker containers to the internet. The docker containers can also communicate with each-other as long as they are in the same network. This is why using the default network is not considered best practice. This means you do not have any sort of network isolation. This means that if a hacker is able to get access to your container, the can connect, and possibly hack, the other containers too. This can be disastrous!

MacVLAN

This is kind of a weird one. This driver makes it so each docker container has its own, individual, MAC address and IP address on the host network. You no longer need to expose ports, instead, just use the IP address of the container itself. The traffic will be routed accordingly.

There is a good chance you will have to change the configuration of your network because having several MAC addresses on a single port is usually a sign of a security threat.

IPVLAN

An IP VLAN, makes it look like all the containers are on the network of the host wit their own IP. It will look like containers are their own devices on the network,

IPVLAN vs. MacVLAN

An IPVLAN driver and a MacVLAN driver are both very similar, but they have 1 very important difference. It is that the IP VLAN assigns each container its own IP, but still uses the MAC address of the host. The MAC VLAN, on the other hand, gives each container its own MAC address and IP address.

There are many situations where IP VLANs are preferable over MAC VLANs because the switches do not like having several MAC addresses on a single port. I am not aware of any situation where using a MAC VLAN is really better than using an IP VLAN, but I am sure there are some.

Host

This is a very unique network driver. When you use the host network, the container and the host share a single networking namespace. You do not need to publish any ports, because any ports the container listens on will also be listened to on the host. The host and the container, both have the same ports.

This sort of thing is useful if you need a very high amount of performance. Using other networking types can degrade performance because of all the additional overhead present in the NAT or other things. They are also very useful when you need all the hosts to be exposed.

None

The none driver has very special use-cases. As the name implies, the none network means that there is no network connection for the container. It is completely isolated from the internet and anything else. It is best practice to use this for as many use cases as you can since this is the absolute most secure networking type. After all, the best way to secure a server is to not connect it to the internet in the first place.

Conclusion

Docker networking is a great skill. If you have read this far, you have that skill. You know the basics of docker networking. That is great! Not many people know that. This is one of the skills you can tell your future employers about. I know you are probably not very comfortable with docker networking right now. I will create a hands-on walk through that will help you gain confidence with this.

One more thing before you go. There is a lot to remember. I know it can be overwhelming. That is why I created an cheat-sheet. Here it is

Comments

Popular posts from this blog

Persistent Data in Docker: Explanation + Hands-On Demo

Pods to Deployments | Kubernetes Architecture Evolution

Docker Compose Explained: Simplifying Multi-Container Deployments