I'm building a Rust program in Docker (rust:1.33.0
).
Every time code changes, it re-compiles (good), which also re-downloads all dependencies (bad).
I thought I could cache dependencies by adding VOLUME ["/usr/local/cargo"]
. edit I've also tried moving this dir with CARGO_HOME
without luck.
I thought that making this a volume would persist the downloaded dependencies, which appear to be in this directory.
But it didn't work, they are still downloaded every time. Why?
Dockerfile
FROM rust:1.33.0VOLUME ["/output", "/usr/local/cargo"]RUN rustup default nightly-2019-01-29COPY Cargo.toml .COPY src/ ./src/RUN ["cargo", "build", "-Z", "unstable-options", "--out-dir", "/output"]
Built with just docker build .
.
Cargo.toml
[package]name = "mwe"version = "0.1.0"[dependencies]log = { version = "0.4.6" }
Code: just hello world
Output of second run after changing main.rs
:
...Step 4/6 : COPY Cargo.toml .---> Using cache---> 97f180cb6ce2Step 5/6 : COPY src/ ./src/---> 835be1ea0541Step 6/6 : RUN ["cargo", "build", "-Z", "unstable-options", "--out-dir", "/output"]---> Running in 551299a42907Updating crates.io indexDownloading crates ...Downloaded log v0.4.6Downloaded cfg-if v0.1.6Compiling cfg-if v0.1.6Compiling log v0.4.6Compiling mwe v0.1.0 (/)Finished dev [unoptimized + debuginfo] target(s) in 17.43sRemoving intermediate container 551299a42907---> e4626da13204Successfully built e4626da13204