温馨提示×

centos容器里怎配rust

小樊
55
2026-10-23 09:09:10
栏目: 编程语言

在 CentOS 容器里配置 Rust,一般分几种情况,下面给你最常用、最稳妥的做法。


一、基于官方 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 容器里手动安装 Rust

如果你必须用 CentOS 基础镜像(如 centos:7 / centos:stream9)

1️⃣ 启动 CentOS 容器

docker run -it centos:7 bash

2️⃣ 安装基础依赖

yum install -y gcc make curl

CentOS 8 / Stream 用 dnf 替代 yum

3️⃣ 安装 Rust(官方方式)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

按提示选 1) Proceed with installation

4️⃣ 加载环境变量

source $HOME/.cargo/env

5️⃣ 验证

rustc --version
cargo --version

三、在 Dockerfile 中安装(可复现)

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 7 太老,编译失败?

  • 建议用 centos:stream9
  • 或加:
yum 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

如果你能说清楚:

  • CentOS 版本(7 / 8 / Stream)
  • 用途(编译 / 运行 / CI)
  • 是否用 Dockerfile

我可以给你更精确的一套配置。

0 踩