Skip to content

Instantly share code, notes, and snippets.

@Fyko
Created July 25, 2020 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fyko/743b11b51b619837dbebcd3682ca8dda to your computer and use it in GitHub Desktop.
Save Fyko/743b11b51b619837dbebcd3682ca8dda to your computer and use it in GitHub Desktop.
Dockerfile that caches cargo packages

⚠ CRITICAL: I have only tested this with Compose, and you must set both DOCKER_BUILDKIT and COMPOSE_DOCKER_CLI_BUILD to 1.

Example:

DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose build
# syntax=docker/dockerfile-upstream:experimental
# WARNING: THE LINE ABOVE CANNOT BE REMOVED
# Note: Remember to change the workdir & paths that use "trakor/inkress"
# https://stackoverflow.com/q/54952867
# https://github.com/rust-lang/cargo/issues/2644#issuecomment-436907777
# --------------------------------
# building in this image
# --------------------------------
FROM alpine:3.12 as builder
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH \
RUST_VERSION=1.45.0
# install required dependencies
RUN apk add --update \
&& apk add --no-cache ca-certificates \
&& apk add --no-cache --virtual .build-deps curl gcc build-base libressl-dev openssl-dev
# install rust toolchain
RUN curl https://sh.rustup.rs -sSf > rustup-init \
&& chmod +x rustup-init \
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain $RUST_VERSION \
&& rm rustup-init
# setting the work dir
WORKDIR code/trakor/inkress
COPY . .
# Build with mounted cache
RUN --mount=type=cache,target=target \
--mount=type=cache,target=/usr/local/cargo/git \
--mount=type=cache,target=/usr/local/cargo/registry \
cargo build --release
# Copy binaries into normal layers
RUN --mount=type=cache,target=target \
cp ./target/release/inkress /usr/local/bin/inkress
# --------------------------------
# copy the binaries
# --------------------------------
FROM alpine:3.12
COPY --from=builder /usr/local/bin/inkress /usr/local/bin/inkress
COPY .env .
# RUN chmod +x /usr/local/bin/inkress
CMD ["inkress"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment