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

Use a non-ephemeral cargo cache #5

Closed luser closed 6 years ago

luser commented 8 years ago

It would be nice if the cargo cache for this image wasn't ephemeral so that every rebuild didn't wind up re-downloading all the crates it needs. I can think of two ways do to this: 1) Use VOLUME ~rust/.cargo to make the .cargo directory a persistent data volume: https://docs.docker.com/engine/reference/builder/#/volume (This must be done after all the steps in the Dockerfile that will write data to that directory.) 2) Change the alias to mount the host ~/.cargo as ~rust/.cargo, to share the cargo cache from the host system.

emk commented 8 years ago

Hmm. Couldn't you do (2) manually when you invoke the container? I'm reluctant to hard-code a VOLUME into the container, because that can have some surprising consequences for people who don't know about it, or who want clean production builds.

luser commented 8 years ago

I can, yeah. It would be nice to at least provide this as an alternative in the documentation for people who want it. I understand wanting to have defaults that work well for most people, but this is pretty painful and it breaks a built-in cargo feature (caching) that people expect to have.

Mange commented 7 years ago

For anyone else wondering how to do this correctly, here's an example:

mkdir -p "${project_root}/target/musl-builder/cargo-cache"
docker run --rm -it \
  -v "${project_root}":/home/rust/src \
  -v "${project_root}/target/musl-builder/cargo-cache":/home/rust/.cargo/registry \
  ekidd/rust-musl-builder cargo build --release

The registry cache will not lie in the made-up target/musl-builder directory, while the image will still use the same cargo version that is installed in the image.

I hope this helps someone.

emk commented 7 years ago

Thank you, that's a nice solution! I would be happy to include a version of these instructions in the README.md file if anybody wants to submit a PR.

emk commented 6 years ago

I'm going to close this issue for now, but please feel free to submit a PR with instructions.