{"id":13437731,"url":"https://github.com/emk/rust-musl-builder","last_synced_at":"2025-05-15T15:05:40.595Z","repository":{"id":7456102,"uuid":"56430295","full_name":"emk/rust-musl-builder","owner":"emk","description":"Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.","archived":false,"fork":false,"pushed_at":"2024-05-04T20:43:43.000Z","size":147,"stargazers_count":1555,"open_issues_count":17,"forks_count":192,"subscribers_count":21,"default_branch":"main","last_synced_at":"2025-04-08T09:03:57.891Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-17T10:13:45.000Z","updated_at":"2025-03-23T22:05:04.000Z","dependencies_parsed_at":"2024-05-04T21:32:27.200Z","dependency_job_id":"44b1e21f-260a-40b6-8905-0803cec1afa2","html_url":"https://github.com/emk/rust-musl-builder","commit_stats":{"total_commits":91,"total_committers":18,"mean_commits":5.055555555555555,"dds":"0.31868131868131866","last_synced_commit":"5911b882c547e114372d66cbb084916516321aa6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emk%2Frust-musl-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emk%2Frust-musl-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emk%2Frust-musl-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emk%2Frust-musl-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emk","download_url":"https://codeload.github.com/emk/rust-musl-builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254364270,"owners_count":22058878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:00:59.787Z","updated_at":"2025-05-15T15:05:40.545Z","avatar_url":"https://github.com/emk.png","language":"Dockerfile","funding_links":[],"categories":["Development tools","Dockerfile","开发工具 Development tools","开发工具"],"sub_categories":["Deployment","部署 Deployment","部署"],"readme":"# `rust-musl-builder`: Docker container for easily building static Rust binaries\n\n[![Docker Image](https://img.shields.io/docker/pulls/ekidd/rust-musl-builder.svg?maxAge=2592000)](https://hub.docker.com/r/ekidd/rust-musl-builder/)\n\n- [Source on GitHub](https://github.com/emk/rust-musl-builder)\n- [Changelog](https://github.com/emk/rust-musl-builder/blob/master/CHANGELOG.md)\n\n**UPDATED:** This repository is effectively dead at this point, given the increasing rarity of crates which require OpenSSL.\n\nHowever, **[`rustls`](rustls) now works well** with most of the Rust ecosystem, including `reqwest`, `tokio`, `tokio-postgres`, `sqlx` and many others. The only major project which still requires `libpq` and OpenSSL is [Diesel](https://diesel.rs/). If you don't need `diesel` or `libpq`:\n\n- See if you can switch away from OpenSSL, typically by using `features` in `Cargo.toml` to ask your dependencies to use [`rustls`](rustls) instead.\n- If you don't need OpenSSL, try [`cross build --target=x86_64-unknown-linux-musl --release`](https://github.com/rust-embedded/cross) to cross-compile your binaries for `libmusl`. This supports many more platforms, with less hassle!\n\n[rustls]: https://github.com/rustls\n\n## What is this?\n\nThis image allows you to build static Rust binaries using `diesel`, `sqlx` or `openssl`. These images can be distributed as single executable files with no dependencies, and they should work on any modern Linux system.\n\nTo try it, run:\n\n```sh\nalias rust-musl-builder='docker run --rm -it -v \"$(pwd)\":/home/rust/src ekidd/rust-musl-builder'\nrust-musl-builder cargo build --release\n```\n\nThis command assumes that `$(pwd)` is readable and writable by uid 1000, gid 1000. At the moment, it doesn't attempt to cache libraries between builds, so this is best reserved for making final release builds.\n\nFor a more realistic example, see the `Dockerfile`s for [examples/using-diesel](./examples/using-diesel) and [examples/using-sqlx](./examples/using-sqlx).\n\n## Deploying your Rust application\n\nWith 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!\n\n## Available tags\n\nIn general, we provide the following tagged Docker images:\n\n- `latest`, `stable`: Current stable Rust, now with OpenSSL 1.1. We\n  try to update this fairly rapidly after every new stable release, and\n  after most point releases.\n- `X.Y.Z`: Specific versions of stable Rust.\n- `beta`: This usually gets updated every six weeks alongside the stable\n  release. It will usually not be updated for beta bugfix releases.\n- `nightly-YYYY-MM-DD`: Specific nightly releases. These should almost\n  always support `clippy`, `rls` and `rustfmt`, as verified using\n  [rustup components history][comp]. If you need a specific date for\n  compatibility with `tokio` or another popular library using unstable\n  Rust, please file an issue.\n\nAt a minimum, each of these images should be able to\ncompile [examples/using-diesel](./examples/using-diesel) and [examples/using-sqlx](./examples/using-sqlx).\n\n[comp]: https://rust-lang.github.io/rustup-components-history/index.html\n\n## Caching builds\n\nYou may be able to speed up build performance by adding the following `-v` commands to the `rust-musl-builder` alias:\n\n```sh\n-v cargo-git:/home/rust/.cargo/git\n-v cargo-registry:/home/rust/.cargo/registry\n-v target:/home/rust/src/target\n```\n\nYou will also need to fix the permissions on the mounted volumes:\n\n```sh\nrust-musl-builder sudo chown -R rust:rust \\\n  /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src/target\n```\n\n## How it works\n\n`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new [rustup][] `target` support.  It includes static versions of several libraries:\n\n- The standard `musl-libc` libraries.\n- OpenSSL, which is needed by many Rust applications.\n- `libpq`, which is needed for applications that use `diesel` with PostgreSQL.\n- `libz`, which is needed by `libpq`.\n- SQLite3. See [examples/using-diesel](./examples/using-diesel/).\n\nThis library also sets up the environment variables needed to compile popular Rust crates using these libraries.\n\n## Extras\n\nThis image also supports the following extra goodies:\n\n- Basic compilation for `armv7` using `musl-libc`. Not all libraries are supported at the moment, however.\n- [`mdbook`][mdbook] and `mdbook-graphviz` for building searchable HTML documentation from Markdown files. Build manuals to use alongside your `cargo doc` output!\n- [`cargo about`][about] to collect licenses for your dependencies.\n- [`cargo deb`][deb] to build Debian packages\n- [`cargo deny`][deny] to check your Rust project for known security issues.\n\n## Making OpenSSL work\n\nIf 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`](https://crates.io/crates/openssl-probe) as follows:\n\n```rust\nfn main() {\n    openssl_probe::init_ssl_cert_env_vars();\n    //... your code\n}\n```\n\n## Making Diesel work\n\nIn addition to setting up OpenSSL, you'll need to add the following lines to your `Cargo.toml`:\n\n```toml\n[dependencies]\ndiesel = { version = \"1\", features = [\"postgres\", \"sqlite\"] }\n\n# Needed for sqlite.\nlibsqlite3-sys = { version = \"*\", features = [\"bundled\"] }\n\n# Needed for Postgres.\nopenssl = \"*\"\n```\n\nFor PostgreSQL, you'll also need to include `diesel` and `openssl` in your `main.rs` in the following order (in order to avoid linker errors):\n\n```rust\nextern crate openssl;\n#[macro_use]\nextern crate diesel;\n```\n\nIf this doesn't work, you _might_ be able to fix it by reversing the order. See [this PR](https://github.com/emk/rust-musl-builder/issues/69) for a discussion of the latest issues involved in linking to `diesel`, `pq-sys` and `openssl-sys`.\n\n## Making static releases with Travis CI and GitHub\n\nThese instructions are inspired by [rust-cross][].\n\nFirst, read the [Travis CI: GitHub Releases Uploading][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:\n\n```yaml\nlanguage: rust\nsudo: required\nos:\n- linux\n- osx\nrust:\n- stable\nservices:\n- docker\nbefore_deploy: \"./build-release myapp ${TRAVIS_TAG}-${TRAVIS_OS_NAME}\"\ndeploy:\n  provider: releases\n  api_key:\n    secure: \"...\"\n  file_glob: true\n  file: \"myapp-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.*\"\n  skip_cleanup: true\n  on:\n    rust: stable\n    tags: true\n```\n\nNext, copy [`build-release`](./examples/build-release) into your project and run `chmod +x build-release`.\n\nFinally, add a `Dockerfile` to perform the actual build:\n\n```Dockerfile\nFROM ekidd/rust-musl-builder\n\n# We need to add the source code to the image because `rust-musl-builder`\n# assumes a UID of 1000, but TravisCI has switched to 2000.\nADD --chown=rust:rust . ./\n\nCMD cargo build --release\n```\n\nWhen 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.\n\nFor a working example, see [faradayio/cage][cage].\n\n[rust-cross]: https://github.com/japaric/rust-cross\n[uploading]: https://docs.travis-ci.com/user/deployment/releases\n[cage]: https://github.com/faradayio/cage\n\n## Making tiny Docker images with Alpine Linux and Rust binaries\n\nDocker now supports [multistage builds][multistage], 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`](./examples/using-diesel/Dockerfile).\n\n[multistage]: https://docs.docker.com/engine/userguide/eng-image/multistage-build/\n[Alpine Linux]: https://alpinelinux.org/\n\n## Adding more C libraries\n\nIf 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`](./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.\n\nIf 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.\n\n## Development notes\n\nAfter modifying the image, run `./test-image` to make sure that everything works.\n\n## Other ways to build portable Rust binaries\n\nIf for some reason this image doesn't meet your needs, there's a variety of other people working on similar projects:\n\n- [messense/rust-musl-cross](https://github.com/messense/rust-musl-cross) shows how to build binaries for many different architectures.\n- [japaric/rust-cross](https://github.com/japaric/rust-cross) has extensive instructions on how to cross-compile Rust applications.\n- [clux/muslrust](https://github.com/clux/muslrust) also supports libcurl.\n- [golddranks/rust_musl_docker](https://github.com/golddranks/rust_musl_docker). Another Docker image.\n\n## License\n\nEither the [Apache 2.0 license](./LICENSE-APACHE.txt), or the\n[MIT license](./LICENSE-MIT.txt).\n\n[Alpine Linux container]: https://hub.docker.com/_/alpine/\n[about]: https://github.com/EmbarkStudios/cargo-about\n[deb]: https://github.com/mmstick/cargo-deb\n[deny]: https://github.com/EmbarkStudios/cargo-deny\n[mdbook]: https://github.com/rust-lang-nursery/mdBook\n[musl-libc]: http://www.musl-libc.org/\n[musl-gcc]: http://www.musl-libc.org/how.html\n[rustup]: https://www.rustup.rs/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femk%2Frust-musl-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femk%2Frust-musl-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femk%2Frust-musl-builder/lists"}