Git Product home page Git Product logo

rust-static-builder's Introduction

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 fredrikfornwall/rust-static-builder

# Nightly release channel:
docker run -v "$PWD":/build fredrikfornwall/rust-static-builder-nightly 

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 \
       fredrikfornwall/rust-static-builder

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 \
       fredrikfornwall/rust-static-builder \
       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 \
       fredrikfornwall/rust-static-builder

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:

  • liblzma
  • openssl
  • sqlite
  • zlib

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

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

rust-static-builder's People

Contributors

fornwall avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.