#cargo-toml #docker #cache #cargo-lock

构建 dummy-cargo-toml-creater

用于 Docker 缓存的 dummy-cargo-toml-creater

3 个版本

0.1.6 2019年6月12日
0.1.5 2019年6月12日
0.1.2 2019年6月12日

#667 in 构建工具

MIT 许可证

14KB
71

dummy-cargo-toml-creater

用于 Docker 缓存的 dummy-cargo-toml-creater

动机

英文

虽然 Docker 构建很好,但只想缓存依赖包。但要这样做,首先需要在 Dockerfile 中写入 COPY Cargo.toml Cargo.lock. /。构建很好,但每次包本身的版本升级,例如,Cargo.tomlCargo.lock 的哈希值更改。即使是微小的错误修复,依赖包也需要重新构建。

因此,我们引入了一种称为 dummy-cargo-toml-creater 的技术。只需查看源代码 . / Src / main.rs,但只有 70 行代码。我们只实现了最小实现,以便不做任何额外或可疑的事情。

换句话说,通过不更改 Cargo.toml 和 Cargo.lock 的版本的方法,只有在安装依赖时才使用 0.1.0 固定包版本来解决问题。

最终镜像使用原始 Cargo.toml 和 Cargo.lock 构建,因此二进制中 cargo 包的版本没有问题,如果添加依赖包,Docker 的缓存也将正常工作。

日语

Docker 构建很好,但只想缓存依赖包。但是为了做到这一点,首先需要在 Dockerfile 中写入 COPY Cargo.toml Cargo.lock ./ 等,构建很好,但每次包本身的版本升级,例如,Cargo.tomlCargo.lock 的哈希值更改。即使是微小的错误修复,依赖包也需要重新构建。

因此,我们介绍了一种称为 dummy-cargo-toml-creater 的技术。只需查看源代码 ./src/main.rs,但只有 70 行代码。看起来没有做任何额外或可疑的事情,只实现了最小实现。

也就是说,通过不更改 Cargo.toml 和 Cargo.lock 的版本的方法,只有在安装依赖关系时才使用 0.1.0 固定包版本来解决问题。

最终的镜像使用原始 Cargo.toml 和 Cargo.lock 构建,因此二进制中的 cargo 包版本没有问题,如果添加依赖包,Docker 的缓存也将正常工作。

如何使用?

  1. 安装
$ cargo install dummy-cargo-toml-creater

1-a. 可能需要编写 gitignore DummyVersion.toml/lock

$ echo DummyVersion.toml >> ./.gitignore
$ echo DummyVersion.lock >> ./.gitignore
  1. 创建 DummyVersion.toml
$ ~/.cargo/bin/dummy-cargo-toml-creater
$ ls ./DummyVersion.toml
./DummyVersion.toml
$ ls ./DummyVersion.lock
./DummyVersion.lock
  1. 创建 Dockerfile

旧版本

FROM ekidd/rust-musl-builder:nightly-2019-04-25 as builder

RUN mkdir /tmp/app
WORKDIR /tmp/app
COPY . .
RUN sudo chown -R rust:rust .
RUN cargo build --release

新版本

FROM ekidd/rust-musl-builder:nightly-2019-04-25 as builder

## Build Cache Dependency Library
RUN mkdir /tmp/app
WORKDIR /tmp/app
## Build Dependency Library with DummyVersion.toml/lock
COPY DummyVersion.toml ./Cargo.toml
COPY DummyVersion.lock ./Cargo.lock
RUN mkdir -p src/ && \
    touch src/lib.rs
RUN sudo chown -R rust:rust .
RUN cargo build --release
## Build Base Library with Cargo.toml/lock
COPY ./src/ ./src/
COPY Cargo.toml ./Cargo.toml
COPY Cargo.lock ./Cargo.lock
RUN sudo chown -R rust:rust .
RUN cargo build --release
  1. 运行 docker build
