Copy Rust binaries for Docker images

When building a Docker image, Docker supports copying files from other Docker images. I'd like to leverage this feature of Docker (to avoid repeating Rust installation in different Docker images). I wonder

  1. Is this a good idea?
  2. What Rust binaries (and other files) should I copy if I follow this approach?

Yes, it's a good idea. Often it is enough to copy target/release/thebinary from the "building container" to the "runtime container", as described here.

If your rust program uses specific external dependencies (c libraries, files available runtime, etc), those must also be included in the runtime container, but that will be specific to your application.

How about Rust itself? Say that I've already installed Rust (using rustup or other ways) in a Docker image. Can I copy Rust binaries (rustc, rustup, cargo, etc.) to another Docker image?

You need to copy the entire rust installation. If you are using rustup it is one of the directories in ~/.rustup/toolchain depending on which rust version you want to use. The installation directory contains all binaries and libraries directly required by rustc as well as the compiled standard library to compile against.

You can use the minimal rustup profile if you want to save space and don't need tools like clippy or the documentation though.

How can install Rust with minimal profile? Is there an option to the rustup installation script?

Why do you want to copy the compiler? You can build within the container where the toolchain is installed and copy your binary into the image that will be distributed. You don't need the compiler to run the binary produced.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.