Bootstrap FreeKB - DHCP - Install DHCPD on Docker
DHCP - Install DHCPD on Docker

Updated:   |  DHCP articles

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

The docker pull command can be used to pull down the latest dhcpd image.

~]# docker pull networkboot/dhcpd
Using default tag: latest
latest: Pulling from networkboot/dhcpd
171857c49d0f: Pull complete
419640447d26: Pull complete
61e52f862619: Pull complete
9743765a89f2: Pull complete
a6b215f926d1: Pull complete
Digest: sha256:cc08468fbecfbe18002450620ede54d2d33892524076ac53c34361210740bcf6
Status: Downloaded newer image for networkboot/dhcpd:latest
docker.io/networkboot/dhcpd:latest

 

Or you could create Dockerfile so that the Dockerfile contains something like this.

FROM dhcpd:latest

 

Then use the docker build command to create the image, running this command in the same directory as the Dockerfile.

docker build . --tag dhcpd:latest

 

The docker images command can be used to display the DHCPD image.

~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED         SIZE
networkboot/dhcpd   latest    b57b32205814   10 months ago   79.3MB

 

Create the dhcpd.conf file on the Docker server.

touch /usr/local/docker/dhcpd/dhcpd.conf

 

The dhcpd.conf file should at least have the following.

  • option routers should be the IP address of the router in your network
  • option domain-name-servers should be the IP addresses of the DNS name servers that will be assigned to your clients
  • Range is the range of IP addresses that can be leased by clients. In this example, IP addresses 192.168.0.1 through 192.168.0.99 cannot be leased, so that these IP addresses can be used as static IP addresses for servers and infrastructure devices.
subnet 192.168.0.0 netmask 255.255.255.0 {
  option routers                  192.168.0.1;
  option subnet-mask              255.255.255.0;
  option domain-name-servers      192.168.0.5, 192.168.0.6;
  default-lease-time              43200;
  max-lease-time                  86400;
  range                           192.168.0.100 192.168.0.254;
}

 

The following command can then be used to create and start the dhcpd container. Let's break down this command.

  • The docker run command is used to create and start the dhcpd container.
  • The --detach flag is used to run the container in the background.
  • The --volume option is used to mount the /usr/local/docker/dhcpd directory on the Docker server to the /data directory in the Docker container. The /usr/local/docker/dhcp directory on the Docker container has the dhcpd.conf file.
  • The --name option is used to name the container dhcpd.
  • The --network host option is used to so that the Docker server (not the container) network is used.
  • The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
  • The networkboot/dhcpd image is used.
  • The eth0 option us used to use the eth0 network interface
docker run 
--detach
--volume /usr/local/docker/dhcpd:/data
--network host
--name dhcpd
--restart unless-stopped
networkboot/dhcpd
eth0

 

Use the docker container ls command to ensure the container is running.

~]# docker container ls -a
CONTAINER ID   IMAGE               COMMAND            CREATED       STATUS       NAMES
ba2fff144f7f   networkboot/dhcpd   "/entrypoint.sh"   3 hours ago   Up 3 hours   dhcpd

 

The docker logs command should return something like this.

~]# docker logs dhcpd
Internet Systems Consortium DHCP Server 4.3.5
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /data/dhcpd.conf
Database file: /data/dhcpd.leases
PID file: /var/run/dhcpd.pid
Wrote 0 leases to leases file.
Listening on LPF/eth0/00:0c:29:ad:31:5d/192.168.0.0/24
Sending on   LPF/eth0/00:0c:29:ad:31:5d/192.168.0.0/24
Sending on   Socket/fallback/fallback-net
Server starting service.

 

To confirm that the DHCPD service is working, the /data/dhcpd.leases file should have something like this.

~]# docker exec dhcpd cat /data/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.3.5

# authoring-byte-order entry is generated, DO NOT DELETE
authoring-byte-order little-endian;

lease 192.168.0.100 {
  starts 1 2021/09/06 21:37:20;
  ends 1 2021/09/06 21:39:20;
  cltt 1 2021/09/06 21:37:20;
  binding state free;
  hardware ethernet 08:78:08:3c:81:b8;
  uid "\001\010x\010<\201\270";
  set vendor-class-identifier = "android-dhcp-9";
  client-hostname "Galaxy-Tab-A-2017";
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c2b39b in the box below so that we can be sure you are a human.