$ $ docker build -t nnao45/dummy .
[+] Building 119.7s (18/18) FINISHED                                                                                           
 => [internal] load .dockerignore                                                                                         0.0s
 => => transferring context: 2B                                                                                           0.0s
 => [internal] load build definition from Dockerfile                                                                      0.0s
 => => transferring dockerfile: 584B                                                                                      0.0s
 => [internal] load metadata for docker.io/ekidd/rust-musl-builder:nightly-2019-04-25                                     2.5s
 => [1/12] FROM docker.io/ekidd/rust-musl-builder:nightly-2019-04-25@sha256:e12231fc754848ccf3865d1e4e80204125c6d77baaa9  0.0s
 => CACHED [2/12] RUN mkdir /tmp/app                                                                                      0.0s
 => CACHED [internal] helper image for file operations                                                                    0.0s
 => [internal] load build context                                                                                         0.1s
 => => transferring context: 24.98kB                                                                                      0.0s
 => [3/12] COPY DummyVersion.toml ./Cargo.toml                                                                            0.7s
 => [4/12] COPY DummyVersion.lock ./Cargo.lock                                                                            0.9s
 => [5/12] RUN mkdir -p src/ &&     touch src/lib.rs                                                                      1.6s
 => [6/12] RUN sudo chown -R rust:rust .                                                                                  0.7s
 => [7/12] RUN cargo build --release                                                                                     99.0s
 => [8/12] COPY ./src/ ./src/                                                                                             0.8s
 => [9/12] COPY Cargo.toml ./Cargo.toml                                                                                   1.2s
 => [10/12] COPY Cargo.lock ./Cargo.lock                                                                                  0.8s
 => [11/12] RUN sudo chown -R rust:rust .                                                                                 4.0s
 => [12/12] RUN cargo build --release                                                                                     2.7s
 => exporting to image                                                                                                    4.2s
 => => exporting layers                                                                                                   4.1s
 => => writing image sha256:e95b11c44810ef4abc3e5a18998b4ba20df9871d5dbcfdbf7a656d992e802857                              0.0s
 => => naming to docker.io/nnao45/dummy                                                                                   0.0s
  1. 修复 Cargo.toml
$ vi Cargo.toml
- version = "0.0.1"
+ version = "0.0.2"
  1. 重新运行 docker build
$ docker build -t nnao45/dummy .
[+] Building 14.2s (18/18) FINISHED                                                                                            
 => [internal] load build definition from Dockerfile                                                                      0.1s
 => => transferring dockerfile: 44B                                                                                       0.0s
 => [internal] load .dockerignore                                                                                         0.0s
 => => transferring context: 2B                                                                                           0.0s
 => [internal] load metadata for docker.io/ekidd/rust-musl-builder:nightly-2019-04-25                                     2.5s
 => [1/12] FROM docker.io/ekidd/rust-musl-builder:nightly-2019-04-25@sha256:e12231fc754848ccf3865d1e4e80204125c6d77baaa9  0.0s
 => CACHED [internal] helper image for file operations                                                                    0.0s
 => [internal] load build context                                                                                         0.0s
 => => transferring context: 11.46kB                                                                                      0.0s
 => CACHED [2/12] RUN mkdir /tmp/app                                                                                      0.0s
 => CACHED [3/12] COPY DummyVersion.toml ./Cargo.toml                                                                     0.0s
 => CACHED [4/12] COPY DummyVersion.lock ./Cargo.lock                                                                     0.0s
 => CACHED [5/12] RUN mkdir -p src/ &&     touch src/lib.rs                                                               0.0s
 => CACHED [6/12] RUN sudo chown -R rust:rust .                                                                           0.0s
 => CACHED [7/12] RUN cargo build --release                                                                               0.0s
 => CACHED [8/12] COPY ./src/ ./src/                                                                                      0.0s
 => [9/12] COPY Cargo.toml ./Cargo.toml                                                                                   0.5s
 => [10/12] COPY Cargo.lock ./Cargo.lock                                                                                  0.9s
 => [11/12] RUN sudo chown -R rust:rust .                                                                                 1.7s
 => [12/12] RUN cargo build --release                                                                                     6.0s
 => exporting to image                                                                                                    1.6s
 => => exporting layers                                                                                                   1.5s
 => => writing image sha256:7904fe7a43ea67e850591d7b6c0b827a97be625b0be447fa028d0e011a2a3cb8                              0.0s
 => => naming to docker.io/nnao45/dummy                                                                                   0.0

缓存它!!

依赖项

~3-4MB
~78K SLoC