Rust in Docker
| The rust image | |
| Run one-liners | |
| Mount your crate | |
| Cache the registry |
The rust image
The official rust image ships the compiler, cargo, and rustup in a ready to use Linux environment.
Use the rust:1 tag for the latest Rust 1 series so builds stay reproducible while still receiving compiler updates. That tag is a good default for learning and for small automation tasks.
No host install is needed because the container carries the complete toolchain.
Run one-liners
Start with a version check that downloads the image on first use and then prints the result.
The remove flag cleans up the container automatically after the command finishes.
docker run --rm rust:1 rustc --version
rustc 1.75.0 (ad2397f6f 2023-12-28) (x86_64-unknown-linux-gnu)
Swap the final words to check cargo with the same one-liner pattern.
Mount your crate
Mount the current project directory into the container so cargo can build local code.
The command below maps the host directory to the app work directory and runs the project from there.
docker run --rm -v "$PWD":/app -w /app rust:1 cargo run
Hello from Docker
Edit files on the host with any editor and rerun the same command to rebuild inside the container.
Cache the registry
Plain one-liners download dependencies on every run, so cache the cargo registry for faster repeat builds.
Use a named volume for the cargo home directory as plain text in your run command. The cached crates survive container removal and make the next build start almost instantly.
Next: Rust course
Article author: Arthur Isaev