-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dockerfile
113 lines (77 loc) · 3.29 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
########################################
# Base Build Environment #
########################################
FROM rustlang/rust:nightly-bullseye as rust-base
RUN apt update \
&& apt upgrade -yq \
&& apt dist-upgrade -yq \
&& apt install -yq build-essential; \
apt install -yq gcc-aarch64-linux-gnu || apt install -yq gcc-x86-64-linux-gnu; \
apt purge -yq \
&& apt-get clean -yq \
&& apt-get autoclean -yq \
&& rm -rf /var/cache/apt/archives/* \
&& rustup toolchain install nightly \
&& rustup default nightly \
&& rustup target add aarch64-unknown-linux-gnu \
x86_64-unknown-linux-gnu \
aarch64-unknown-linux-musl \
x86_64-unknown-linux-musl \
&& rustup component add rust-src \
rust-std-aarch64-unknown-linux-gnu \
rust-std-aarch64-unknown-linux-musl \
rust-std-x86_64-unknown-linux-gnu \
rust-std-x86_64-unknown-linux-musl \
&& cargo install cargo-chef
########################################
# Cache Pre-Planner #
########################################
FROM rust-base as planner
WORKDIR /artifacts
COPY . /artifacts
# Create a "plan" for the dependency cache
RUN cargo chef prepare --recipe-path recipe.json
########################################
# Artifact Factory #
########################################
FROM planner as artifactory
ARG cargo_profile=release
WORKDIR /artifacts
# Copy over the previously created dependency cache plan
COPY --from=planner /artifacts/recipe.json recipe.json
# Execute the plan and cache the project's dependencies
RUN cargo chef cook --recipe-path recipe.json --profile ${cargo_profile}
# Copy over the project's "actual" source ...
COPY . .
# ... and compile it
RUN cargo build --profile ${cargo_profile} \
&& mv "$(pwd)/target/${cargo_profile}/pinnothera" "$(pwd)/pinnothera-bin" \
&& chmod +x "$(pwd)/pinnothera-bin"
#####################################
# Image Factory #
#####################################
# Use distroless image for production
FROM gcr.io/distroless/cc as pinn-image
COPY --from=precompiled-artifactory /artifacts/pinnothera-bin /usr/bin/pinnothera
CMD ["/usr/bin/pinnothera"]
########################################
# Precompiled Factory #
########################################
FROM alpine:latest as precompiled-artifactory
ARG profile='release'
ENV PINN_PROFILE=${profile}
WORKDIR /artifacts
# Copy over the precompiled binaries
COPY ./artifacts/ /artifacts/precompiled
RUN export TARGET_OS="$(uname -s | awk '{ print tolower($0) }')" \
&& export TARGET_ARCH="$(uname -m | sed 's#x86_64#amd64#g; s#aarch64#arm64#g')" \
&& mv "$(pwd)/precompiled/pinnothera-${TARGET_ARCH}-${TARGET_OS}-${PINN_PROFILE}" "$(pwd)/pinnothera-bin" \
&& rm -rf "$(pwd)/precompiled" \
&& chmod +x "$(pwd)/pinnothera-bin"
#####################################
# Image Factory #
#####################################
# Use distroless image for production
FROM gcr.io/distroless/cc as precompiled-pinn-image
COPY --from=precompiled-artifactory /artifacts/pinnothera-bin /usr/bin/pinnothera
CMD ["/usr/bin/pinnothera"]