You can wrap up the build and the copy with a multi-stage docker build. Something like the following.
FROM rust:latest
RUN rustup target install x86_64-unknown-linux-musl
COPY . /src
WORKDIR /src
RUN cargo build --release --target=x86_64-unknown-linux-musl
FROM scratch
COPY --from=0 /src/target/x86_64-unknown-linux-musl/release/rust_docker_barebones /rust_docker_barebones
ENTRYPOINT ["/rust_docker_barebones"]
Although, I typically recommend _not_ using the scratch container unless you really need something that tiny. It makes debugging very difficult if you don't at a minimum have a shell and a package manager.
Also, packaging up rust to be static like this can take some extra steps. For example, loading in root certificates.
Also, packaging up rust to be static like this can take some extra steps. For example, loading in root certificates.