A compilation of concepts I want to remember...

Navigation
 » Home
 » About Me
 » Github

Rust-Docker: Speeding up compile times when using docker + rust

22 Jun 2017 » rust, docker

Recently, have been trying to have a go at ROS using the rust language. As launch files aren’t available, a clear alternative is using docker containers with docker compose.

In order to accomplish the above, I first need to be able to create containers that can compile the rust language. Do a bit of searching on the internet, I came across Docker container for easily building static Rust binaries, which seemed perfect for this particular exercise.

Simply running allows for the compilation of a static Rust binary with no external dependencies:

$ alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
$ rust-musl-builder cargo build --release

One suggestion that was made to me to improve the build times, was to cache the dependencies necessary by creating a .cargo/registry in the rust project.

Going to the root of the rust project directory:

$ mkdir .cargo/registry -p

and add the mounting command as follows when creating the alias:

$ alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src -v "$(pwd)/.cargo/registry":/home/rust/.cargo/registrye kidd/rust-musl-builder'
$ rust-musl-builder cargo build --release

The first build will take the usual expected time, while the next builds will be faster with any dependencies having been cached.

Thanks @yasuyuky!