Skip to content

Commit 0efc30c

Browse files
committed
chore: docs
1 parent 059a669 commit 0efc30c

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

oci_python_image/MODULE.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ register_toolchains(
2222
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
2323

2424
oci.pull(
25-
name = "distroless_python",
25+
name = "docker_python",
2626
digest = "sha256:b48e216f7c4adcf24fecd7016f3b8ead76866a19571819f67f47c1ccaf899717",
27+
# Shorthand for https://hub.docker.com/_/python
2728
image = "python",
2829
)
2930

@@ -37,4 +38,4 @@ oci.pull(
3738
],
3839
)
3940

40-
use_repo(oci, "distroless_base", "distroless_python")
41+
use_repo(oci, "distroless_base", "docker_python")

oci_python_image/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example of Python plus rules_oci
2+
3+
There are a couple ways to treat the interpreter:
4+
5+
1. Choose a base image which includes it. This is the typical approach outside Bazel, and is
6+
recommended when migrating from a Dockerfile or similar build system to avoid changing
7+
multiple things at the same time. In `/MODULE.bazel` we use `oci.pull` to pull https://hub.docker.com/_/python and then in `use_python_base/BUILD.bazel` we choose that as the base image. Then, the Bazel-built Python application just falls through to the "system interpreter".
8+
2. Propagate Bazel's "toolchain resolution" so that the same interpreter used by `bazel run`
9+
will also be used when the Python application runs in a container. This avoids version skew in the Python version used. In `/MODULE.bazel` we use `oci.pull` to pull the Distroless base image (which is tiny). Then the `interpreter_as_layer/BUILD.bazel` file shows how we get that interpreter as a layer in the image.
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
load(":current_py_toolchain.bzl", "current_py_toolchain")
2+
load("@rules_oci//oci:defs.bzl", "oci_image")
3+
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
4+
5+
current_py_toolchain(
6+
name = "current_py_toolchain_runfiles",
7+
)
8+
19
pkg_tar(
2-
name = "interpreter",
3-
srcs = ["@rules_python//python:current_py_toolchain"],
10+
name = "py_interpreter_toolchain",
11+
# If we just used @rules_python//python:current_py_toolchain then we would end up with platform
12+
# specific paths here. The current_py_toolchain_runfiles remaps the paths to be platform independent
13+
srcs = [":current_py_toolchain_runfiles"],
414
extension = "tar.gz",
515
package_dir = "/opt/python",
616
strip_prefix = ".",
717
)
818

19+
pkg_tar(
20+
name = "python_aliases",
21+
symlinks = {
22+
"/usr/bin/python": "/usr/bin/fake_python",
23+
"/usr/bin/python3": "/usr/bin/fake_python",
24+
},
25+
)
26+
927
oci_image(
10-
name = "image",
28+
name = "py_base",
1129
base = "@distroless_base",
12-
entrypoint = ["/opt/hello_world/hello_world_bin"],
13-
tars = [":hello_world_layer"],
30+
tars = [
31+
":python_aliases",
32+
":py_interpreter_toolchain",
33+
],
34+
visibility = ["//visibility:public"],
1435
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("@rules_pkg//:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
2+
3+
def _strip_platform_specific(path):
4+
parts = path.split("/")
5+
return "/".join(parts[2:])
6+
7+
def _current_py_toolchain_impl(ctx):
8+
default = ctx.toolchains["@rules_python//python:toolchain_type"].py3_runtime
9+
file_map = {}
10+
11+
files = depset(transitive = [default.files])
12+
13+
for file in files.to_list():
14+
file_map[_strip_platform_specific(file.path)] = file
15+
16+
files = depset([], transitive = [files])
17+
18+
return [
19+
PackageFilegroupInfo(
20+
pkg_dirs = [],
21+
pkg_files = [
22+
[PackageFilesInfo(
23+
dest_src_map = file_map,
24+
attributes = {},
25+
), ctx.label],
26+
],
27+
),
28+
DefaultInfo(files = files),
29+
]
30+
31+
current_py_toolchain = rule(
32+
implementation = _current_py_toolchain_impl,
33+
attrs = {},
34+
toolchains = ["@rules_python//python:toolchain_type"],
35+
)

oci_python_image/use_python_base/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ py_image_layer(
3131

3232
oci_image(
3333
name = "image",
34-
base = "@distroless_python",
34+
base = "@docker_python",
3535
entrypoint = ["/opt/hello_world/hello_world_bin"],
3636
tars = [":hello_world_layer"],
3737
)

0 commit comments

Comments
 (0)