Docker images for compiling static Rust binaries using musl-cross

Overview

rust-musl-cross

Docker images for compiling static Rust binaries using musl-cross-make, inspired by rust-musl-builder

Docker Image Build

Prebuilt images

Currently we have the following prebuilt Docker images on Docker Hub.

Rust toolchain Cross Compile Target Docker Image Tag
stable aarch64-unknown-linux-musl aarch64-musl
stable arm-unknown-linux-musleabi arm-musleabi
stable arm-unknown-linux-musleabihf arm-musleabihf
stable armv5te-unknown-linux-musleabi armv5te-musleabi
stable armv7-unknown-linux-musleabi armv7-musleabi
stable armv7-unknown-linux-musleabihf armv7-musleabihf
stable i586-unknown-linux-musl i586-musl
stable i686-unknown-linux-musl i686-musl
stable mips-unknown-linux-musl mips-musl
stable mipsel-unknown-linux-musl mipsel-musl
stable x86_64-unknown-linux-musl x86_64-musl

To use armv7-unknown-linux-musleabihf target for example, first pull the image:

docker pull messense/rust-musl-cross:armv7-musleabihf

Then you can do:

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

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

How it works

rust-musl-cross uses musl-libc, musl-gcc with the help of musl-cross-make to make it easy to compile, 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.

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
}

Use beta/nightly Rust

Currently we install stable Rust by default, if you want to switch to beta/nightly Rust, you can do it by extending from our Docker image, for example to use beta Rust for target x86_64-unknown-linux-musl:

FROM messense/rust-musl-cross:x86_64-musl
RUN rustup update beta && \
    rustup target add --toolchain beta x86_64-unknown-linux-musl

Strip binaries

You can use the musl-strip command inside the image to strip binaries, for example:

docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:armv7-musleabihf musl-strip /home/rust/src/target/release/example

License

Licensed under The MIT License

