优化Docker中的货物建造时间 [英] Optimising cargo build times in Docker

查看:79
本文介绍了优化Docker中的货物建造时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rust开发API,并正在使用Docker管理环境,包括外部数据库。每次我更改API代码时,都会进行货物重建,并且由于Docker不缓存与 ADD 语句有关的任何内容,从而将Rust目录复制到容器中,它会重新下载所有软件包,这是一个相当漫长的过程,因为我使用的是Nickel,它似乎有很多依赖关系。

I am developing an API with Rust, and am managing the environments, including the external database with Docker. Every time I make a change to the API code, cargo rebuilds, and since Docker doesn't cache anything to do with the ADD statement to copy the Rust directory over to the container, it re-downloads all the packages, which is a fairly lengthy process since I'm using Nickel, which seems to have a boatload of dependencies.

有没有办法在运行货物建造之前引入这些依赖关系?至少以这种方式,如果依赖项发生更改,它将仅安装所需的内容,类似于在本地进行Cargo编译。

Is there a way to bring those dependencies in prior to running cargo build? At least that way if the dependencies change it will only install what's required, similar to Cargo compiling locally.

以下是我当前使用的Dockerfile:

Here's the Dockerfile I currently use:

FROM ubuntu:xenial
RUN apt-get update && apt-get install curl build-essential ca-certificates file xutils-dev nmap -y
RUN mkdir /rust
WORKDIR /rust
RUN curl https://sh.rustup.rs -s >> rustup.sh
RUN chmod 755 /rust/rustup.sh
RUN ./rustup.sh -y
ENV PATH=/root/.cargo/bin:$PATH SSL_VERSION=1.0.2h
RUN rustup default 1.11.0
RUN curl https://www.openssl.org/source/openssl-$SSL_VERSION.tar.gz -O && \
    tar -xzf openssl-$SSL_VERSION.tar.gz && \
    cd openssl-$SSL_VERSION && ./config && make depend && make install && \
    cd .. && rm -rf openssl-$SSL_VERSION*
ENV OPENSSL_LIB_DIR=/usr/local/ssl/lib \
    OPENSSL_INCLUDE_DIR=/usr/local/ssl/include \
    OPENSSL_STATIC=1
RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN cargo build
EXPOSE 20000
CMD ./target/debug/api

这是我的Cargo.toml

And here's my Cargo.toml

[profile.dev]
debug = true

[package]
name = "api"
version = "0.0.1"
authors = ["Vignesh Sankaran <developer@ferndrop.com>"]

[dependencies]
nickel = "= 0.8.1"
mongodb = "= 0.1.6"
bson = "= 0.3.0"
uuid = { version = "= 0.3.1", features = ["v4"] }


推荐答案

Docker确实缓存了根据 ADD (最好是 COPY )指令构建的层,前提是来源没有变化。您可以利用它并通过首先复制 Cargo.toml 并进行构建来缓存依赖项。

Docker does cache the layer built from the ADD (preferably COPY) instruction, provided the sources haven't changed. You could make use of that and get your dependencies cached by copying the Cargo.toml in first, and doing a build.

但是不幸的是,您需要构建一些东西,因此您可以使用单个源文件和一个虚拟的 lib 来实现。您清单中的目标:

But unfortunately you need something to build, so you could do it with a single source file and a dummy lib target in your manifest:

[lib]
name = "dummy"
path = "dummy.rs"

在您的Dockerfile中分别构建虚拟对象:

In your Dockerfile build the dummy separately:

COPY Cargo.toml /app/Cargo.toml
COPY dummy.rs /app/dummy.rs
RUN cargo build --lib

该层的输出将被缓存,并安装所有依赖项,然后您可以继续添加其余代码(在相同的 Dockerfile 中):

The output of this layer will be cached, with all the dependencies installed, and then you can go on to add the rest of your code (in the same Dockerfile):

COPY /src/ app/src/
RUN cargo build

虚拟东西很丑陋,但这意味着您的常规构建将很快,因为它来自缓存层,并且当您更改 Cargo.toml 然后Docker将选择它并使用更新的依赖关系构建一个新层ncies。

The dummy stuff is ugly, but it means your normal build will be quick, as it comes from the cached layer, and when you change dependencies in your Cargo.toml then Docker will pick it up and build a new layer with updated dependencies.

这篇关于优化Docker中的货物建造时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