Skip to content

Instantly share code, notes, and snippets.

@johnmaguire
Last active July 20, 2023 20:18
Show Gist options
  • Select an option

  • Save johnmaguire/9f305b5ac23ede90f45e68b1123d65e4 to your computer and use it in GitHub Desktop.

Select an option

Save johnmaguire/9f305b5ac23ede90f45e68b1123d65e4 to your computer and use it in GitHub Desktop.

Revisions

  1. johnmaguire revised this gist Jul 20, 2023. 3 changed files with 9 additions and 5 deletions.
    6 changes: 4 additions & 2 deletions alpine.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    FROM alpine:3

    RUN apk add --update curl
    RUN apk add --update \
    curl \
    jq

    RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux/amd64/dnclient && \
    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"]
    6 changes: 4 additions & 2 deletions debian.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    FROM debian:11

    RUN apt-get update && apt-get install -y \
    curl
    curl \
    jq

    RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux/amd64/dnclient && \

    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"]
    2 changes: 1 addition & 1 deletion main.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    HOST_CONFIG="/etc/defined/dnclient.yml"
    CONTROL_SOCKET="/var/run/dnclient.sock"

    dnclient run -server "$DN_API_SERVER" &
    dnclient run -server "${DN_API_SERVER:-https://api.defined.net}" &

    for i in $(seq 1 11); do
    if [ -S "$CONTROL_SOCKET" ]; then
  2. johnmaguire revised this gist Mar 16, 2023. 2 changed files with 6 additions and 6 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ docker run \
    --network host \
    --volume defined:/etc/defined \
    --volume /dev/net/tun:/dev/net/tun \
    --env ENROLLMENT_CODE="insert code here" \
    --env DN_ENROLLMENT_CODE="insert code here" \
    --rm \
    dnclient
    ```
    @@ -38,4 +38,4 @@ 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 ENROLLMENT_CODE="insert code here"` is necessary only for the first run. Get an enrollment code from https://admin.defined.net
    - `--env DN_ENROLLMENT_CODE="insert code here"` is necessary only for the first run. Get an enrollment code from https://admin.defined.net
    8 changes: 4 additions & 4 deletions main.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    HOST_CONFIG="/etc/defined/dnclient.yml"
    CONTROL_SOCKET="/var/run/dnclient.sock"

    dnclient run &
    dnclient run -server "$DN_API_SERVER" &

    for i in $(seq 1 11); do
    if [ -S "$CONTROL_SOCKET" ]; then
    @@ -20,12 +20,12 @@ for i in $(seq 1 11); do
    done

    if [ ! -f "$HOST_CONFIG" ]; then
    if [ -z "$ENROLLMENT_CODE" ]; then
    echo "Please provide an enrollment code using the ENROLLMENT_CODE environment variable."
    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 "$ENROLLMENT_CODE"; then
    if ! dnclient enroll -code "$DN_ENROLLMENT_CODE"; then
    echo "Enrollment failed."
    exit 1
    else
  3. johnmaguire revised this gist Mar 15, 2023. 2 changed files with 4 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions alpine.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@ RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux
    VOLUME ["/etc/defined"]

    COPY main.sh /main.sh
    RUN chmod +x /main.sh

    CMD ["/main.sh"]
    2 changes: 2 additions & 0 deletions debian.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,6 @@ RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux
    VOLUME ["/etc/defined"]

    COPY main.sh /main.sh
    RUN chmod +x /main.sh

    CMD ["/main.sh"]
  4. johnmaguire revised this gist Mar 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Building

    ## Alpine-based iamge
    ## Alpine-based image

    ```
    docker build . \
  5. johnmaguire revised this gist Mar 15, 2023. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,26 @@
    # Building

    To build a Docker image, run `docker build . --file alpine.Dockerfile --tag "dnclient:alpine-latest"` or `docker build . --file debian.Dockerfile --tag "dnclient:debian-latest"`
    ## Alpine-based iamge

    ```
    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 image, use the following:
    To run the built image, use the following command:

    ```
    docker run \
  6. johnmaguire revised this gist Mar 15, 2023. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,14 @@ To build a Docker image, run `docker build . --file alpine.Dockerfile --tag "dnc
    To run the image, use the following:

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

  7. johnmaguire created this gist Mar 15, 2023.
    25 changes: 25 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Building

    To build a Docker image, run `docker build . --file alpine.Dockerfile --tag "dnclient:alpine-latest"` or `docker build . --file debian.Dockerfile --tag "dnclient:debian-latest"`

    # Running

    To run the image, use the following:

    ```
    docker run
    --name dnclient
    --cap-add NET_ADMIN
    --network host
    --volume defined:/etc/defined
    --volume /dev/net/tun:/dev/net/tun
    --env 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 ENROLLMENT_CODE="insert code here"` is necessary only for the first run. Get an enrollment code from https://admin.defined.net
    11 changes: 11 additions & 0 deletions alpine.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    FROM alpine:3

    RUN apk add --update curl

    RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux/amd64/dnclient && \
    chmod +x /usr/local/bin/dnclient

    VOLUME ["/etc/defined"]

    COPY main.sh /main.sh
    CMD ["/main.sh"]
    12 changes: 12 additions & 0 deletions debian.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    FROM debian:11

    RUN apt-get update && apt-get install -y \
    curl

    RUN curl -o /usr/local/bin/dnclient https://dl.defined.net/02c6d0f9/v0.1.9/linux/amd64/dnclient && \
    chmod +x /usr/local/bin/dnclient

    VOLUME ["/etc/defined"]

    COPY main.sh /main.sh
    CMD ["/main.sh"]
    36 changes: 36 additions & 0 deletions main.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/sh

    HOST_CONFIG="/etc/defined/dnclient.yml"
    CONTROL_SOCKET="/var/run/dnclient.sock"

    dnclient run &

    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 "$ENROLLMENT_CODE" ]; then
    echo "Please provide an enrollment code using the ENROLLMENT_CODE environment variable."
    exit 1
    fi

    if ! dnclient enroll -code "$ENROLLMENT_CODE"; then
    echo "Enrollment failed."
    exit 1
    else
    echo "Enrollment complete."
    fi
    fi

    wait