3

I'd like to slim down a debian 10 Docker image using Bazel and then flatten the result into a single layer image.

Here's the code I have:

load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_flatten", "container_bundle", "container_import")
load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")


container_run_and_commit(
  name = "debian10_slimmed_layers",
  commands = ["rm -rf /usr/share/man/*"],
  image = "@debian10//image",
)

# Create an image just so we can flatten it.
container_image(
  name = "debian10_slimmed_image",
  base = ":debian10_slimmed_layers",
)

# Flatten the layers to a single layer.
container_flatten(
    name = "debian10_flatten",
    image = ":debian10_slimmed_image",
)

Where I'm stuck is I can't figure out how to use the output of debian10_flatten to produce a runnable Docker image.

I tried:

container_image(
  name = "debian10",
  base = ":debian10_flatten",
)

That fails with:

2021/06/27 13:16:25 Unable to load docker image from bazel-out/k8-fastbuild/bin/debian10_flatten.tar:
file manifest.json not found in tar
Joe
  • 3,370
  • 4
  • 33
  • 56

1 Answers1

4

container_flatten gives you the filesystem tarball. You need to add the tarball as tars in debian10, instead of deps:

container_image(
  name = "debian10",
  tars = [":debian10_flatten.tar"],
)

deps is for another container_image rule (or equivalent). If you had a docker save-style tarball, container_load would be the way to get the container_image equivalent.

I figured this out looking at the implementation in container/flatten.bzl. The docs could definitely use some improvements if somebody wants to open a PR (they're generated from the python-style docstring in that .bzl file).

Brian Silverman
  • 3,085
  • 11
  • 13