Skip to content

Commit a847004

Browse files
committed
Add container example
1 parent 2b5e6f5 commit a847004

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

examples/bzlmod/container/.bazelrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
common --@rules_python//python/config_settings:bootstrap_impl=script
2+
common --@rules_python//python/config_settings:venvs_site_packages=no
3+
# See https://github.com/bazel-contrib/rules_python/issues/2864#issuecomment-2859325467
4+
common --@rules_python//python/config_settings:venvs_use_declare_symlink=yes
5+
6+
7+
# With container
8+
# | vens_site_packages | venvs_use_declare_symlink | result |
9+
# |--------------------|-----------------------------|---------|
10+
# | yes | no | failure |
11+
# | no | no | success |
12+
# | no | yes | failure |
13+
# | yes | yes | failure |
14+
# | -----------------------------------------------------------|
15+
# | all commented out | success |
16+
17+
18+
# Without container
19+
# | vens_site_packages | venvs_use_declare_symlink | result |
20+
# |--------------------|-----------------------------|---------|
21+
# | yes | no | failure |
22+
# | no | no | success |
23+
# | no | yes | success |
24+
# | yes | yes | failure |
25+
# | -----------------------------------------------------------|
26+
# | all commented out | success |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.2.1

examples/bzlmod/container/BUILD.bazel

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@aspect_rules_py//py:defs.bzl", "py_image_layer")
2+
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
3+
load("@rules_python//python:defs.bzl", "py_binary")
4+
5+
licenses(["notice"])
6+
7+
py_binary(
8+
name = "server",
9+
srcs = ["server.py"],
10+
deps = [
11+
"@pypi//absl_py",
12+
"@pypi//flask",
13+
],
14+
)
15+
16+
oci_image(
17+
name = "server_image",
18+
base = "@docker_io_python",
19+
entrypoint = ["/app/server"],
20+
tars = py_image_layer(
21+
name = "server_layer",
22+
binary = ":server",
23+
root = "/app/",
24+
),
25+
)
26+
27+
oci_load(
28+
name = "server_image.load",
29+
image = ":server_image",
30+
repo_tags = ["server:latest"],
31+
)
32+
33+
filegroup(
34+
name = "server_image.tar",
35+
srcs = [":server_image.load"],
36+
output_group = "tarball",
37+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Example of python inside container."""
2+
3+
module(
4+
name = "container-example",
5+
version = "0.0.1",
6+
)
7+
8+
bazel_dep(name = "rules_python", version = "1.4.1")
9+
bazel_dep(name = "rules_oci", version = "2.2.6")
10+
bazel_dep(name = "platforms", version = "0.0.11")
11+
bazel_dep(name = "aspect_rules_py", version = "1.6.0")
12+
bazel_dep(name = "rules_pkg", version = "1.1.0")
13+
14+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
15+
python.toolchain(
16+
is_default = True,
17+
python_version = "3.12",
18+
)
19+
python.override(minor_mapping = {"3.12": "3.12.7"})
20+
21+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
22+
pip.parse(
23+
hub_name = "pypi",
24+
python_version = "3.12",
25+
requirements_lock = "requirements.txt",
26+
)
27+
28+
use_repo(pip, "pypi")
29+
30+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
31+
oci.pull(
32+
name = "docker_io_python",
33+
digest = "sha256:034724ef64585eeb0e82385e9aabcbeabfe5f7cae2c2dcedb1da95114372b6d7",
34+
image = "docker.io/library/python",
35+
platforms = [
36+
"linux/amd64",
37+
"linux/arm64/v8",
38+
],
39+
tag = "3.13-slim",
40+
)
41+
use_repo(oci, "docker_io_python", "docker_io_python_linux_amd64", "docker_io_python_linux_arm64_v8")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
absl-py
2+
flask
3+
redis
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file was autogenerated by uv via the following command:
2+
# uv pip compile ./requirements.in -o ./requirements.txt
3+
absl-py==2.3.1
4+
# via -r ./requirements.in
5+
blinker==1.9.0
6+
# via flask
7+
click==8.2.1
8+
# via flask
9+
flask==3.1.1
10+
# via -r ./requirements.in
11+
itsdangerous==2.2.0
12+
# via flask
13+
jinja2==3.1.6
14+
# via flask
15+
markupsafe==3.0.2
16+
# via
17+
# flask
18+
# jinja2
19+
# werkzeug
20+
redis==6.2.0
21+
# via -r ./requirements.in
22+
werkzeug==3.1.3
23+
# via flask

examples/bzlmod/container/server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""A HTTP server example."""
2+
3+
from collections.abc import Sequence
4+
import os
5+
import signal
6+
import time
7+
8+
from absl import app
9+
from absl import flags
10+
from flask import Flask
11+
12+
server = Flask(__name__)
13+
14+
15+
@server.route("/")
16+
def index() -> str:
17+
return f"Hello, World! This is Flask."
18+
19+
20+
def main(argv: Sequence[str]) -> None:
21+
del argv
22+
23+
def handler(signum, frame):
24+
# Raising an exception is a clean way to break out of server.run()
25+
raise SystemExit("Server timed out, shutting down.")
26+
27+
# Set the alarm signal handler
28+
signal.signal(signal.SIGALRM, handler)
29+
# Trigger the alarm after 5 seconds
30+
signal.alarm(2)
31+
32+
try:
33+
server.run(host="0.0.0.0", port=8080, debug=False)
34+
except SystemExit as e:
35+
print(e)
36+
37+
38+
if __name__ == "__main__":
39+
app.run(main)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy