When creating containers inside a user-defined bridge network without specifying an IP address, the started containers are given IP addresses starting from the beginning of the IP range. When a container goes down, its IP address becomes available again and can later be used by another container. Docker also detects duplicate IPs and raises exceptions when invalid addresses are supplied. As far as my research goes, the docker daemon is not depending on any DHCP services. So how does Docker actually figure out which IP addresses are in use/available for a new container? Furthermore, how can a docker network plugin (such as docker-go-plugin
) do the same thing?
I think one of the keywords here is IPAM, but I don't know anything apart from that. I'd appreciate every piece of information that points me to the right direction.
Docker is a service. Whenever you start a container, it does so asking the Docker service to do all the necessary work. The IP addresses are defined whenever you create a docker network. Docker can also create new networks if you don't do so yourself. From what I've seen they use IPs in the 172.16.0.0 – 172.31.255.255 range. These are all private IP addresses. By default they start with 172.19.0.0 from what I've seen. You can also create your own networks with whatever IP range you'd like. Then add containers to that network and the next available IP will be used. Whenever you kill a container, its IP address becomes available again so the Docker service can re-add it to that list.
This Docker documentation says that you can consider this mechanism to be similar to having a DHCP although the Docker service takes care of the assignments.
I do not know how it's implemented. Probably a list, although they could be using a bitmap. For 65536 IPs, your map has to be 64Kb / 8 = 8Kb only, so it's very small. Each bit then tells you whether the IP is in use or not. However, if they have to support IPv6, such a map would not be practical. Way too large. They can also check the list of existing containers and try to assign the smallest possible IP which is not currently in use.