├── .gitignore ├── src └── main.rs ├── Cargo.toml ├── Dockerfile ├── README.md └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use chrono::prelude::*; 2 | 3 | fn main() { 4 | let dt = Utc.ymd(2014, 7, 8).and_hms(9, 10, 11); 5 | 6 | println!("Hello, world! It is: {}", dt); 7 | } 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "test123" 3 | version = "0.1.0" 4 | authors = ["ben"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | chrono = "0.4.11" 11 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # syntax=docker/dockerfile:experimental 2 | from rust 3 | 4 | RUN cargo install sccache 5 | 6 | ENV HOME=/home/root 7 | ENV SCCACHE_CACHE_SIZE="1G" 8 | ENV SCCACHE_DIR=$HOME/.cache/sccache 9 | ENV RUSTC_WRAPPER="/usr/local/cargo/bin/sccache" 10 | 11 | WORKDIR $HOME/app 12 | 13 | ADD src src 14 | ADD Cargo.lock . 15 | ADD Cargo.toml . 16 | 17 | # Change this line to force docker recompilation from this step on. 18 | # This will hit sccache the second time. 19 | RUN echo 1 20 | 21 | RUN --mount=type=cache,target=/home/root/.cache/sccache cargo build --release && sccache --show-stats 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker+Rust Buildcache 2 | 3 | Describing two strategies to cache rust builds within docker in the minimal example repo. 4 | 5 | ## Caching Cargo and Target Folders 6 | To achieve local like build caching you simply need mount cargos `registry` and your build `target` folder as a docker cache mount. 7 | 8 | A minimal example can be found in this branch: https://github.com/benmarten/sccache-docker-test/tree/no-sccache 9 | 10 | ## Minimal rust+sccache compilation example with docker cache volume. 11 | If you want your cache to outlive the `target` folder or you want to share the buildcache between multiple clients/nodes, you can use sccache to achieve that. Check out the example in this master branch: 12 | 13 | To see sccache in action within the docker container, run: 14 | 15 | `DOCKER_BUILDKIT=1 docker build . --progress=plain` 16 | 17 | then change the `echo 1` line to `echo 2` (to force recompilation from this line), and rerun the docker command, you'll see cache hits in sccache's stats ;) 18 | 19 | To clear the docker cache mount: 20 | `docker builder prune --filter type=exec.cachemount` 21 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "autocfg" 5 | version = "1.0.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" 8 | 9 | [[package]] 10 | name = "chrono" 11 | version = "0.4.11" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" 14 | dependencies = [ 15 | "num-integer", 16 | "num-traits", 17 | "time", 18 | ] 19 | 20 | [[package]] 21 | name = "libc" 22 | version = "0.2.67" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "eb147597cdf94ed43ab7a9038716637d2d1bf2bc571da995d0028dec06bd3018" 25 | 26 | [[package]] 27 | name = "num-integer" 28 | version = "0.1.42" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" 31 | dependencies = [ 32 | "autocfg", 33 | "num-traits", 34 | ] 35 | 36 | [[package]] 37 | name = "num-traits" 38 | version = "0.2.11" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" 41 | dependencies = [ 42 | "autocfg", 43 | ] 44 | 45 | [[package]] 46 | name = "redox_syscall" 47 | version = "0.1.56" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" 50 | 51 | [[package]] 52 | name = "test123" 53 | version = "0.1.0" 54 | dependencies = [ 55 | "chrono", 56 | ] 57 | 58 | [[package]] 59 | name = "time" 60 | version = "0.1.42" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" 63 | dependencies = [ 64 | "libc", 65 | "redox_syscall", 66 | "winapi", 67 | ] 68 | 69 | [[package]] 70 | name = "winapi" 71 | version = "0.3.8" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" 74 | dependencies = [ 75 | "winapi-i686-pc-windows-gnu", 76 | "winapi-x86_64-pc-windows-gnu", 77 | ] 78 | 79 | [[package]] 80 | name = "winapi-i686-pc-windows-gnu" 81 | version = "0.4.0" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 84 | 85 | [[package]] 86 | name = "winapi-x86_64-pc-windows-gnu" 87 | version = "0.4.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 90 | --------------------------------------------------------------------------------