我遵循了 this 建议来构建 Rust docker 镜像。这是我当前的 Dockerfile:
FROM lukemathwalker/cargo-chef:latest-rust-alpine as chef
RUN apk update && \
apk add git cmake
WORKDIR /app
FROM chef AS planner
COPY ./Cargo.toml ./Cargo.lock ./
COPY ./src ./src
RUN cargo chef prepare
FROM chef AS builder
COPY --from=planner /app/recipe.json .
RUN cargo chef cook --release
COPY . .
RUN cargo build --release
RUN mv ./target/release/my_app ./app
FROM scratch AS runtime
WORKDIR /app
COPY --from=builder /app/app /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/app"]
但是我收到以下错误:
#13 91.72 CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
#13 91.72 thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/cmake-0.1.50/src/lib.rs:1098:5:
#13 91.72
#13 91.72 command did not execute successfully, got: exit status: 1
#13 91.72
#13 91.72 build script failed, must exit now
#13 91.72 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#13 91.72 warning: build failed, waiting for other jobs to finish...
#13 98.75 thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/cargo-chef-0.1.66/src/recipe.rs:218:27:
#13 98.75 Exited with status code: 101
#13 98.75 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#13 ERROR: process "/bin/sh -c cargo chef cook --release" did not complete successfully: exit code: 101
------
> [builder 2/5] RUN cargo chef cook --release:
91.72 thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/cmake-0.1.50/src/lib.rs:1098:5:
91.72
91.72 command did not execute successfully, got: exit status: 1
91.72
91.72 build script failed, must exit now
91.72 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
91.72 warning: build failed, waiting for other jobs to finish...
98.75 thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/cargo-chef-0.1.66/src/recipe.rs:218:27:
98.75 Exited with status code: 101
98.75 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
我可以通过计算机上的终端运行 Cargo build --release ,没有任何问题
我最终进行了以下设置:
FROM rust:1.76.0-slim-bullseye as build
RUN apt-get update -y && \
apt-get install -y cmake libclang-dev musl-tools git g++
RUN rustup target add x86_64-unknown-linux-musl
# create a new empty shell project
RUN USER=root cargo new --bin app
WORKDIR /app
# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
# 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/*
RUN cargo build --release
# our final base
FROM debian:bullseye-slim
WORKDIR app
# copy the build artifact from the build stage
COPY --from=build /app/target/release/arnak .
# set the startup command to run your binary
#RUN /app/arnak
CMD ["/app/arnak"]
EXPOSE 8000