LoginSignup
8
5

More than 3 years have passed since last update.

distrolessで作るRust軽量コンテナ

Last updated at Posted at 2019-06-21

をみても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に減った!神!

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5