22# Rust image #
33# #################
44
5- FROM rust:1-bookworm AS rust
6-
5+ FROM rust:1-bookworm AS chef
76WORKDIR /usr/src/qcext-server
7+ RUN cargo install cargo-chef
8+
9+ FROM chef AS planner
10+ COPY . .
11+ RUN cargo chef prepare --recipe-path recipe.json
812
13+ FROM chef AS rust
914# Run SQLX in offline mode
1015ENV SQLX_OFFLINE=true
1116
12- # Use spare registry protocol
13- ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
14-
15- # Build the dependencies in a separate step to avoid rebuilding all of them
16- # every time the source code changes. This takes advantage of Docker's layer
17- # caching, and it works by doing a build using the Cargo.{toml,lock} files with
18- # dummy source code.
19- COPY Cargo.lock Cargo.toml /usr/src/qcext-server/
20- RUN mkdir -p /usr/src/qcext-server/database
21- COPY database/Cargo.toml database/sqlx-data.json /usr/src/qcext-server/database/
22- RUN mkdir -p /usr/src/qcext-server/shared
23- COPY shared/Cargo.toml /usr/src/qcext-server/shared/
24- RUN mkdir -p /usr/src/qcext-server/src && \
25- echo "fn main() {}" > /usr/src/qcext-server/src/main.rs
26- RUN mkdir -p /usr/src/qcext-server/database/src && \
27- touch /usr/src/qcext-server/database/src/lib.rs
28- RUN mkdir -p /usr/src/qcext-server/shared/src && \
29- touch /usr/src/qcext-server/shared/src/lib.rs
30- RUN cargo fetch
31- RUN cargo build --release
17+ # Build dependencies - this is the caching Docker layer!
18+ COPY --from=planner /usr/src/qcext-server/recipe.json recipe.json
19+ RUN cargo chef cook --release --recipe-path recipe.json
3220
33- # Dependencies are now cached, copy the actual source code and do another full
34- # build. The touch on all the .rs files is needed, otherwise cargo assumes the
35- # source code didn't change thanks to mtime weirdness.
36- RUN rm -rf /usr/src/qcext-server/src /usr/src/qcext-server/database/src /usr/src/qcext-server/shared/src
37- COPY src /usr/src/qcext-server/src
38- RUN rm -rf /usr/src/qcext-server/src/client
39- COPY database/src /usr/src/qcext-server/database/src
40- COPY database/migrations /usr/src/qcext-server/database/migrations
41- COPY shared/src /usr/src/qcext-server/shared/src
42- RUN find src -name "*.rs" -exec touch {} \; && \
43- find database/src -name "*.rs" -exec touch {} \; && \
44- find shared/src -name "*.rs" -exec touch {} \; && \
45- cargo build --release
21+ # Dependencies are now cached, build for real.
22+ COPY . .
23+ RUN cargo build --release
4624
4725# #################
4826# NodeJS image #
@@ -52,13 +30,22 @@ FROM debian:bookworm AS nodejs
5230
5331WORKDIR /usr/src/qcext-server
5432
33+ # Download and import the Nodesource GPG key
34+ RUN apt-get update && \
35+ apt-get install -y ca-certificates curl gnupg && \
36+ apt-get clean
37+ RUN mkdir -p /etc/apt/keyrings && \
38+ curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | \
39+ gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
40+
5541# Make sure we have npm and nodejs
56- RUN apt-get update
57- RUN DEBIAN_FRONTEND=noninteractive apt-get install -y curl
58- RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
59- RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs
60- RUN node --version
61- RUN npm --version
42+ ENV NODE_MAJOR=18
43+ RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
44+ RUN DEBIAN_FRONTEND=noninteractive \
45+ apt-get update && \
46+ apt-get install nodejs -y && \
47+ apt-get clean
48+
6249
6350# Next, let's run npm install
6451COPY package.json package-lock.json /usr/src/qcext-server/
0 commit comments