Comments
  • Fix C compiler environment variables

    Fix C compiler environment variables

    Setting CC breaks build dependencies when cross compiling. cc-rs also supports reading TARGET_ variables, and falls back to the default compiler (and include paths) when compiling for the host.

    Fixes #10

    opened by jan-auer 12
  • Cross-compilation libudev-sys

    Cross-compilation libudev-sys

    Hi, I have a problem with cross-compile example tokio-serial. I described the problem on tokio-serial issue - but I think here should be more people that can manage this problem.

    question 
    opened by xoac 5
  • `messense/rust-musl-cross:x86_64-musl` is not `x86_64-unknown-linux-musl` target.

    `messense/rust-musl-cross:x86_64-musl` is not `x86_64-unknown-linux-musl` target.

    Thanks for your work on this. A very usefull docker image to build musl target.

    But recently, I find that messense/rust-musl-cross:x86_64-musl is not x86_64-unknown-linux-musl target. It shows x86_64-unknown-linux-gun in my wsl2 kali roling linux.

    opened by mokeyish 4
  • Request To Add Prebuilt Docker Image For arm32v7 Architecture

    Request To Add Prebuilt Docker Image For arm32v7 Architecture

    @messense is there any plan to add prebuild image of arm32v7 rust-musl-cross in the future?

    I came across your docker image while trying to cross compile a rust source to target armv6, it got all the binaries I need. However due to my project requirements and some constraint on my work PC, I can only run my build on a Raspberry Pi 4 with 32bit OS. The prebuilt images available are only for arm64 and amd64 according to the repo on docker hub.

    Currently I'm working around this limitation by using official rust arm32v7 image and run make install for musl-cross-make on the image. It would be great if you can add arm32v7 prebuilt image as it will result in lesser complexity of my Dockerfile for my project.

    Thank you in advance.

    opened by aggo15 4
  • Strip

    Strip

    Hi, I was wondering how I could cross strip my final application just using this docker image? Anybody know about existing cross striping? or maybe some [profile.release] configuration (can rustc strip by itself?)

    question 
    opened by xoac 4
  • aarch64-musl crashing when building app

    aarch64-musl crashing when building app

    Hi,

    I try to build a rust app with musl, by rust-mul-cross images and GHA.

    x86_64 works fine, but aarch64 crash.

    I get:

    #14 5.127     Updating crates.io index
    #14 85.51 Killed
    ERROR: failed to solve: process "/bin/sh -c cargo build --release" did not complete successfully: exit code: 137
    Error: buildx failed with: ERROR: failed to solve: process "/bin/sh -c cargo build --release" did not complete successfully: exit code: 137
    

    GHA Workflow: https://github.com/Ludea/speedupdate-rs/blob/master/.github/workflows/docker.yml Dockerfile: https://github.com/Ludea/speedupdate-rs/blob/master/Dockerfile Full log: https://github.com/Ludea/speedupdate-rs/actions/runs/3409830963/jobs/5672075085

    I don't understand what is wrong

    opened by Ludea 3
  • GH Actions notifications on cloned repo

    GH Actions notifications on cloned repo

    I just updated the master branch on my repo, and now I have failed Action Notifications in my mail box

    How can I stop that without making a commit to delete it (which will be questioned next time I update the branch) (EDIT: and makes making PR's a bit problematic).

    NOTE: I was not getting any Actions or Notifications before that (then 53 commits behind I think )

    opened by paulwratt 3
  • Can't build with x86_64-musl around pkg-config

    Can't build with x86_64-musl around pkg-config

    I want to build the plotters example in x86_64-musl.
    I did these commands.

    git clone https://github.com/plotters-rs/plotters.git --recursive
    cd plotters
    docker run --rm -it -v `pwd`:/home/rust/src messense/rust-musl-cross:x86_64-musl
    # in the container.
    cd plotters
    RUST_BACKTRACE=1 cargo run --example histogram
    

    And I got errors.

    error: failed to run custom build command for `yeslogic-fontconfig-sys v3.2.0`
    
    Caused by:
      process didn't exit successfully: `/home/rust/src/target/debug/build/yeslogic-fontconfig-sys-cac8f25c1297fba5/build-script-build` (exit status: 101)
      --- stdout
      cargo:rerun-if-env-changed=RUST_FONTCONFIG_DLOPEN
      cargo:rerun-if-env-changed=FONTCONFIG_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
    
      --- stderr
      thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/yeslogic-fontconfig-sys-3.2.0/build.rs:8:48
      stack backtrace:
         0: rust_begin_unwind
                   at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:584:5
         1: core::panicking::panic_fmt
                   at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/panicking.rs:142:14
         2: core::result::unwrap_failed
                   at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1785:5
         3: core::result::Result<T,E>::unwrap
                   at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1078:23
         4: build_script_build::main
                   at ./build.rs:8:9
         5: core::ops::function::FnOnce::call_once
                   at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/ops/function.rs:248:5
      note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
    warning: build failed, waiting for other jobs to finish...
    

    It seems to be a problem with the configuration around pkg-config.
    Sorry if this is not appropriate as an issue.
    Is this a problem with messense/rust-musl-cross:x86_64-musl ?
    Or should I create a properly configured Docker container based on messense/rust-musl-cross:x86_64-musl ?

    opened by HiraokaTakuya 3
  • Build rocksdb with error

    Build rocksdb with error

    Downloaded 134 crates (15.9 MB) in 4.37s (largest was `librocksdb-sys` at 6.1 MB)
       Compiling librocksdb-sys v0.6.1+6.28.2
    error: failed to run custom build command for `librocksdb-sys v0.6.1+6.28.2`
    
    Caused by:
      process didn't exit successfully: `/home/rust/src/target/release/build/librocksdb-sys-575f1be645469399/build-script-build` (exit status: 101)
      --- stderr
      /usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
      /usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found, err: true
      thread 'main' panicked at 'unable to generate rocksdb bindings: ()', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/librocksdb-sys-0.6.1+6.28.2/build.rs:43:10
      note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    
    opened by ipconfiger 3
  • Compilation failed: thread 'rustc' panicked at 'Failed to get crate data for crate179'

    Compilation failed: thread 'rustc' panicked at 'Failed to get crate data for crate179'

    Platform

    Linux 65fe86937035 5.4.0-73-generic #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux`

    Description

    I'm trying to cross-compile a Rust application that depends on ethers-rs with the help of rust-musl-cross. However, compilation fails and after some debugging, I found ethers-rs to be the one causing the error.

    I'm not opening this issue on ethers-rs as that library can be compiled outside rust-musl-cross without issues.

    Steps to Reproduce

    Create a Dockerfile anywhere with the following content:

    FROM messense/rust-musl-cross:x86_64-musl
    
    RUN git clone https://github.com/gakonst/ethers-rs && \
        cd ethers-rs && \
        cargo build
    

    Run:

    $ docker build .
    

    Expected Result

    Compilation succeeds.

    Actual Result

    Compilation fails with:

       Compiling ethers-contract-abigen v0.2.2 (/home/rust/src/ethers-rs/ethers-contract/ethers-contract-abigen)
       Compiling ethers-providers v0.2.2 (/home/rust/src/ethers-rs/ethers-providers)
       Compiling ethers-contract-derive v0.2.2 (/home/rust/src/ethers-rs/ethers-contract/ethers-contract-derive)
       Compiling ethers-contract v0.2.2 (/home/rust/src/ethers-rs/ethers-contract)
    thread 'rustc' panicked at 'Failed to get crate data for crate179', compiler/rustc_metadata/src/creader.rs:136:32
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    
    error: internal compiler error: unexpected panic
    
    note: the compiler unexpectedly panicked. this is a bug.
    
    note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
    
    note: rustc 1.52.1 (9bc8c42bb 2021-05-09) running on x86_64-unknown-linux-gnu
    
    note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C linker=x86_64-unknown-linux-musl-gcc -C incremental --crate-type lib
    
    note: some of the compiler flags provided by cargo are hidden
    
    query stack during panic:
    end of query stack
    error: could not compile `ethers-contract`
    
    To learn more, run the command again with --verbose.
    
    opened by xJonathanLEI 3
  • pkg-config has not been configured for cross-compilation (external sys dependencies)

    pkg-config has not been configured for cross-compilation (external sys dependencies)

    Hi, I'm sorry if this is not the right place to ask, but my application depends on libpulse-sys and causes the following error when I try to build it in the container. I understand that getting host system's libraries to link is not trivial, but I wonder if you can just compile and provide a cross-compiling pkg-config?

    The following warnings were emitted during compilation:
    
    warning: pkg-config has not been configured to support cross-compilation.
    
    error: failed to run custom build command for `libpulse-sys v1.18.0`
    
    Caused by:
      process didn't exit successfully: `/home/rust/src/target/release/build/libpulse-sys-0b539a63b06ecfcd/build-script-build` (exit code: 1)
      --- stdout
      cargo:rerun-if-env-changed=LIBPULSE_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=LIBPULSE_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
      cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:warning=pkg-config has not been configured to support cross-compilation.
    
                      Install a sysroot for the target platform and configure it via
                      PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
                      cross-compiling wrapper for pkg-config and set it via
                      PKG_CONFIG environment variable.
    warning: build failed, waiting for other jobs to finish...
    error: build failed
    
    opened by gustafla 3
  • RISCV 64 support

    RISCV 64 support

    Hey guys, it would be awesome to have RISCV support. I have already seen that there is a feature branch for it, but no PR or ticket yet. So I just wanted to ask here about the status.

    opened by RouHim 2
  • Docker file arm fpu setting

    Docker file arm fpu setting

    in the following line what does -d16 refer to, or enable:

    ENV CFLAGS_armv7_unknown_linux_musleabihf='-mfpu=vfpv3-d16'
    

    As far as I understand this is device specific, that is -mfpu=vfpv3 will apply to more (Broadcom?) armv7 devices that have a vfpv3

    opened by paulwratt 8
  • s390x can't build openssl-src

    s390x can't build openssl-src

      = note: /usr/local/musl/bin/../lib/gcc/s390x-unknown-linux-musl/9.2.0/../../../../s390x-unknown-linux-musl/bin/ld: /home/rust/src/target/s390x-unknown-linux-musl/debug/deps/libopenssl_sys-70172c64409e3e90.rlib(curve448.o): in function `recode_wnaf':
              /home/rust/src/target/s390x-unknown-linux-musl/debug/build/openssl-sys-1de3ca3326f5bf22/out/openssl-build/build/src/crypto/ec/curve448/curve448.c:575: undefined reference to `__ctzdi2'
              /usr/local/musl/bin/../lib/gcc/s390x-unknown-linux-musl/9.2.0/../../../../s390x-unknown-linux-musl/bin/ld: read-only segment has dynamic relocations
              collect2: error: ld returned 1 exit status
    

    Hmm, not sure what's going on.

    Originally posted by @messense in https://github.com/messense/rust-musl-cross/issues/62#issuecomment-1250523140

    bug 
    opened by messense 3
Owner
messense
Python Backend Developer at day, Rustacean at night.
messense
docker-rust — the official Rust Docker image

About this Repo This is the Git repo of the Docker official image for rust. See the Docker Hub page for the full readme on how to use this Docker imag

The Rust Programming Language 321 Dec 11, 2022
Very small rust docker image

mini-docker-rust Very small rust docker image. This is an example project on how to build very small docker images for a rust project. The resulting i

null 155 Jan 1, 2023
Valheim Docker powered by Odin. The Valheim dedicated gameserver manager which is designed with resiliency in mind by providing automatic updates, world backup support, and a user friendly cli interface.

Valheim Docker If you are looking for a guide on how to get started click here Mod Support! It is supported to launch the server with BepInEx but!!!!!

Michael 657 Dec 30, 2022
Rocker is a minimal docker implementation for educational purposes.

Rocker is a minimal docker implementation for educational purposes inspired by gocker. Rocker uses linux kernel features (namespace, cgroup, chroot etc.) to isolate container processes and limit available resourses.

Daiki Miura 16 Feb 14, 2022
A lite tool to make systemd work in any container(Windows Subsystem for Linux 2, Docker, Podman, etc.)

Angea Naming from hydrangea(アジサイ) A lite tool to make systemd work in any container(Windows Subsystem for Linux 2, Docker, Podman, etc.) WSL1 is not s

いんしさくら 16 Dec 5, 2022
Tool to monitor the statistics and the energy consumption of docker containers

Docker Activity Docker activity is a tool to monitor the statistics of your containers and output their energy consumption. Warning It's still in earl

Jérémie Drouet 39 Dec 6, 2022
🐳 📦 Bringing docker containers to your AUR helper since 2022

zeus Releases | CI | Issues | Installing | Building Zeus. A simple AUR helper which utilizes docker containers allowing developers and users alike to

1337 16 Dec 17, 2022
Hot-plug devices into a Docker container as they are plugged.

container-hotplug Hot-plug (and unplug) devices into a Docker container as they are (un)plugged. Description Docker provides the --device flag to give

lowRISC 2 Oct 17, 2022
Automated builded images for rust-lang with rustup, "the ultimate way to install RUST"

rustup Automated builded images on store and hub for rust-lang with musl added, using rustup "the ultimate way to install RUST". tag changed: all3 ->

刘冲 83 Nov 30, 2022
Inspect and dump OCI images.

reinlinsen ?? rl is a tool to inspect and dump OCI images or single image layers. Installation From source If you have cargo installed you can just ru

Tobias Brumhard 5 May 11, 2023
A wasm template for Rust to publish to gh-pages without npm-deploy

Wasm template for Rust hosting without npm-deploy on github pages using Travis script It automatically hosts you wasm projects on gh-pages using a tra

Siddharth Naithani 102 Dec 24, 2022
App Engine Rust boilerplate

Rust App Engine This projects is a minimal boilerplate ro run Rust web application inside Google App Engine. To deploy it use Google Cloud Shell: ```s

Denis Kolodin 48 Apr 26, 2022
A buildpack for Rust applications on Heroku, with full support for Rustup, cargo and build caching.

Heroku buildpack for Rust This is a Heroku buildpack for Rust with support for cargo and rustup. Features include: Caching of builds between deploymen

Eric Kidd 502 Nov 7, 2022
A tiny minimal container runtime written in Rust.

vas-quod A tiny minimal container runtime written in Rust. The idea is to support a minimal isolated containers without using existing runtimes, vas-q

flouthoc 438 Dec 26, 2022
oci-image and oci-runtime spec in rust.

oci-lib Oci-Spec for your container runtime or container registry. Oci-lib is a rust port for original oci spec written in go. Following crate contain

flouthoc 12 Mar 10, 2022
Experimental implementation of the oci-runtime in Rust

youki Experimental implementation of the oci-runtime in Rust Overview youki is an implementation of runtime-spec in Rust, referring to runc. This proj

utam0k 12 Sep 23, 2022
Krustlet: Kubernetes Kubelet in Rust for running WASM

Krustlet: Kubernetes Kubelet in Rust for running WASM ?? ?? This project is highly experimental. ?? ?? It should not be used in production workloads.

null 103 Dec 29, 2022
youki is an implementation of the OCI runtime-spec in Rust, similar to runc.

youki is an implementation of the OCI runtime-spec in Rust, similar to runc.

Containers 4.2k Dec 29, 2022
Shallow Container is a light-weight container tool written in Rust.

Shallow Container is a light-weight container tool written in Rust. It is totally for proof-of-concept and may not suit for production environment.

Rui Li 14 Apr 8, 2022