Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize Docker build with caching + fix build issues #55

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,40 +1,68 @@
ARG BUILDER_DIR=/srv/lnp
# This image uses cargo-chef to build the application in order to compile
# the dependencies apart from the main application. This allows the compiled
# dependencies to be cached in the Docker layer and greatly reduce the
# build time when there isn't any dependency changes.
#
# https://github.com/LukeMathWalker/cargo-chef

ARG SRC_DIR=/usr/local/src/lnp
ARG BUILDER_DIR=/srv/lnp

FROM rust:1.59.0-slim-buster as builder
# Base image
FROM rust:1.59.0-slim-bullseye as chef

ARG SRC_DIR=/usr/local/src/lnp
ARG SRC_DIR
ARG BUILDER_DIR

WORKDIR "$SRC_DIR"
RUN apt-get update && apt-get install -y build-essential

RUN rustup default stable
RUN rustup update
RUN cargo install cargo-chef --locked

COPY doc ${SRC_DIR}/doc
COPY shell ${SRC_DIR}/shell
COPY src ${SRC_DIR}/src
COPY build.rs Cargo.lock Cargo.toml codecov.yml config_spec.toml \
LICENSE license_header README.md ${SRC_DIR}/
WORKDIR $SRC_DIR

WORKDIR ${SRC_DIR}
# Cargo chef step that analyzes the project to determine the minimum subset of
# files (Cargo.lock and Cargo.toml manifests) required to build it and cache
# dependencies
FROM chef AS planner

RUN mkdir "${BUILDER_DIR}"
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

RUN cargo install --path . --root "${BUILDER_DIR}" --bins --all-features
FROM chef AS builder

ARG SRC_DIR
ARG BUILDER_DIR

FROM debian:buster-slim
COPY --from=planner "${SRC_DIR}/recipe.json" recipe.json

# Build dependencies - this is the caching Docker layer
RUN cargo chef cook --release --recipe-path recipe.json --target-dir "${BUILDER_DIR}"

# Copy all files and build application
COPY . .
RUN cargo build --release --target-dir "${BUILDER_DIR}" --bins --all-features

# Final image with binaries
FROM debian:bullseye-slim as final

ARG BUILDER_DIR
ARG BIN_DIR=/usr/local/bin
ARG DATA_DIR=/var/lib/lnp
ARG USER=lnpd
ARG DATA_DIR=/var/lib/lnpd
ARG USER=lnp

RUN adduser --home "${DATA_DIR}" --shell /bin/bash --disabled-login \
--gecos "${USER} user" ${USER}

COPY --from=builder --chown=${USER}:${USER} \
"${BUILDER_DIR}/bin/" "${BIN_DIR}"
"${BUILDER_DIR}/release" "${BIN_DIR}"

WORKDIR "${BIN_DIR}"

# Remove build artifacts in order to keep only the binaries
RUN rm -rf */ *.d .[^.] .??*

USER ${USER}

VOLUME "$DATA_DIR"
Expand All @@ -44,4 +72,4 @@ EXPOSE 62962

ENTRYPOINT ["lnpd"]

CMD ["-vvv", "--data-dir", "/var/lib/lnp"]
CMD ["-vvv", "--data-dir", "/var/lib/lnpd"]