Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fornwall/rust-static-builder
Docker image to build statically linked Linux executables from Rust projects.
https://github.com/fornwall/rust-static-builder
docker musl rust statically-linking
Last synced: 2 months ago
JSON representation
Docker image to build statically linked Linux executables from Rust projects.
- Host: GitHub
- URL: https://github.com/fornwall/rust-static-builder
- Owner: fornwall
- License: mit
- Created: 2018-12-01T14:23:43.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-04T10:29:26.000Z (4 months ago)
- Last Synced: 2024-10-14T14:57:06.326Z (3 months ago)
- Topics: docker, musl, rust, statically-linking
- Language: Dockerfile
- Homepage:
- Size: 424 KB
- Stars: 46
- Watchers: 8
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[![Build status](https://github.com/fornwall/rust-static-builder/workflows/Build/badge.svg)](https://github.com/fornwall/rust-static-builder/actions?query=branch%3Amaster)
[![Docker Hub](https://img.shields.io/docker/v/fredrikfornwall/rust-static-builder.svg?label=docker)](https://hub.docker.com/r/fredrikfornwall/rust-static-builder)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)# Rust static binary builder
Docker image for building statically linked x86_64 Linux binaries from Rust projects.## Building
From inside your project directoring containing a `Cargo.toml` file:```sh
# Stable release channel:
docker run -v "$PWD":/build fredrikfornwall/rust-static-builder:1.80.1# Nightly release channel:
docker run -v "$PWD":/build fredrikfornwall/rust-static-builder-nightly:2024-09-04
```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:```sh
docker run \
-v "$PWD":/build \
-v $HOME/.cargo/git:/root/.cargo/git \
-v $HOME/.cargo/registry:/root/.cargo/registry \
fredrikfornwall/rust-static-builder:1.80.1
```## Testing
Override the entry point to run tests against the statically linked binary:```sh
docker run \
-v "$PWD":/build \
-v $HOME/.cargo/git:/root/.cargo/git \
-v $HOME/.cargo/registry:/root/.cargo/registry \
--entrypoint cargo \
fredrikfornwall/rust-static-builder:1.80.1 \
test --target x86_64-unknown-linux-musl
```## Disable stripping
By default the built binary will be stripped. Run with `-e NOSTRIP=1`, as in```sh
docker run \
-e NOSTRIP=1 \
-v "$PWD":/build \
fredrikfornwall/rust-static-builder:1.80.1
```to disable stripping.
## Creating a lightweight Docker image
The built binary can be used to create a lightweight Docker image built from scratch:```dockerfile
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:- [bzip2](https://www.sourceware.org/bzip2/)
- [liblzma](https://tukaani.org/xz/)
- [openssl](https://www.openssl.org/)
- [sqlite](https://www.sqlite.org/)
- [zlib](https://zlib.net/)Note that if the projects needs certificates for OpenSSL a [base image containing /cacert.pem](scratch-with-certificates/Dockerfile) can be used when building a Docker image:
```dockerfile
FROM fredrikfornwall/scratch-with-certificates
COPY target/x86_64-unknown-linux-musl/release/tls-using-executable /
ENTRYPOINT ["/tls-using-executable"]
```