27

我在 Rust + Actix-web 中有 hello world web 项目。我有几个问题。首先是代码的每次更改都会导致重新编译整个项目,包括下载和编译每个 crate。我想像在正常开发中一样工作——这意味着缓存已编译的 crate 并且只重新编译我的代码库。第二个问题是它不会暴露我的应用程序。无法通过网络浏览器访问

Dockerfile:

FROM rust

WORKDIR /var/www/app

COPY . .

EXPOSE 8080

RUN cargo run

码头工人-compose.yml:

version: "3"
services:
  app:
    container_name: hello-world
    build: .
    ports:
      - '8080:8080'
    volumes:
      - .:/var/www/app
      - registry:/root/.cargo/registry

volumes:
  registry:
    driver: local

main.rs:

extern crate actix_web;

use actix_web::{web, App, HttpServer, Responder};

fn index() -> impl Responder {
    "Hello world"
}

fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(web::resource("/").to(index)))
        .bind("0.0.0.0:8080")?
        .run()
}

货物.toml:

[package]
name = "hello-world"
version = "0.1.0"
authors = []
edition = "2018"

[dependencies]
actix-web = "1.0"
4

7 回答 7

30

似乎您并不是唯一一个努力通过 docker 构建过程缓存 rust 依赖项的人。这是一篇很棒的文章,可以帮助您一路走来:https ://blog.mgattozzi.dev/caching-rust-docker-builds/

它的要点是您首先需要一个 dummy.rs 和 Cargo.toml,然后构建它以缓存依赖项,然后稍后复制您的应用程序源,以免每次构建都使缓存无效。

Dockerfile

FROM rust
WORKDIR /var/www/app
COPY dummy.rs .
COPY Cargo.toml .
RUN sed -i 's#src/main.rs#dummy.rs#' Cargo.toml
RUN cargo build --release
RUN sed -i 's#dummy.rs#src/main.rs#' Cargo.toml
COPY . .
RUN cargo build --release
CMD ["target/release/app"]

CMD 应用程序名称“app”基于您在 Cargo.toml 中为二进制文件指定的内容。

假人.rs

fn main() {}

货运.toml

[package]
name = "app"
version = "0.1.0"
authors = ["..."]
[[bin]]
name = "app"
path = "src/main.rs"

[dependencies]
actix-web = "1.0.0"

src/main.rs

extern crate actix_web;

use actix_web::{web, App, HttpServer, Responder};

fn index() -> impl Responder {
    "Hello world"
}

fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(web::resource("/").to(index)))
        .bind("0.0.0.0:8080")?
        .run()
}
于 2019-10-20T15:44:20.883 回答
13

使用(仍处于试验阶段)Docker Buildkit,您最终可以在一个docker build步骤中正确缓存构建文件夹:

Dockerfile:

# syntax=docker/dockerfile:experimental
from rust
ENV HOME=/home/root
WORKDIR $HOME/app
[...]
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/home/root/app/target \
    cargo build --release

然后运行:

DOCKER_BUILDKIT=1 docker build . --progress=plain

随后的 docker 构建将重用缓存中的货物和目标文件夹,从而大大加快您的构建速度。

要清除 docker 缓存挂载:docker builder prune --filter type=exec.cachemount

如果您没有看到正确的缓存:如果您没有看到正确的缓存,请确保确认您的 cargo/registry 和 docker 映像中的目标文件夹的位置。

最小工作示例:https ://github.com/benmarten/sccache-docker-test/tree/no-sccache

于 2020-03-08T18:26:16.143 回答
8

您可以使用cargo-chef多阶段构建来利用 Docker 层缓存。

FROM rust as planner
WORKDIR app
# We only pay the installation cost once, 
# it will be cached from the second build onwards
RUN cargo install cargo-chef 
COPY . .
RUN cargo chef prepare  --recipe-path recipe.json

FROM rust as cacher
WORKDIR app
RUN cargo install cargo-chef
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json

FROM rust as builder
WORKDIR app
COPY . .
# Copy over the cached dependencies
COPY --from=cacher /app/target target
RUN cargo build --release --bin app

FROM rust as runtime
WORKDIR app
COPY --from=builder /app/target/release/app /usr/local/bin
ENTRYPOINT ["./usr/local/bin/app"]

它不需要 Buildkit,适用于简单的项目和工作区。您可以在发布公告中找到更多详细信息。

于 2020-10-25T20:29:49.517 回答
3

虽然electronix384128的答案非常好。我想通过添加.cargo/git使用 git 的任何依赖项所需的缓存以及添加多级 docker 示例来扩展它。

使用 rust-musl-builder 和 Docker Buildkit 功能,现在是 Docker Desktop 2.4 的默认设置。在其他版本上,您可能仍需要通过以下方式启用它:DOCKER_BUILDKIT=1 docker build .

