をみてもRustとかD言語みたいなのに使えますよとしか書いてないし、記事がどこにもありません。果たしてどうしたものか。

とりあえずhttps://techno-tanoc.github.io/posts/rust-multistage-build/ の記事を参考にさせてもらって

# select build image
FROM rust:1.34 as build

# create a new empty shell project
RUN USER=root cargo new --bin appname
WORKDIR /appname
# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml

RUN \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release


# this build step will cache your dependencies
RUN cargo build --release
RUN rm src/*.rs

# copy your source tree
COPY ./src ./src

# build for release
RUN rm ./target/release/deps/appname*
RUN cargo build --release

# our final base
FROM rust:1.34

# copy the build artifact from the build stage
COPY --from=build /appname/target/release/appname .

# set the startup command to run your binary
CMD ["./appname"]

のFROM rust:1.34をFROM gcr.io/distroless/ccに書き換えるだけ!

# select build image
FROM rust:1.34 as build

# create a new empty shell project
RUN USER=root cargo new --bin appname
WORKDIR /appname
# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml

RUN \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release


# this build step will cache your dependencies
RUN cargo build --release
RUN rm src/*.rs

# copy your source tree
COPY ./src ./src

# build for release
RUN rm ./target/release/deps/appname*
RUN cargo build --release

# our final base
FROM gcr.io/distroless/cc

# copy the build artifact from the build stage
COPY --from=build /appname/target/release/appname .

# set the startup command to run your binary
CMD ["./appname"]

イメージが3GBから22mbに減った!神!

广告
将在 10 秒后关闭
bannerAds