{"id":13450971,"url":"https://github.com/nickdecooman/bazel-rust-docker-example","last_synced_at":"2026-03-02T16:39:43.649Z","repository":{"id":71654435,"uuid":"454540679","full_name":"nickdecooman/bazel-rust-docker-example","owner":"nickdecooman","description":"Demonstrating Bazel setup for Rust services with Cargo and Docker support.","archived":false,"fork":false,"pushed_at":"2022-02-10T07:46:48.000Z","size":54,"stargazers_count":13,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-06T04:36:12.999Z","etag":null,"topics":["bazel","bazel-examples","cargo","cross-platform","docker","rust","vscode"],"latest_commit_sha":null,"homepage":"","language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nickdecooman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2022-02-01T20:29:47.000Z","updated_at":"2024-08-08T20:43:48.000Z","dependencies_parsed_at":"2023-09-10T11:00:57.611Z","dependency_job_id":null,"html_url":"https://github.com/nickdecooman/bazel-rust-docker-example","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/nickdecooman%2Fbazel-rust-docker-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickdecooman%2Fbazel-rust-docker-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickdecooman%2Fbazel-rust-docker-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickdecooman%2Fbazel-rust-docker-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickdecooman","download_url":"https://codeload.github.com/nickdecooman/bazel-rust-docker-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245149322,"owners_count":20568875,"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":["bazel","bazel-examples","cargo","cross-platform","docker","rust","vscode"],"created_at":"2024-07-31T07:00:41.112Z","updated_at":"2026-03-02T16:39:38.612Z","avatar_url":"https://github.com/nickdecooman.png","language":"Starlark","readme":"[![CircleCI](https://circleci.com/gh/nickdecooman/bazel-rust-docker-example/tree/main.svg?style=svg)](https://circleci.com/gh/nickdecooman/bazel-rust-docker-example/tree/main)\n\n# bazel-rust-docker-example\nThis project is intended to demonstrate a Bazel setup for Rust services with Docker support. \n\nIt has the following features:\n* 🏗  [Bazel](https://bazel.build/) build system\n* 🦀 Compile, test and run [Rust](https://www.rust-lang.org/) binaries, libraries and tests\n* 🧰 Cargo workspace and [Cargo Raze](https://github.com/google/cargo-raze) for managing Rust dependencies\n* 🧐  Support for [Rust-analyzer](https://rust-analyzer.github.io/) (and VS Code)\n* ⚖️ Optimized Rust compiler flags for release targets\n* 📦  Minimal Docker Rust images with [distroless/static-debian11](https://github.com/GoogleContainerTools/distroless/blob/main/base/README.md) as base image (2.36MB)\n* 🚀 [CircleCI](https://circleci.com/) pipeline with Bazel [remote-cache](https://github.com/buchgr/bazel-remote) on AWS S3\n* ⚙️ Support for cross-compiling Rust targets from MacOS to Linux using [Musl](https://musl.libc.org/)\n\n## Prerequisites\n\n* Install [Bazelisk](https://github.com/bazelbuild/bazelisk)\n\n* Install [Rustup](https://rustup.rs/)\n\n* Install [Cargo Raze](https://github.com/google/cargo-raze)\n```bash\n$ cargo install cargo-raze\n```\n\n* For cross-compilation from MacOS to Linux musl targets, install [musl-cross](https://github.com/FiloSottile/homebrew-musl-cross)\n```bash\n$ brew install filosottile/musl-cross/musl-cross\n```\n\n* For Bazel remote cache, install [https://github.com/buchgr/bazel-remote](https://github.com/buchgr/bazel-remote)\n\n## Bazel Commands\n\n### Build the entire project:\n\n```bash\n$ bazel build //...\n```\n\n### Run all tests:\n\n```bash\n$ bazel test //...\n```\n\n### Run service1 natively:\n\n```bash\n$ bazel run //service1:app\n```\n\n### Run service1 within a Docker container:\n\n```bash\n$ bazel run //service1:image\n```\n⚠️ This only works when your host machine is Linux. Look for cross-compiling below when building on MacOS.\n\n### Run service1 within an optimzed release Docker container:\n\n```bash\n$ bazel run //service1:image --//:release\n```\n\n⚠️ This only works when your host machine is Linux. Look for cross-compiling below when building on MacOS.\n\n### Cross-compile service1 from MacOS to Linux and run within a Docker container:\n\n```bash\n$ bazel run //service1:image --platforms //build/platform:musl\n```\n\nCan also be combined with ` --//:release`.\n\n## Rust-Analyzer\n\nIn order for Rust-Analyzer to work, it is necessary to generate a `rust-project.json` file. To do so, run\n\n```bash\n$ bazel run @rules_rust//tools/rust_analyzer:gen_rust_project\n```\n\n## VS Code\n\nWhen using VS Code as IDE, install the following extension for Rust-Analyzer: https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer\n\nIn your VS Code `settings.json`, make sure to have the following configuration:\n\n```json\n{ \n  \"rust-analyzer.checkOnSave.overrideCommand\": [\"cargo\", \"check\" \"--message-format=json\"]\n}\n```\n\nThe Rust-Analyzer extension will pick up the `rust-project.json` file which was generated earlier.\n\n⚠️ Make sure to generate an updated `rust-project.json` file whenever a new folder or dependency is added. It may also be needed to reload the workspace when the updated structure is not immediately recognized by Rust-Analyzer.\n\n## Cargo Dependencies\n\nFor each of the dependencies listed in any of the `Cargo.toml` files, a Bazel build should be generated. This is done automatically by running\n\n```bash\n$ cargo raze\n```\n\nIf not already the case, this will add a `cargo` folder in the root of the project. Crates can now be referenced fron within Bazel through `//cargo/*NAME*` (eg. `//cargo/ferris_says`).\n\n### Adding a new dependency\n\nAfter adding a new external dependency in any of the `Cargo.toml` files, run the following steps:\n\n1. Run `cargo raze --generate-lockfile`\n2. Run `bazel build //...`\n3. Update `rust-project.json` by running `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`\n4. Reload Rust Workspace in VS Code\n5. Start using crate in your Rust library or binary\n\n## Cross-compiling\n\nThis project provides a basic setup for cross-compiling from MacOS to Linux. The setup is inspired by [rust-bazel-cross](https://github.com/duarten/rust-bazel-cross).\n\n⚠️ A known limitation is that dependencies relying on macros fail to compile. More info [here](https://github.com/duarten/rust-bazel-cross/issues/2).\n\n## Bazel remote-cache\n\nWhen applying the Bazel config flag `--config=remote_cache`, Bazel will connect to a remote cache running at `localhost:9090`. This server will sync files between a local cache and an AWS S3 bucket.\n\nTo start the bazel-remote server, run the script `./scripts/start_bazel_remote.sh`. Provide the following environment variables:\n* *AWS_REGION*\n* *AWS_ACCESS_KEY_ID*\n* *AWS_SECRET_ACCESS_KEY*\n* *AWS_S3_BUCKET_BAZEL_CACHE*\n\n## Did this help you?\n\n[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/N4N5ADJPF)\n","funding_links":["https://ko-fi.com/N4N5ADJPF"],"categories":["Resources"],"sub_categories":["Demos"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickdecooman%2Fbazel-rust-docker-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickdecooman%2Fbazel-rust-docker-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickdecooman%2Fbazel-rust-docker-example/lists"}