rusl-musl-builder的工作目录已/home/rust/src
尝试设置 uid/gid,--mount但由于目标中的权限问题未能编译 rust。

# syntax=docker/dockerfile:1.2
FROM ekidd/rust-musl-builder:stable AS builder

COPY . .
RUN --mount=type=cache,target=/home/rust/.cargo/git \
    --mount=type=cache,target=/home/rust/.cargo/registry \
    --mount=type=cache,sharing=private,target=/home/rust/src/target \
    sudo chown -R rust: target /home/rust/.cargo && \
    cargo build --release && \
    # Copy executable out of the cache so it is available in the final image.
    cp target/x86_64-unknown-linux-musl/release/my-executable ./my-executable

FROM alpine
COPY --from=builder /home/rust/src/my-executable .
USER 1000
CMD ["./my-executable"]
于 2020-09-30T15:38:43.150 回答
1

我认为问题在于您的volumes定义没有进行绑定安装。我相信您当前的配置在容器关闭时正在复制、写入HOST ./registry/和丢弃内容。DOCKER /root/.cargo/registry/DOCKER /root/.cargo/registry/

相反,您需要指定bind卷上的类型:

version: "3"
services:
  app:
    container_name: hello-world
    build: .
    environment:
      - CARGO_HOME=/var/www/
    ports:
      - '8080:8080'
    volumes:
      - .:/var/www/app
      - type: bind
        source: ./registry
        target: /root/.cargo/registry

但是,请记住,/root/.cargo/.package-cache还会创建一个文件,但不会保留在此处。相反,您可以将sourceto./.cargo和 target 更改为/root/.cargo


对于我自己(主要是 cli)的 rust 项目,我喜欢使用我为它编写的插入式替换cargo,我已经确认在构建之间缓存包,从而大大减少构建时间。这可以复制以/usr/local/bin供全局使用,或./cargo build在单个项目中运行。但请记住,此特定脚本假定应用程序位于/usr/src/app容器内,因此可能需要针对您的使用进行调整。

于 2019-11-22T19:45:37.317 回答
1

这就是我所做的,它与构建脚本兼容。这是一个多阶段构建,因此它会生成一个小图像,但会将构建的依赖项缓存在第一个图像中。

FROM rust:1.43 AS builder

RUN apt-get update
RUN cd /tmp && USER=root cargo new --bin <projectname>
WORKDIR /tmp/<projectname>

# cache rust dependencies in docker layer
COPY Cargo.toml Cargo.lock ./
RUN touch build.rs && echo "fn main() {println!(\"cargo:rerun-if-changed=\\\"/tmp/<projectname>/build.rs\\\"\");}" >> build.rs
RUN cargo build --release

# build the real stuff and disable cache via the ADD
ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache
COPY ./build.rs ./build.rs

# force the build.rs script to run by modifying it
RUN echo " " >> build.rs
COPY ./src ./src
RUN cargo build --release

FROM rust:1.43
WORKDIR /bin
COPY --from=builder /tmp/<projectname>/target/release/server /bin/<project binary>
RUN chmod +x ./<project binary>
CMD ./<project binary>
于 2020-04-24T17:24:29.057 回答
1

根据@ckaseer的回答,您可以RUN echo "fn main() {}" > ./src/main.rs在构建应用程序之前构建依赖项。

首先复制您的Cargo.tomlCargo.lock文件并构建虚拟 main.rs 文件:

FROM rust as rust-builder
WORKDIR /usr/src/app

# Copy Cargo files
COPY ./Cargo.toml .
COPY ./Cargo.lock .

# Create fake main.rs file in src and build
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' > ./src/main.rs
RUN cargo build --release

然后您可以复制您的真实 src 目录并再次运行构建:

# Copy source files over
RUN rm -rf ./src
COPY ./src ./src

# The last modified attribute of main.rs needs to be updated manually,
# otherwise cargo won't rebuild it.
RUN touch -a -m ./src/main.rs

RUN cargo build --release

然后我们可以将我们的文件复制到精简版的debain。这是完整的 docker 文件:

FROM rust as rust-builder
WORKDIR /usr/src/app
COPY ./Cargo.toml .
COPY ./Cargo.lock .
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' > ./src/main.rs
RUN cargo build --release
RUN rm -rf ./src
COPY ./src ./src
RUN touch -a -m ./src/main.rs
RUN cargo build --release

FROM debian:buster-slim
COPY --from=rust-builder /usr/src/app/target/release/app /usr/local/bin/
WORKDIR /usr/local/bin
CMD ["app"]
于 2021-03-20T11:43:14.930 回答