Skip to content

Instantly share code, notes, and snippets.

@johnmaguire
Last active July 20, 2023 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmaguire/9f305b5ac23ede90f45e68b1123d65e4 to your computer and use it in GitHub Desktop.
Save johnmaguire/9f305b5ac23ede90f45e68b1123d65e4 to your computer and use it in GitHub Desktop.
Example Dockerfiles for dnclient host networking

Building

Alpine-based image

docker build . \
    --file alpine.Dockerfile \
    --tag "dnclient:alpine-latest" \
    --tag "dnclient:latest"

Debian-based image

docker build . \
    --file debian.Dockerfile \
    --tag "dnclient:debian-latest" \
    --tag "dnclient:latest"

Running

To run the built image, use the following command:

docker run \
    --name dnclient \
    --cap-add NET_ADMIN \
    --network host \
    --volume defined:/etc/defined \
    --volume /dev/net/tun:/dev/net/tun \
    --env DN_ENROLLMENT_CODE="insert code here" \
    --rm \
    dnclient

A few notes:

  • The NET_ADMIN capability and /dev/net/tun volume are necessary to create the tun adapter on the host.
  • --volume defined:/etc/defined will persist the config to a named volume across runs.
  • --env DN_ENROLLMENT_CODE="insert code here" is necessary only for the first run. Get an enrollment code from https://admin.defined.net
FROM alpine:3
RUN apk add --update \
curl \
jq
RUN curl -o /usr/local/bin/dnclient $(curl https://api.defined.net/v1/downloads | jq -r '.data.dnclient.latest."linux-amd64"') && \
chmod +x /usr/local/bin/dnclient
VOLUME ["/etc/defined"]
COPY main.sh /main.sh
RUN chmod +x /main.sh
CMD ["/main.sh"]
FROM debian:11
RUN apt-get update && apt-get install -y \
curl \
jq
RUN curl -o /usr/local/bin/dnclient $(curl https://api.defined.net/v1/downloads | jq -r '.data.dnclient.latest."linux-amd64"') && \
chmod +x /usr/local/bin/dnclient
VOLUME ["/etc/defined"]
COPY main.sh /main.sh
RUN chmod +x /main.sh
CMD ["/main.sh"]
#!/bin/sh
HOST_CONFIG="/etc/defined/dnclient.yml"
CONTROL_SOCKET="/var/run/dnclient.sock"
dnclient run -server "${DN_API_SERVER:-https://api.defined.net}" &
for i in $(seq 1 11); do
if [ -S "$CONTROL_SOCKET" ]; then
break
fi
if [ "$i" -eq 11 ]; then
echo "Timed out."
exit 1
fi
echo "Waiting for dnclient $CONTROL_SOCKET ($i/10)..."
sleep 1
done
if [ ! -f "$HOST_CONFIG" ]; then
if [ -z "$DN_ENROLLMENT_CODE" ]; then
echo "Please provide an enrollment code using the DN_ENROLLMENT_CODE environment variable."
exit 1
fi
if ! dnclient enroll -code "$DN_ENROLLMENT_CODE"; then
echo "Enrollment failed."
exit 1
else
echo "Enrollment complete."
fi
fi
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment