Skip to content

Latest commit

 

History

History
78 lines (60 loc) · 2.44 KB

README.md

File metadata and controls

78 lines (60 loc) · 2.44 KB

Docker Hub License: MIT

Rust static binary builder

Docker image for building statically linked Linux binaries from Rust projects.

Building

From inside your project directoring containing a Cargo.toml file:

# Stable release channel:
docker run -v "$PWD":/build dimlev/rust-static-builder:1.75.0

A statically linked binary will be created under target/x86_64-unknown-linux-musl/release/.

Speeding up builds by sharing registry and git folders

To speed up builds the cargo registry and git folders can be mounted:

docker run \
       -v "$PWD":/build \
       -v $HOME/.cargo/git:/root/.cargo/git \
       -v $HOME/.cargo/registry:/root/.cargo/registry \
       dimlev/rust-static-builder:1.75.0

Testing

Override the entry point to run tests against the statically linked binary:

docker run \
       -v "$PWD":/build \
       -v $HOME/.cargo/git:/root/.cargo/git \
       -v $HOME/.cargo/registry:/root/.cargo/registry \
       --entrypoint cargo \
       dimlev/rust-static-builder:1.75.0 \
       test --target x86_64-unknown-linux-musl

Disable stripping

By default the built binary will be stripped. Run with -e NOSTRIP=1, as in

docker run \
       -e NOSTRIP=1 \
       -v "$PWD":/build \
       dimlev/rust-static-builder:1.75.0

to disable stripping.

Creating a lightweight Docker image

The built binary can be used to create a lightweight Docker image built from scratch:

FROM scratch
COPY target/x86_64-unknown-linux-musl/release/my-executable /
ENTRYPOINT ["/my-executable"]

Native libraries and OpenSSL

The rust-static-builder image contains statically libraries for the following images in order for crates to be able to link them in:

Note that if the projects needs certificates for OpenSSL a base image containing /cacert.pem can be used when building a Docker image:

FROM dimlev/scratch-with-certificates
COPY target/x86_64-unknown-linux-musl/release/tls-using-executable /
ENTRYPOINT ["/tls-using-executable"]