From 0f3dcfc57a898b16bc160035141086aa36dda29b Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 9 Aug 2020 17:49:13 -0400 Subject: [PATCH 1/4] Dockerfile --- .dockerignore | 11 +++++++++++ Dockerfile | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..67509c3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +.git +.github +extra +_config.yml +.gitignore +example.json +LICENSE +README.md +/target +**/*.rs.bk +*.vim diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bb0cd86 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +ARG ALPINE_VERSION=3.12 +ARG RUST_VERSION=1-alpine${ALPINE_VERSION} + +FROM rust:${RUST_VERSION} AS build +WORKDIR /usr/src/prometheus_wireguard_exporter + +# Setup +ARG ARCH=x86_64 +RUN apk add --update -q --no-cache musl-dev +RUN rustup target add ${ARCH}-unknown-linux-musl + +# Install dependencies +COPY Cargo.toml Cargo.lock ./ +RUN mkdir src && \ + echo "fn main() {}" > src/main.rs +RUN cargo build --release && \ + rm -rf target/release/deps/prometheus_wireguard_exporter* + +# Build the musl linked binary +COPY . . +RUN cargo build --release +RUN cargo install --target ${ARCH}-unknown-linux-musl --path . + +FROM alpine:${ALPINE_VERSION} +EXPOSE 9586/tcp +RUN apk add --update -q --no-cache wireguard-tools-wg +COPY --from=build --chown=1000 /usr/local/cargo/bin/prometheus_wireguard_exporter /usr/local/bin/prometheus_wireguard_exporter +ENTRYPOINT [ "prometheus_wireguard_exporter" ] +CMD [ "-h" ] +USER 1000 From 5cd14e0aa7ba1ce098042331443e4498be4c08e7 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 9 Aug 2020 18:06:11 -0400 Subject: [PATCH 2/4] Add Dockerfile and itself to .dockerignore --- .dockerignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.dockerignore b/.dockerignore index 67509c3..77dc51c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,7 +2,9 @@ .github extra _config.yml +.dockerignore .gitignore +Dockerfile example.json LICENSE README.md From 98c251bba8086cdc2641d0728546ca4f1306abe5 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 9 Aug 2020 18:06:23 -0400 Subject: [PATCH 3/4] Run as root to access wg --- Dockerfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index bb0cd86..c2c42cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,5 @@ RUN cargo install --target ${ARCH}-unknown-linux-musl --path . FROM alpine:${ALPINE_VERSION} EXPOSE 9586/tcp RUN apk add --update -q --no-cache wireguard-tools-wg -COPY --from=build --chown=1000 /usr/local/cargo/bin/prometheus_wireguard_exporter /usr/local/bin/prometheus_wireguard_exporter +COPY --from=build /usr/local/cargo/bin/prometheus_wireguard_exporter /usr/local/bin/prometheus_wireguard_exporter ENTRYPOINT [ "prometheus_wireguard_exporter" ] -CMD [ "-h" ] -USER 1000 From 01b7e16de037e029235079b7c340a797235cdc8f Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 9 Aug 2020 18:06:48 -0400 Subject: [PATCH 4/4] Readme instructions to use with Docker --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index ecdd72e..6f9acd2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,15 @@ A Prometheus exporter for [WireGuard](https://www.wireguard.com), written in Rus * You need [Rust](https://www.rust-lang.org/) to compile this code. Simply follow the instructions on Rust's website to install the toolchain. If you get weird errors while compiling please try and update your Rust version first (I have developed it on `rustc 1.42.0 (b8cedc004 2020-03-09)`). * You need [WireGuard](https://www.wireguard.com) *and* the `wg` CLI in the path. The tool will call `wg show |all dump` and of course will fail if the `wg` executable is not found. If you want I can add the option of specifying the `wg` path in the command line, just open an issue for it. +Alternatively, as long as you have Wireguard on your host kernel with some Wireguard interfaces running, you can use Docker. For example: + +```sh +docker build -t prometheusWireguardExporter https://github.com/mindflavor/prometheus_wireguard_exporter +docker run -it --rm --init --net=host --cap-add=NET_ADMIN prometheusWireguardExporter +# Check it's up +docker run -it --rm alpine:3.12 wget -qO- http://localhost:9586/metrics +``` + ## Compilation To compile the latest master version: