Docker Community Forums

Share and learn in the Docker community.

Dhcpd on centos container

Hi all,
I’m trying to build/run my first image/container “centos-dhcp”, my Dockerfile is as follow:
FROM centos

RUN yum -y install dhcp-server

CMD systemctl start dhcpd

  • The issue is that when I try to run the image I get the following error:
    System has not been booted with systemd as init system (PID 1). Can’t operate.
    Failed to connect to bus: Host is down

Any idea why and how I can solve it?
Thank you

The command used to launch the DHCP server is

/usr/sbin/dhcpd -f -d -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid

which is what CentOS’ default systemd unit file /usr/lib/systemd/system/dhcpd.service uses, except the -d flag added to reconfigure logging to use stdout/stderr instead of SYSLOG.

The DHCP server needs access to the network interface informations to be able to interpret its configuration - in fact, the DHCP configuration is network interface agnostic and it’s up to the server to match the configurations with the actual network situation. If the DHCP service is meant to serve networks of the Docker host, it has to be given access to the host’s network interfaces with the Docker run flag --net host

The -cf flag tells the DHCP server to expect its configuration file to be located at /etc/dhcp/dhcpd.conf. This file could be added at build time using a

ADD <dhcpd_conf_filepath_on_host> /etc/dhcp/dhcpd.conf

instruction in the Dockerfile, where dhcpd_conf_filepath_on_host is the absolute filepath of the configuration file on the Docker host. However, that would imply that the image has to be rebuild every time the configuration changes. To avoid this, this image rather expects that the configuration file is mounted from the host at runtime using the Docker run flag

-v <dhcpd_conf_filepath_on_host>:/etc/dhcp/dhcpd.conf:ro

The …:ro flag tells Docker to mount it as read-only.

The DHCP server stores its leases in /var/lib/dhcpd. This folder should be made persistant to insure service continuation in case of DHCP server restarts (faulty or not). The easiest way is to use a Docker volume

-v <volume_name>:/var/lib/dhcpd

@lewish95 , Thank you for your reply.
I did try to change my Dockerfile according to what you explained , because I don’t want to type flags each time I want to run a container. I did create a local volume and try to map it to the container but not working : here is my Dockerfile
FROM centos

RUN yum -y update && yum -y install dhcp-server

VOLUME /mnt/dhcp/etc/dhcp/:/etc/dhcp/

VOLUME /mnt/dhcp/var/lib/dhcpd:/var/lib/dhcpd/

CMD /usr/sbin/dhcpd -f -d -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid eth0

Any idea how to mount in a Dockerfile ?