0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DockerでDHCPサーバー(ipv4)を構築する

Posted at

目的

alpineの練習も兼ねて、記事を参考にしながらDHCPサーバーを構築する。今回はその方法を紹介する。ほとんど参考元サイトからの引用なので、本記事は補足資料として読んでいただきたい。

構築

構成

image.png

インターフェースの確認

今回はDocker network hostを用いるので、Dockerホスト上でインターフェース名を調査する

$ ip a
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:6c:04:4b brd ff:ff:ff:ff:ff:ff
    inet 172.24.20.34/24 brd 172.24.20.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe6c:44b/64 scope link
       valid_lft forever preferred_lft forever

今回はenp1s0を用いる。

volumeの作成

$ docker volume create v4

確認

$ docker volume ls
DRIVER    VOLUME NAME
local     v4

Dockerfile

Dockerfileの作成

FROM alpine:latest
RUN  apk add dhcp-server-vanilla tzdata && \
     touch /var/lib/dhcp/dhcpd.leases
ADD  entrypoint.sh /
ADD  dhcpd.conf /etc/dhcp/
RUN mkdir -p /run/dhcp/
RUN touch /run/dhcp/dhcpd.pid
ENTRYPOINT ["/entrypoint.sh"]

entorypoint.sh

#!/bin/ash
echo "Start container with parameter : $@"
trap sig_term SIGTERM
sig_term() {
    echo "CATCH SIGTERM"
    pkill -SIGTERM dhcpd
    wait
    exit 0
}
# Execute paramater.
exec "$@"
dhcpd -4 -f $DHCPIF &
wait
exit 1

$DHCPIFはdocker-compose.ymlにて値を代入する。今回の場合は以下の様にDHCPIF: "enp1s0"設定する。

dhcpdを起動した後にwaitを入れる事でdhcpdプロセスの終了を待つ。

dhcpd.conf

#-------------------------------
# Global options
#-------------------------------
authoritative;

option domain-name "hogeserver.hogeddns.jp";
option domain-name-servers 192.168.110.10;
option routers 192.168.110.10;

default-lease-time 86400; # 24 hours.
max-lease-time 604800; # 7 days.

lease-file-name "/var/lib/dhcp/dhcpd.leases";

#-------------------------------
# Subnet
#-------------------------------
subnet 192.168.110.0 netmask 255.255.255.0 {
    range 192.168.110.100 192.168.110.199;
}

Docker-compose.yml

docker-compose.yml
version: "3.9"
services:

  dhcp4sv:
    build: ./v4
    image: dhcp4:1.0.0
    container_name: dhcpsv4
    restart: "always"
    environment:
      TZ: Asia/Tokyo
      DHCPIF: "enp1s0"
    hostname: dhcp4
    network_mode: "host"
    tty: true


    volumes:
      - v4:/var/lib/dhcp


volumes:
  v4:

起動確認

dhcpsv4    | Start container with parameter :
dhcpsv4    | Internet Systems Consortium DHCP Server 4.4.3-P1
dhcpsv4    | Copyright 2004-2022 Internet Systems Consortium.
dhcpsv4    | All rights reserved.
dhcpsv4    | For info, please visit https://www.isc.org/software/dhcp/
dhcpsv4    | Config file: /etc/dhcp/dhcpd.conf
dhcpsv4    | Database file: /var/lib/dhcp/dhcpd.leases
dhcpsv4    | PID file: /run/dhcp/dhcpd.pid
dhcpsv4    | Wrote 0 leases to leases file.
dhcpsv4    | Listening on LPF/enp1s0/52:54:00:6c:04:4b/172.24.20.0/24
dhcpsv4    | Sending on   LPF/enp1s0/52:54:00:6c:04:4b/172.24.20.0/24
dhcpsv4    | Sending on   Socket/fallback/fallback-net

参考文献

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?