【问题标题】:Building a rust project with docker is extremely slow on google cloud使用 docker 构建 rust 项目在谷歌云上非常慢
【发布时间】:2021-03-07 21:24:14
【问题描述】:

我对 Rust 比较陌生,但我一直在 Docker 容器中工作。下面是我的 dockerfile,效果很好。我的构建使用中间容器在主项目之前构建所有货物容器。除非我更新依赖项,否则项目会在本地快速构建。即使重建了依赖项,在我的旧 macbook pro 上最多也不会花费超过 10 分钟。

FROM ekidd/rust-musl-builder as builder

WORKDIR /home/rust/

# Avoid having to install/build all dependencies by copying
# the Cargo files and making a dummy src/main.rs
COPY Cargo.toml .
COPY Cargo.lock .
RUN echo "fn main() {}" > src/main.rs
RUN cargo test
RUN cargo build --release

# We need to touch our real main.rs file or else docker will use
# the cached one.
COPY . .
RUN sudo touch src/main.rs

RUN cargo test
RUN cargo build --release

# Size optimization
RUN strip target/x86_64-unknown-linux-musl/release/project-name

# Start building the final image
FROM scratch
WORKDIR /home/rust/
COPY --from=builder /home/rust/target/x86_64-unknown-linux-musl/release/project-name .
ENTRYPOINT ["./project-name"]

但是,当我将我的项目设置为通过 google cloud build 从 github repo 自动构建时,我震惊地发现构建需要将近 45 分钟!我想如果我为中间容器正确设置了缓存,至少可以节省一些时间。即使构建器成功提取了缓存的图像,它似乎也没有使用它,并且总是从头开始构建中间容器。这是我的 cloudbuild.yaml:

steps:
  - name: gcr.io/cloud-builders/docker
    args:
      - "-c"
      - >-
        docker pull $_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest
        || exit 0
    id: Pull
    entrypoint: bash
  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - "-t"
      - "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
      - "--cache-from"
      - "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
      - .
      - "-f"
      - Dockerfile
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
    id: Push
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    args:
      - run
      - services
      - update
      - $_SERVICE_NAME
      - "--platform=managed"
      - "--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
      - >-
        --labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
      - "--region=$_DEPLOY_REGION"
      - "--quiet"
    id: Deploy
    entrypoint: gcloud
timeout: 3600s
images:
  - "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
options:
  substitutionOption: ALLOW_LOOSE

我正在寻找有关我在 cloudbuild.yaml 中做错了什么的任何信息,以及考虑到本地速度如此之快如何加快我的云构建的提示。理想情况下,我想坚持使用谷歌云,但如果有另一个 CI 服务可以更好地处理这样的 rust/docker 构建,我会愿意切换。

【问题讨论】:

    标签: docker google-cloud-platform rust continuous-integration


    【解决方案1】:

    这就是我在 Google Cloud Build 上改进 Rust 项目的构建时间所做的。不是一个完美的解决方案,但总比没有好:

    • 在 Docker 文件中与您的类似更改,为 deps 和我自己的源创建不同的缓存层。

    • 使用 kaniko 来利用缓存(这似乎是您的特殊问题)

      steps:
     - name: 'gcr.io/kaniko-project/executor:latest'
        args:
        - --destination=eu.gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA
        - --cache=true
        - --cache-ttl=96h
        timeout: 2400s
    

    文档:https://cloud.google.com/build/docs/kaniko-cache

    • 将机器类型更改为更高的选项,就我而言:
    options:
      machineType: 'E2_HIGHCPU_8'
    

    但请注意,更改机器类型会影响您的预算,因此您应该考虑这对于您的特定项目是否值得。

    如果您经常推送更改,效果会更好,但说实话仍然不够好。

    【讨论】:

    • 初始构建现在缩短到 10 分钟,重建缩短到 3 分钟。即使构建过程说有一个奇怪的错误,似乎工作正常。
    • 这里是错误:ERROR: failed to find one or more images after execution of build steps:
    • @iicaptain,我自己没有。也许您在步骤中使用 kaniko 推送您的镜像两次,>“Cloud Build 在构建容器镜像层时直接将它们上传到注册表,因此没有明确的推送步骤”。
    【解决方案2】:

    在速度方面需要考虑两件事:

    • 在您的(甚至是旧的)macbook pro 上,

      • 您拥有多核超线程 CPU
      • 在 Turbo 模式下 CPU 最高可达 3.5Ghz
    • 在云端构建

      • 每个构建只有一个 vCPU(默认情况下)
      • vCPU 是“服务器设计的 CPU”:没有高端性能,但性能稳定且一致,大约 2.1Ghz(在 turbo 模式下略高)

    因此,性能差异是显而易见的。为了加快构建速度,我建议您使用 the machine type option:

    ...
    ...
    options:
      substitutionOption: ALLOW_LOOSE
      machineType: 'E2_HIGHCPU_8'
    

    应该会更好!

    【讨论】:

    • 谢谢!我很欣赏这个答案,我肯定会试一试,但云构建的核心问题不是使用中间容器而是每次都从头开始构建是我的主要关注点。
    猜你喜欢
    • 2019-10-31
    • 2022-11-10
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2019-04-22
    • 2018-01-09
    相关资源
    最近更新 更多