在 CentOS 容器里配置 Rust,一般分几种情况,下面给你最常用、最稳妥的做法。
如果你只是想用 Rust 编译/运行程序,最简单:
FROM rust:1.79
WORKDIR /app
COPY . .
RUN cargo build --release
运行:
docker build -t rust-app .
docker run --rm -it rust-app
✅ 优点
rustc / cargo如果你必须用 CentOS 基础镜像(如 centos:7 / centos:stream9)
docker run -it centos:7 bash
yum install -y gcc make curl
CentOS 8 / Stream 用
dnf替代yum
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选 1) Proceed with installation
source $HOME/.cargo/env
rustc --version
cargo --version
FROM centos:7
RUN yum install -y gcc make curl && \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
WORKDIR /app
COPY . .
RUN cargo build --release
centos:stream9yum install -y centos-release-scl
yum install -y devtoolset-11
scl enable devtoolset-11 bash
rustup target add x86_64-unknown-linux-musl
export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
如果你能说清楚:
我可以给你更精确的一套配置。