-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dockerfile
59 lines (46 loc) · 2.26 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
FROM --platform=$BUILDPLATFORM rust:1.83.0@sha256:a45bf1f5d9af0a23b26703b3500d70af1abff7f984a7abef5a104b42c02a292b AS builder
ARG TARGET=x86_64-unknown-linux-musl
ARG APPLICATION_NAME
RUN rustup target add ${TARGET}
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
# borrowed (Ba Dum Tss!) from
# https://github.com/pablodeymo/rust-musl-builder/blob/7a7ea3e909b1ef00c177d9eeac32d8c9d7d6a08c/Dockerfile#L48-L49
RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \
dpkg --add-architecture arm64 && \
apt-get update && \
apt-get --no-install-recommends install -y \
build-essential \
musl-dev \
musl-tools \
libc6-dev-arm64-cross \
gcc-aarch64-linux-gnu
# The following block
# creates an empty app, and we copy in Cargo.toml and Cargo.lock as they represent our dependencies
# This allows us to copy in the source in a different layer which in turn allows us to leverage Docker's layer caching
# That means that if our dependencies don't change rebuilding is much faster
WORKDIR /build
RUN cargo new ${APPLICATION_NAME}
WORKDIR /build/${APPLICATION_NAME}
COPY .cargo ./.cargo
COPY Cargo.toml Cargo.lock ./
RUN --mount=type=cache,id=cargo-dependencies,target=/build/${APPLICATION_NAME}/target \
cargo build --release --target ${TARGET}
# now we copy in the source which is more prone to changes and build it
COPY src ./src
# --release not needed, it is implied with install
RUN --mount=type=cache,id=full-build,target=/build/${APPLICATION_NAME}/target \
cargo install --path . --target ${TARGET} --root /output
# Create a separate file because we don't want to copy over the
# whole one to our scratch container
RUN echo "root:x:0:0:root:/dev/null:/sbin/nologin" > /tmp/passwd
FROM scratch
ARG APPLICATION_NAME
# We're explicitely wanting to be root, because most consumers will just
# run the container expecting it to work. Since Docker runs as root, we match
# We fetch the passwrd file from the builder as we don't have sh here to create the file
COPY --from=builder /tmp/passwd /etc/passwd
USER root
WORKDIR /app
COPY --from=builder /output/bin/${APPLICATION_NAME} /app/entrypoint
ENV RUST_BACKTRACE=full
ENTRYPOINT ["/app/entrypoint"]