Rust in Docker

Contents
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

Related articles
Rust
Rust course: zero to hero in 50 lessons
What is Rust and where it runs
Install Rust
cargo new
Your first program
Variables
Numbers
Strings
Booleans
Tuples
Arrays
Vectors
Slices
if and else
match
Loops
Ownership
Borrowing
Lifetimes
Functions
Structs
Enums in Rust
Advanced match in Rust
Option combinators in Rust
Result and the question mark in Rust
Advanced strings in Rust
Vec methods in Rust
HashMaps in Rust
Modules in Rust
Crates and workspaces in Rust
Testing in Rust
Error handling
Traits
Generics
Trait bounds
Closures
Iterators
Smart pointers
Threads
Mutex and Arc
Files
CLI arguments
Environment variables
Diagnostics
Time
Parsing strings
TCP networking
Capstone: wordfreq CLI
Capstone: tiny HTTP server
Modern Rust tour
Hero roadmap: the whole course on one page
Install Rust on Windows 11
Install Rust on Ubuntu
Install Rust on Rocky Linux
Install Rust on macOS
Install Rust on FreeBSD
Rust in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: