From 753f8f38222c99ed56d646a97cb10ff2f8e036ea Mon Sep 17 00:00:00 2001 From: nickfarrow Date: Fri, 14 Oct 2022 16:51:35 -0400 Subject: [PATCH] Multistage cargo build for x86 and ARM Conditionally `cargo build` for platforms of x86_64 or ARM. In the final Docker stage we copy the built binary to alpine, and use a run_nolooking script to start nolooking with the environment: - $LND_HOST, $LND_GRPC_PORT, $TLS_FILE, $MACAROON_FILE" - Binds to 0.0.0.0:4444 (exposed within docker) Resolves #4 --- Dockerfile | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ run_nolooking | 5 +++++ 2 files changed, 66 insertions(+) create mode 100644 Dockerfile create mode 100755 run_nolooking diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..acfa1b5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,61 @@ +# Multistage Build for Nolooking +# +# x86_64-unknown-linux-musl +# aarch64-unknown-linux-musl +# +# Conditionally `cargo build` for platforms of x86_64 or ARM. +# Use musl for static linking, producing a standalone executable with no dependencies. +# In the final Docker stage we copy the built binary to alpine, and run with environment: +# $LND_HOST, $LND_GRPC_PORT, $TLS_FILE, $MACAROON_FILE" + +## Initial build Stage +FROM rustlang/rust:nightly AS builder +# Target architecture argument used to change build +ARG TARGETARCH +# Some nicer rust debugging +ENV RUSTFLAGS="-Z macro-backtrace" +ENV RUST_BACKTRACE=1 +# Copy the required build files. In this case, these are all the files that +# are used for both architectures. +WORKDIR /usr/src/nolooking/ +COPY Cargo.toml Cargo.lock build.rs config_spec.toml ./ +COPY src/ ./src/ + +## x86_64 +FROM builder AS branch-version-amd64 +RUN echo "Preparing to cargo build for x86_64 (${TARGETARCH})" +# Install the required dependencies to build for `musl` static linking +RUN apt-get update && apt-get install -y musl-tools musl-dev +# Add our x86 target to rust, then compile and install +RUN rustup target add x86_64-unknown-linux-musl +RUN cargo --config "net.git-fetch-with-cli=true" install --target x86_64-unknown-linux-musl --path . + +# ARM +FROM builder AS branch-version-arm64 +RUN echo "Preparing to cargo build for arm (${TARGETARCH})" +# Install the required dependencies to build for `musl` static linking for arm. +RUN apt-get update && apt-get install musl-tools clang llvm -y +# Add our arm target to rust, some build variables, then compile and install +RUN rustup target add aarch64-unknown-linux-musl +ENV CC_aarch64_unknown_linux_musl=clang +ENV AR_aarch64_unknown_linux_musl=llvm-ar +ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld" +RUN cargo --config "net.git-fetch-with-cli=true" install --target aarch64-unknown-linux-musl --path . + +# We build for either x86_64 or ARM from above options using the docker $TARGETARCH +FROM branch-version-${TARGETARCH} AS chosen_builder +RUN echo "Called build!" + +# Run nolooking from a final debian container +FROM debian:buster-slim +USER 1000 + +# Copy just the binary from our build stage +COPY --from=chosen_builder /usr/local/cargo/bin/nolooking /usr/local/bin/nolooking +COPY run_nolooking /usr/local/bin/run_nolooking +COPY --chown=1000:1000 public/ /usr/share/nolooking/public/ + +# Expose any necessary ports +EXPOSE 4444 +# Run +CMD ["run_nolooking"] diff --git a/run_nolooking b/run_nolooking new file mode 100755 index 0000000..a798a1a --- /dev/null +++ b/run_nolooking @@ -0,0 +1,5 @@ +#!/bin/sh + +echo "Running nolooking" +nolooking --bind-ip=0.0.0.0 --bind-port=4444 --lnd-address=https://lightning_lnd_1:$LND_GRPC_PORT --lnd-cert-path=$TLS_FILE --lnd-macaroon-path=$MACAROON_FILE --endpoint=http://$APP_HIDDEN_SERVICE +echo "nolooking exited!"