emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.
Apache License 2.0
1.54k stars 193 forks source link

Caching builds in a multi-stage Docker build #119

Closed last-genius closed 3 years ago

last-genius commented 3 years ago

README's section on caching builds suggests a few solutions to cache builds when you use rust-musl-builder through docker run. I've been trying to integrate this into my my multi-stage Dockerfile but I can't get it to work.

My Dockerfile looks roughly like this:

# Build stage
FROM ekidd/rust-musl-builder:latest AS builder

ADD --chown=rust:rust . ./
VOLUME cargo-git /home/rust/.cargo/git
VOLUME cargo-registry /home/rust/.cargo/registry
VOLUME target /home/rust/src/target 
CMD "sudo chown -R rust:rust /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src/target"

RUN cargo build --release

This does not work, and Docker throws this error on the last stage:

Step 7/13 : RUN cargo build --release
 ---> Running in 7a879da880ce
error: failed to get `chrono` as a dependency of package `rust-crawler v0.1.0 (/home/rust/src)`

Caused by:
  failed to create directory `/home/rust/.cargo/registry/index/github.com-1ecc6299db9ec823`

Caused by:
  Permission denied (os error 13)

What am I doing wrong here?