Skip to main content

Use container images by rules_docker with other tools

This document shows some real examples to work with rules_docker with out-of-bazel worlds.

info

For Flattmates: You may need to replace bazel command with ./bazel.sh in our monorepo.

Use images with docker command

By running following command, you can expose the built image to docker world (i.e. /var/lib/docker/...):

$ bazel run //path/to/container/image/target:image
INFO: Analyzed target //path/to/container/image/target:image (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //path/to/container/image/target:image up-to-date:
/private/var/tmp/bazel-host/execroot/__main__/bazel-out/darwin_arm64-dbg/bin/path/to/container/image/target/image-layer.tar
INFO: Elapsed time: 0.320s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Loaded image ID: sha256:343ab854c3b17473defbff7636aaa7041a1a634c22d60a6d40f3b241310df03f
Tagging 343ab854c3b17473defbff7636aaa7041a1a634c22d60a6d40f3b241310df03f as bazel/path/to/container/image/target:image

You can validate that the image bazel/path/to/container/image/target:image is now available as follows:

$ docker inspect `bazel/path/to/container/image/target:image`
[
{
"Id": "sha256:343ab854c3b17473defbff7636aaa7041a1a634c22d60a6d40f3b241310df03f",
"RepoTags": [
"bazel/path/to/container/image/target:image"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "1970-01-01T00:00:00Z"
....

Use images with dlayer command

When you just need a tarball for an image, you can use <target name>.tar build target to obtain it:

$ bazel build //path/to/container/image/target:image.tar
INFO: Analyzed target //path/to/container/image/target:image.tar (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //path/to/container/image/targetr:image.tar up-to-date:
/private/var/tmp/bazel-host/execroot/__main__/bazel-out/darwin_arm64-dbg/bin/bpath/to/container/image/target/image.tar
INFO: Elapsed time: 0.313s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

For instance, you can use the tarball with dlayer as follows:

# in bash
bazel build //path/to/container/image/target:image.tar
dlayer -f $(./bazel.sh info bazel-genfiles)/path/to/container/image/target/image.tar

# in fish
bazel build //path/to/container/image/target:image.tar
dlayer -f (./bazel.sh info bazel-genfiles)/path/to/container/image/target/image.tar