{"id":15701108,"url":"https://github.com/nwtgck/docker-rust-musl-builder","last_synced_at":"2025-05-12T15:35:12.167Z","repository":{"id":39918639,"uuid":"417254554","full_name":"nwtgck/docker-rust-musl-builder","owner":"nwtgck","description":"original: https://github.com/emk/rust-musl-builder","archived":false,"fork":false,"pushed_at":"2024-01-27T13:25:39.000Z","size":149,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T23:41:51.306Z","etag":null,"topics":["docker","musl","rust","single-binary"],"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/nwtgck.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":"2021-10-14T19:15:35.000Z","updated_at":"2025-03-14T06:21:38.000Z","dependencies_parsed_at":"2024-01-27T14:41:45.230Z","dependency_job_id":null,"html_url":"https://github.com/nwtgck/docker-rust-musl-builder","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fdocker-rust-musl-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fdocker-rust-musl-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fdocker-rust-musl-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fdocker-rust-musl-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nwtgck","download_url":"https://codeload.github.com/nwtgck/docker-rust-musl-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253766399,"owners_count":21960907,"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":["docker","musl","rust","single-binary"],"created_at":"2024-10-03T19:59:49.833Z","updated_at":"2025-05-12T15:35:12.094Z","avatar_url":"https://github.com/nwtgck.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `rust-musl-builder`: Docker container for easily building static Rust binaries\n\n## About\n\n```bash\ndocker run nwtgck/rust-musl-builder:1.56.1\n```\n\n```bash\ndocker run nwtgck/rust-musl-builder\n```\n\nDocker Hub: \u003chttps://hub.docker.com/r/nwtgck/rust-musl-builder/tags\u003e\n\nThe original repository is \u003chttps://github.com/emk/rust-musl-builder\u003e, but recently not maintained. So I made this repository and automated Docker build \u0026 push.\n\n\u003c!-- [![Docker Image](https://img.shields.io/docker/pulls/ekidd/rust-musl-builder.svg?maxAge=2592000)](https://hub.docker.com/r/ekidd/rust-musl-builder/) --\u003e\n\n----\n\n## Original README\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:** Major updates in this release which may break some builds. See [the CHANGELOG](https://github.com/emk/rust-musl-builder/blob/master/CHANGELOG.md) for details. If these updates break your build, you can update your `Dockerfile` to use `FROM ekidd/rust-musl-builder:1.48.0` to revert to the previous version.\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```txt\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```toml\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```rust\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## ARM support (experimental)\n\nTo target ARM hard float (Raspberry Pi):\n\n```sh\nrust-musl-builder cargo build --target=armv7-unknown-linux-musleabihf --release\n```\n\nBinaries will be written to `target/$TARGET_ARCHITECTURE/release`. By default it targets `x86_64-unknown-linux-musl` unless specified with `--target`.\n\nThis is missing many of the libraries used by the `x86_64` build, and it should probably be split out of the base image and given its own tags.\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%2Fnwtgck%2Fdocker-rust-musl-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnwtgck%2Fdocker-rust-musl-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnwtgck%2Fdocker-rust-musl-builder/lists"}