Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 37721e8

Browse files
committed
Initial code commit
1 parent b2c4b6f commit 37721e8

File tree

8 files changed

+499
-1
lines changed

8 files changed

+499
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/data

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM ubuntu
2+
3+
MAINTAINER Robin Smidsrød <[email protected]>
4+
5+
RUN apt-get -q -y update \
6+
&& apt-get -q -y -o "DPkg::Options::=--force-confold" -o "DPkg::Options::=--force-confdef" dist-upgrade \
7+
&& apt-get -q -y -o "DPkg::Options::=--force-confold" -o "DPkg::Options::=--force-confdef" install isc-dhcp-server man \
8+
&& apt-get -q -y autoremove \
9+
&& apt-get -q -y clean \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
COPY util/my_init.py /sbin/my_init
13+
COPY util/entrypoint.sh /entrypoint.sh
14+
ENTRYPOINT ["/entrypoint.sh"]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2015 Robin Smidsrød <[email protected]>
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Docker image for ISC DHCP server
2+
================================
3+
4+
This Docker image is suitable for running a DHCP server for your docker host
5+
network. It uses ISC DHCP server which is bundled with the latest Ubuntu
6+
LTS distribution.
7+
8+
How to build
9+
============
10+
11+
1. Install Docker with the instructions on <https://www.docker.com>.
12+
2. Run `./build` to create the local image `dhcpd`.
13+
14+
How to use
15+
==========
16+
17+
The most common use-case is to provide DHCP service to the host network of
18+
the machine running Docker. For that you need to create a configuration for
19+
the DHCP server, start the container with the `--net=host` docker run
20+
option and specify the network interface you want to provide DHCP service
21+
on.
22+
23+
1. Create `data` folder.
24+
2. Create `data/dhcpd.conf` with a subnet clause for the specified
25+
network interface. If you need assistance, you can run
26+
`docker run -it --rm dhcpd man dhcpd.conf` for a description of
27+
the configuration file syntax.
28+
3. Run `docker -it --rm --net=host -v "$(pwd)/data":/data dhcpd eth0`.
29+
`dhcpd` will automatically start and display its logs on the console.
30+
You can press Ctrl-C to terminate the server.
31+
32+
A simple `run` script is also included which makes it quick to iterate on a
33+
configuration until you're satisfied.
34+
35+
Notes
36+
=====
37+
38+
The entrypoint script in the docker image takes care of running the DHCP
39+
server as the same user that owns the `data` folder. This ensures that the
40+
permissions on the files inside the `data` folder is kept consistent. If
41+
the `data` folder is owned by root, dhcpd is run as the normal dhcpd user.
42+
43+
If you forget to run the docker container with the `--net=host` option a
44+
warning will be emitted informing you that you've probably forgotten it.
45+
46+
If a `/data` volume is not provided with a `dhcpd.conf` inside it, the
47+
container will exit early with an error message.
48+
49+
Acknowledgements
50+
================
51+
52+
This image uses the following software components:
53+
54+
* Ubuntu Linux distribution from <https://www.ubuntu.com>.
55+
* ISC DHCP server from <https://www.isc.org/downloads/dhcp/>.
56+
* Docker-optimized my_init from <https://github.com/phusion/baseimage-docker>.
57+
58+
Copyright & License
59+
===================
60+
61+
This project is copyright 2015 Robin Smidsrød <[email protected]>.
62+
63+
It is licensed under the Apache 2.0 license.
64+
65+
See the file LICENSE for full legal details.

build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
docker build -t dhcpd "$@" $(dirname $0)

run

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
pushd $(dirname $0) >/dev/null
3+
data_dir="$(pwd)/data"
4+
docker run -ti --rm --net host -v "$data_dir":/data dhcpd "$@"
5+
popd >/dev/null

util/entrypoint.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Single argument to command line is interface name
5+
if [ -n "$1" ]; then
6+
if [ -e "/sys/class/net/$1" ]; then
7+
IFACE=$1
8+
fi
9+
fi
10+
11+
# No arguments mean all interfaces
12+
if [ -z "$1" ]; then
13+
IFACE=" "
14+
fi
15+
16+
init="/sbin/my_init --skip-startup-files --skip-runit"
17+
18+
if [ -n "$IFACE" ]; then
19+
# Run dhcpd for specified interface or all interfaces
20+
21+
data_dir="/data"
22+
if [ ! -d "$data_dir" ]; then
23+
echo "Please ensure '$data_dir' folder is available."
24+
echo 'If you just want to keep your configuration in "data/", add -v "$(pwd)/data:/data" to the docker run command line.'
25+
exit 1
26+
fi
27+
28+
dhcpd_conf="$data_dir/dhcpd.conf"
29+
if [ ! -r "$dhcpd_conf" ]; then
30+
echo "Please ensure '$dhcpd_conf' exists and is readable."
31+
echo "Run the container with arguments 'man dhcpd.conf' if you need help with creating the configuration."
32+
exit 1
33+
fi
34+
35+
uid=$(stat -c%u "$data_dir")
36+
gid=$(stat -c%g "$data_dir")
37+
if [ $gid -ne 0 ]; then
38+
groupmod -g $gid dhcpd
39+
fi
40+
if [ $uid -ne 0 ]; then
41+
usermod -u $uid dhcpd
42+
fi
43+
44+
[ -e "$data_dir/dhcpd.leases" ] || touch "$data_dir/dhcpd.leases"
45+
chown dhcpd:dhcpd "$data_dir/dhcpd.leases"
46+
if [ -e "$data_dir/dhcpd.leases~" ]; then
47+
chown dhcpd:dhcpd "$data_dir/dhcpd.leases~"
48+
fi
49+
50+
container_id=$(grep docker /proc/self/cgroup | sort -n | head -n 1 | cut -d: -f3 | cut -d/ -f3)
51+
if perl -e '($id,$name)=@ARGV;$short=substr $id,0,length $name;exit 1 if $name ne $short;exit 0' $container_id $HOSTNAME; then
52+
echo "You must add the 'docker run' option '--net=host' if you want to provide DHCP service to the host network."
53+
fi
54+
55+
exec $init -- /usr/sbin/dhcpd -4 -f -d --no-pid -cf "$data_dir/dhcpd.conf" -lf "$data_dir/dhcpd.leases" $IFACE
56+
else
57+
# Run another binary
58+
exec $init -- "$@"
59+
fi

0 commit comments

Comments
 (0)