在google cloud上用docker构建rust项目非常慢



我对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"]

然而,当我将我的项目设置为通过谷歌云构建从github仓库自动构建时,我震惊地发现构建几乎需要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

我正在寻找关于我在云构建中做错的任何信息。Yaml和如何加快我的云构建的提示,考虑到它在本地是如此之快。理想情况下,我想坚持使用谷歌云,但如果有另一个CI服务可以更好地处理rust/docker构建,我愿意切换。

这就是我在Google Cloud build上改进Rust项目构建时间的方法。这不是一个完美的解决方案,但聊胜于无:

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

  • 使用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'

要小心,改变机器类型会影响你的预算,所以你应该考虑是否值得为你的特定项目这么做。

如果你频繁地推动你的更改,这工作得更好,但仍然不够好,说实话。

在速度方面有两点需要考虑:

  • 在你的macbook pro(即使是旧的)上,

    • 你有多核超线程CPU
    • CPU在turbo模式下最高可达3.5Ghz
  • On Cloud Build

    • 每次构建只有一个vCPU(默认情况下)
    • vCPU是"服务器设计的cpu":没有高端性能,但性能稳定一致,在2.1Ghz左右(在turbo模式下略高)

因此,性能差异是明显的。为了加快构建速度,我建议使用机器类型选项:

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

应该更好!

最新更新