Go to file
Adrian Calinescu 7373eb4ed5
Merge pull request #1 from snobu/patch-1
Added ARMhf build info to README
2018-03-25 17:43:08 +03:00
examples Add sqlite support for diesel applications (fixes #28) 2017-12-16 16:15:02 -05:00
hooks Use bash for hook script 2016-11-25 07:49:16 -05:00
.gitignore Add working example and test case for diesel 2017-10-24 09:05:22 -04:00
cargo-config.toml Fix ARM linker ref 2018-03-25 16:48:44 +03:00
Dockerfile Added GCC ARM 2018-03-25 16:01:36 +03:00
git-credential-ghtoken Add support for accessing private GitHub repositories 2018-02-28 06:59:21 -05:00
LICENSE-APACHE.txt Add licenses 2016-04-17 07:45:08 -04:00
LICENSE-MIT.txt Add licenses 2016-04-17 07:45:08 -04:00
README.md Added ARMhf build info to README 2018-03-25 17:39:23 +03:00
sudoers Overhaul extensively and add test script 2016-04-17 07:42:09 -04:00
test-image Add working example and test case for diesel 2017-10-24 09:05:22 -04:00

rust-musl-builder: Docker container for easily building static Rust binaries

Docker Image

Do you want to compile a completely static Rust binary with no external dependencies? If so, try:

alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
rust-musl-builder cargo build --release

To target ARM hard float (Raspberry Pi):

rust-musl-builder cargo build --target=armv7-unknown-linux-gnueabihf --release

This command assumes that $(pwd) is readable and writable by uid 1000, gid 1000. It will output binaries in target/x86_64-unknown-linux-musl/release. At the moment, it doesn't attempt to cache libraries between builds, so this is best reserved for making final release builds.

Deploying your Rust application

With a bit of luck, you should be able to just copy your application binary from target/x86_64-unknown-linux-musl/release, and install it directly on any reasonably modern x86_64 Linux machine. In particular, you should be able make static release binaries using TravisCI and GitHub, or you can copy your Rust application into an Alpine Linux container. See below for details!

How it works

rust-musl-builder uses musl-libc, musl-gcc, and the new rustup target support. It includes static versions of several libraries:

  • The standard musl-libc libraries.
  • OpenSSL, which is needed by many Rust applications.
  • libpq, which is needed for applications that use diesel with PostgreSQL. Note that this may be broken under Rust 1.21.0 and later (see https://github.com/emk/rust-musl-builder/issues/27).
  • libz, which is needed by libpq.

You can also use the following libraries with a bit of setup:

This library also sets up the environment variables needed to compile popular Rust crates using these libraries.

Making OpenSSL work

If your application uses OpenSSL, you will also need to take a few extra steps to make sure that it can find OpenSSL's list of trusted certificates, which is stored in different locations on different Linux distributions. You can do this using openssl-probe as follows:

extern crate openssl_probe;

fn main() {
    openssl_probe::init_ssl_cert_env_vars();
    //... your code
}

Making Diesel work

In addition to setting up OpenSSL, you'll need to add the following lines to your Cargo.toml:

[dependencies]
# This is needed to make sure that Cargo statically links against
# `libssl`. This should happen automatically, but it doesn't.
openssl-sys = "0.9"

[patch.crates-io]
# This is needed to handle cross-compilation of libpq.
pq-sys = { git = 'https://github.com/golddranks/pq-sys' }

See this PR for a discussion of the issues involved in cross-compiling diesel and diesel_codegen.

Making static releases with Travis CI and GitHub

These instructions are inspired by rust-cross.

First, read the Travis CI: GitHub Releases Uploading page, and run travis setup releases as instructed. Then add the following lines to your existing .travis.yml file, replacing myapp with the name of your package:

language: rust
sudo: required
os:
- linux
- osx
rust:
- stable
services:
- docker
before_deploy: "./build-release myapp ${TRAVIS_TAG}-${TRAVIS_OS_NAME}"
deploy:
  provider: releases
  api_key:
    secure: "..."
  file_glob: true
  file: "myapp-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.*"
  skip_cleanup: true
  on:
    rust: stable
    tags: true

Next, copy build-release into your project and run chmod +x build-release.

Finally, add a Dockerfile to perform the actual build:

FROM ekidd/rust-musl-builder

# We need to add the source code to the image because `rust-musl-builder`
# assumes a UID of 1000, but TravisCI has switched to 2000.
ADD . ./
RUN sudo chown -R rust:rust .

CMD cargo build --release

When you push a new tag to your project, build-release will automatically build new Linux binaries using rust-musl-builder, and new Mac binaries with Cargo, and it will upload both to the GitHub releases page for your repository.

For a working example, see faradayio/cage.

Making tiny Docker images with Alpine Linux and Rust binaries

Docker now supports multistage builds, which make it easy to build your Rust application with rust-musl-builder and deploy it using Alpine Linux. For a working example, see examples/using-diesel/Dockerfile.

Adding more C libraries

If you're using Docker crates which require specific C libraries to be installed, you can create a Dockerfile based on this one, and use musl-gcc to compile the libraries you need. For an example, see examples/adding-a-library/Dockerfile. This usually involves a bit of experimentation for each new library, but it seems to work well for most simple, standalone libraries.

If you need an especially common library, please feel free to submit a pull request adding it to the main Dockerfile! We'd like to support popular Rust crates out of the box.

Development notes

After modifying the image, run ./test-image to make sure that everything works.xs

Other ways to build portable Rust binaries

If for some reason this image doesn't meet your needs, there's a variety of other people working on similar projects:

License

Either the Apache 2.0 license, or the MIT license.