Skip to content

Commit c441622

Browse files
added docker example
1 parent bdae77e commit c441622

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

examples/MODULE.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ bazel_dep(name = "rules_python", version = "0.36.0")
44
bazel_dep(name = "boost.any", version = "1.83.0")
55
bazel_dep(name = "boost.smart_ptr", version = "1.83.0")
66
bazel_dep(name = "boost.thread", version = "1.83.0")
7+
bazel_dep(name = "rules_oci", version = "2.0.1")
8+
bazel_dep(name = "rules_pkg", version = "1.0.1")
79

810
# This import is relevant for these examples and this (rules_ros) repository.
911
bazel_dep(name = "rules_ros")
@@ -44,3 +46,18 @@ use_repo(
4446
"urdfdom",
4547
"urdfdom_headers",
4648
)
49+
50+
# in order to build an oci image with our ros nodes in it, one
51+
# needs to define the base image to use, and that must be performed
52+
# at the module level here.
53+
# NOTE: Even though the version of python that the nodes run in is contained
54+
# in the runfiles, a version of python is required at the system level in order
55+
# to run the roslaunch command.
56+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
57+
oci.pull(
58+
name = "rules_ros_base_image",
59+
image = "python:3.10-bookworm",
60+
digest = "sha256:fd0fa50d997eb56ce560c6e5ca6a1f5cf8fdff87572a16ac07fb1f5ca01eb608",
61+
platforms = ["linux/amd64"],
62+
)
63+
use_repo(oci, "rules_ros_base_image", "rules_ros_base_image_linux_amd64")

examples/chatter/BUILD.bazel

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ load("@rules_ros//ros:launch.bzl", "ros_launch")
1919
load("@rules_ros//ros:test.bzl", "ros_test")
2020
load("@rules_ros//ros:topic.bzl", "ros_topic")
2121
load("@rules_ros//third_party:expand_template.bzl", "expand_template")
22+
load(
23+
"@rules_oci//oci:defs.bzl",
24+
"oci_image",
25+
"oci_load"
26+
)
27+
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
2228

2329
# Handling of ROS messages & services resembles to some extent Bazel's rules for
2430
# handling protobuf messages (e.g. proto_library and cc_proto_library).
@@ -127,3 +133,37 @@ ros_topic(
127133
name = "rostopic",
128134
deps = [":chatter"],
129135
)
136+
137+
138+
# packages up the nodes, launch file, and all the other required runfiles.
139+
# Note the symlink is required in order to place the files where ros_launch is
140+
# expecting them to be.
141+
pkg_tar(
142+
name = "chatter_tar",
143+
srcs = [
144+
":chatter",
145+
],
146+
include_runfiles = True,
147+
package_dir = "chatter",
148+
symlinks = {
149+
"/chatter/chatter.runfiles/_main/external": "/chatter/chatter.runfiles",
150+
}
151+
)
152+
153+
# builds the oci image on top of the @rules_ros_base_image defined at the MODULE level.
154+
oci_image(
155+
name = "chatter_oci_image",
156+
base = "@rules_ros_base_image",
157+
tars = [":chatter_tar"],
158+
workdir = "/chatter/chatter.runfiles/_main",
159+
entrypoint = ["/chatter/chatter"],
160+
)
161+
162+
# loads the oci image into the docker daemon.
163+
# Example usage: `bazel run //chatter:chatter_oci_load`
164+
# Then to run the container: `docker run -it --rm chatter:latest`
165+
oci_load(
166+
name = "chatter_oci_load",
167+
image = ":chatter_oci_image",
168+
repo_tags = ["chatter:latest"],
169+
)

0 commit comments

Comments
 (0)