Skip to content

Commit

Permalink
chore: 🚀 use cargo-chef for Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Jul 1, 2024
1 parent 5abeaca commit 00a9b51
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 44 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.github/
.vscode/
target/
Dockerfile
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repository = "https://github.com/NJUPT-SAST/rsjudge"
rust-version = "1.77"

[workspace.dependencies]
log = "0.4.21"
log = "0.4.22"
rsjudge-traits = { version = "0.1.0", path = "crates/rsjudge-traits" }
rsjudge-utils = { version = "0.1.0", path = "crates/rsjudge-utils" }
serde = { version = "1.0.203", features = ["derive"] }
Expand Down Expand Up @@ -152,10 +152,10 @@ rsjudge-grpc = { version = "0.1.0", path = "crates/rsjudge-grpc", optional = tru
rsjudge-rest = { version = "0.1.0", path = "crates/rsjudge-rest", optional = true }

anyhow = "1.0.86"
clap = { version = "4.5.7", features = ["derive"] }
clap = { version = "4.5.8", features = ["derive"] }
env_logger = "0.11.3"
log.workspace = true
mimalloc = { version = "0.1.42", optional = true }
mimalloc = { version = "0.1.43", optional = true }
serde.workspace = true
sysinfo = { version = "0.30.12", features = ["serde"] }
tokio = { version = "1.38.0", features = ["fs", "rt-multi-thread", "macros", "process"] }
Expand All @@ -173,9 +173,9 @@ mimalloc = ["dep:mimalloc"]
default = ["grpc", "rest", "mimalloc"]

[build-dependencies]
clap = { version = "4.5.7", features = ["derive"] }
clap_complete = "4.5.6"
clap_mangen = "0.2.21"
clap = { version = "4.5.8", features = ["derive"] }
clap_complete = "4.5.7"
clap_mangen = "0.2.22"

[profile.release]
codegen-units = 1
Expand Down
67 changes: 49 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
FROM rust AS builder

FROM rust:slim AS chef
# We only pay the installation cost once,
# it will be cached from the second build onwards
RUN cargo install cargo-chef --locked
WORKDIR /src

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

RUN mkdir -p /root/.cargo cat >/root/.cargo/config <<EOF
FROM docker.io/bufbuild/buf:latest AS buf

FROM chef AS builder
COPY --from=buf /usr/local/bin/buf /usr/local/bin/buf
RUN <<EOF
apt-get update
apt-get install -y protobuf-compiler
rm -rf /var/lib/apt/lists/*
EOF
COPY --from=planner /src/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release --bin rsjudge

FROM debian:stable-slim AS rsjudge
RUN <<EOF
apt-get update
apt-get install -y libcap2-bin
rm -rf /var/lib/apt/lists/*

useradd --create-home \
--comment "Supervisor of rsjudge" \
--home /var/lib/rsjudge-supervisor/ \
--system \
--shell /sbin/nologin \
rsjudge-supervisor

useradd --create-home \
--comment "Builder of rsjudge" \
--home /var/lib/rsjudge-builder/ \
--system \
--shell /sbin/nologin \
rsjudge-builder

useradd --create-home \
--comment "Runner of rsjudge" \
--home /var/lib/rsjudge-runner/ \
--system \
--shell /sbin/nologin \
rsjudge-runner
EOF

RUN cargo build --release

FROM gcr.io/distroless/base-debian12

COPY --from=builder /src/target/release/rsjudge /app
COPY --from=builder /src/target/release/rsjudge /app/
RUN setcap "cap_setuid,cap_setgid,cap_dac_read_search=p" /app/rsjudge

USER rsjudge-supervisor

CMD ["/app/rsjudge"]

# FROM rust:1 as build-env
# WORKDIR /app
# COPY . /app
# RUN cargo build --release

# FROM gcr.io/distroless/cc-debian12
# COPY --from=build-env /app/target/release/hello-world-distroless /
# CMD ["./hello-world-distroless"]
2 changes: 1 addition & 1 deletion crates/rsjudge-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ serde.workspace = true
tokio = { version = "1.38.0", features = ["process"] }

[dev-dependencies]
serde_json = "1.0.117"
serde_json = "1.0.119"
toml = { version = "0.8.14", features = ["preserve_order"] }
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "A task runner for cargo workspaces"

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.7", features = ["derive"] }
clap = { version = "4.5.8", features = ["derive"] }
sh = "0.2.1"

[features]
Expand Down

0 comments on commit 00a9b51

Please sign in to comment.