Docker is a powerful platform for containerizing and deploying applications, and its networking capabilities allow for the creation of isolated test networks and the management of containerized applications.
In some cases, however, containerized applications require a DHCP server to lease IP addresses to the containers running on the same network. By running a Dockerized DHCP server, you can simplify the deployment and management of your containerized applications, and create virtual networks for practicing networking concepts and configurations. In this article, we will walk through the steps for creating and using a Dockerized DHCP server for your applications and networks.
We will cover how to create a bridge network, run the DHCP server container, and configure your host and other containers to use the DHCP server to obtain IP addresses.
Choose a base image: You will need a base image for your DHCP server. In this example, we will use the Alpine Linux base image, which is a lightweight distribution of Linux that is popular for Docker images.
Install DHCP server software: Next, you will need to install the DHCP server software on your image. In this example, we will use the ISC DHCP server software, which is a widely used and well-supported DHCP server.
Configure the DHCP server: Once you have installed the DHCP server software, you will need to configure it to lease IPs. You will need to specify the range of IP addresses that can be leased, the subnet mask, and other network settings.
Create a Docker Network – I have called mine <my>-<network>
docker network create my-network
Create the DHCPD.CONF file in the build directory.
##########dhcpd.conf########### default-lease-time 259200; max-lease-time 777600; option domain-name "your-domain.com"; subnet 192.168.2.0 netmask 255.255.255.0{ range 192.168.2.2 192.168.2.250; option broadcast-address 192.168.2.255; option routers 192.168.2.1; option domain-name-servers 192.168.1.1; }
Create a Dockerfile: With the base image and DHCP server software installed and configured, you can now create a Dockerfile that will build the image. Here is an example Dockerfile:
Create a Dockerfile
FROM alpine:latest RUN apk add --no-cache dhcp COPY dhcpd.conf /etc/dhcpd.conf EXPOSE 67/udp ENTRYPOINT ["dhcpd", "-f", "-d", "--no-pid"]
In this Dockerfile, we start with the latest Alpine Linux image, then we install the ISC DHCP server software using the apk package manager. We copy a pre-configured dhcpd.conf file to the /etc directory, which contains the configuration settings for the DHCP server. We expose port 67/udp, which is the port used by DHCP servers to lease IP addresses. Finally, we set the ENTRYPOINT to start the dhcpd daemon with the specified options.
Build the image: Once you have created the Dockerfile, you can build the image using the docker build command:
docker build -t dhcp-server .
Run the container: With the image built, you can now run a container from the image using the docker run command:
docker run -d --name dhcp-server --net=host dhcp-server
In this command, we run the container in detached mode (-d), give it a name (–name dhcp-server), and use the host network (–net=host) so that the DHCP server can lease IPs to devices on the same network as the host. We specify the name of the image we built in the previous step (dhcp-server) as the container to run.
Your DHCP server container should now be running and leasing IPs to devices on your network. You can view the logs of the container using the docker logs command:
docker logs dhcp-server
And you can stop and remove the container using the docker stop and docker rm commands:
docker stop dhcp-server docker rm dhcp-server
There are several use cases for having a Docker image running as a DHCP server:
Development and testing: Developers and testers can use a Dockerized DHCP server to create isolated test networks for their applications or services. This allows them to test network configurations and connectivity without interfering with the production network.
Containerized applications: Some containerized applications require a DHCP server to lease IP addresses to the containers running on the same network. By running a Dockerized DHCP server, you can simplify the deployment and management of your containerized applications.
Education and training: DHCP servers are commonly used in networking courses and training programs. By running a Dockerized DHCP server, educators and students can create virtual networks for practicing networking concepts and configurations.
To get hosts to connect to the network served by the Dockerized DHCP server, you will need to configure the hosts to use DHCP to obtain an IP address. This can usually be done by configuring the network interface of the host to use DHCP. The exact steps to do this will depend on the operating system of the host.
For example, on a Linux host, you can configure the network interface to use DHCP by editing the /etc/network/interfaces file and adding the following lines
auto eth0 iface eth0 inet dhcp
On a Windows host, you can configure the network interface to use DHCP by going to the Control Panel, selecting Network and Sharing Center, selecting Change adapter settings, right-clicking on the network adapter, selecting Properties, selecting Internet Protocol Version 4 (TCP/IPv4), and selecting Obtain an IP address automatically.
Once the host is configured to use DHCP, it will automatically obtain an IP address from the Dockerized DHCP server when it is connected to the network.
You might rightly ask how these other containers or hosts get an IP address from the above DHCP server container.
Well, below is the answer to your question.
You would need to create a Docker network to add containers in there before they can receive IP addresses from the DHCP server. When you create a Docker network, you can specify that it is a bridge network, which is the default network type for Docker. Containers connected to a bridge network can communicate with each other using their IP addresses.
To create a bridge network, you can use the docker network create command. Here’s an example:
docker network create my-network
This command creates a bridge network named my-network. You can then start your DHCP server container on this network by using the –network option when running the container:
docker run -d --name dhcp-server --network my-network dhcp-server
This command starts the DHCP server container in detached mode (-d), names the container dhcp-server, and connects it to the my-network network.
Once your DHCP server container is running on the my-network network, you can start other containers on the same network by using the –network option:
docker run -d --name my-container --network my-network my-image
This command starts a container named my-container from the my-image image, and connects it to the my-network network.
When the container starts up, it will obtain an IP address from the DHCP server running on the my-network network. You can view the IP address of the container by using the docker inspect command:
docker inspect my-container
In the output, look for the IPAddress field under the NetworkSettings section. This will show you the IP address that was assigned to the container by the DHCP server.
Ubuntu has a good guide on DHCP – https://ubuntu.com/server/docs/network-dhcp