|
1 | | -# pycross_image |
| 1 | +# @pycross_image |
2 | 2 |
|
3 | | -`pycross_image` provides starlark rules for building container images for python |
4 | | -applications with bazel. |
| 3 | + |
5 | 4 |
|
6 | | -You can use the rules directly from this repo, or simply as examples and |
7 | | -copy/paste them into your own project. |
| 5 | +Bazel starlark rules for building container images from `py_binary` :sparkles: |
| 6 | +using [@rules_pycross](https://github.com/jvolkman/rules_pycross) :magic:. |
8 | 7 |
|
| 8 | +`@pycross_image` provides: |
9 | 9 |
|
| 10 | +- `load("@pycross_image//bazel/rules:oci.bzl", "py_image")`: image rule |
| 11 | + compatible with [@rules_oci](https://github.com/bazel-contrib/rules_oci) |
| 12 | +- `load("@pycross_image//bazel/rules:docker.bzl", "py_image")`: image rule |
| 13 | + compatible with [@rules_docker](https://github.com/bazelbuild/rules_docker) |
| 14 | + |
| 15 | +## Installation & Usage |
| 16 | + |
| 17 | +See [releases] page for an `http_archive` of the latest `@pycross_image`. |
| 18 | + |
| 19 | +Examples: |
| 20 | +- [@rules_oci example](example/oci/WORKSPACE.in). |
| 21 | +- [@rules_docker example](example/docker/WORKSPACE.in). |
| 22 | + |
| 23 | +A few notes about the workspace setup: |
| 24 | + |
| 25 | +- It's divided into "steps" based on load statement dependencies. `step1.bzl` |
| 26 | + only depends on things declared in `repositories.bzl`, `step2.bzl` depends on |
| 27 | + things declared in `step3.bzl`, etc (this pattern is from |
| 28 | + [tensorflow](https://github.com/tensorflow/tensorflow/tree/master/tensorflow)). |
| 29 | +- Your workspace may already have many of the dependencies. Some of the |
| 30 | + external workspace names may be differ from yours. Use the example as a study |
| 31 | + guide rather than canonical reference. |
| 32 | +- The examples are only tested with the older `WORKSPACE`. The rules may not be |
| 33 | + compatible with bzlmod yet. |
| 34 | + |
| 35 | +## How it Works |
| 36 | + |
| 37 | +```mermaid |
| 38 | +graph TD; |
| 39 | + pypi[(pypi)] |
| 40 | + DefaultInfo[[DefaultInfo]] |
| 41 | + numpy{{numpy}} |
| 42 | + grpclib{{grpclib}} |
| 43 | +
|
| 44 | + subgraph linux_x86_64["linuxx86_64\n"] |
| 45 | + image.tar-->image; |
| 46 | + image-->app_layer; |
| 47 | + image-->site_packages_layer; |
| 48 | + image-->interpreter_layer; |
| 49 | + app_layer-->DefaultInfo; |
| 50 | + site_packages_layer-->DefaultInfo; |
| 51 | + interpreter_layer-->DefaultInfo; |
| 52 | + end |
| 53 | +
|
| 54 | + DefaultInfo--transition :linux_x86_64-->pycross_binary; |
| 55 | + pycross_binary-->py_binary; |
| 56 | + py_binary-->numpy; |
| 57 | + py_binary-->grpclib; |
| 58 | +
|
| 59 | + numpy-.->numpy-cp310-macosx_arm64.whl |
| 60 | + numpy-.->numpy-cp310-manylinux_x86_64.whl |
| 61 | + grpclib-.->grpclib.whl |
| 62 | + numpy-cp310-macosx_arm64.whl-->pypi |
| 63 | + numpy-cp310-manylinux_x86_64.whl-->pypi |
| 64 | +
|
| 65 | + subgraph zig_toolchain |
| 66 | + grpclib.whl-->grpclib-tar.gz |
| 67 | + end |
| 68 | +
|
| 69 | + grpclib-tar.gz-->pypi |
| 70 | +
|
| 71 | + style pypi fill:#3171b2,stroke:#333 |
| 72 | + style numpy fill:#5d97d2,stroke:#3171b2,color:black |
| 73 | + style grpclib fill:#5d97d2,stroke:#3171b2,color:black |
| 74 | + style grpclib.whl stroke:#bc082b |
| 75 | +
|
| 76 | + style numpy-cp310-manylinux_x86_64.whl stroke:#bc082b |
| 77 | + style numpy-cp310-macosx_arm64.whl stroke:#bb22d8 |
| 78 | + style linux_x86_64 stroke:#bc082b,fill:none |
| 79 | +``` |
| 80 | + |
| 81 | +In this example the `py_binary` rule has `deps` on two python wheels: |
| 82 | + - `numpy` has a binary wheel available from pypi for both the darwin and linux |
| 83 | + platforms. |
| 84 | + - `grpclib` only has a source distribution available. |
| 85 | + |
| 86 | +The `pycross_binary` rule transitions from the host platform to `:linux_x86_64`. |
| 87 | + - the transition affects how `@rules_pycross` fetches wheels. If the binary |
| 88 | + distribution is available, take it. |
| 89 | + - if the binary distribution is not available, compile from source using (in |
| 90 | + this case, with `zig` and `@hermetic_cc_toolchains`). |
| 91 | + |
| 92 | +The image is partitioned into three tar layers by matching against filename |
| 93 | +patterns (see rule implementation for details). |
| 94 | + |
| 95 | +> `@rules_pycross` supports dependency fetching using PDM or poetry. |
0 commit comments