dockerkubernetesbazelcontainer-image

How can I define an owner to an empty_dir using container_image or container_layer from bazel rules_docker?


From the PR that implemented empty_dirs, it seems there's support for defining dir owners (with the names argument) and mode into the add_empty_dir method of TarFile class.

But the container_image rule (and container_layer) supports only mode.

This works:

container_image(
    name = "with_empty_dirs",
    empty_dirs = [
        "etc",
        "foo",
        "bar",
    ],
    mode = "0o777",
)

But this returns an error: "ERROR: (...) no such attribute 'names' in 'container_image_' rule":

container_image(
    name = "with_empty_dirs",
    empty_dirs = [
        "etc",
        "foo",
        "bar",
    ],
    names = "nginx",
)

Do we need to “write a customized container_image” if we want to add support for owner of empty_dirs?


Solution

  • In a BUILD file, the attribute you're looking for is ownername. See the pkg_tar reference documentation documentation for more details. Also, I don't think you can pass it directly to container_image, you have to create a separate pkg_tar first. Like this:

    pkg_tar(
        name = "with_empty_dirs_tar",
        empty_dirs = [
            "etc",
            "foo",
            "bar",
        ],
        ownername = "nginx.nginx",
    )
    
    container_image(
        name = "with_empty_dirs",
        tars = [":with_empty_dirs_tar"],
    )
    

    In general, container_image has a subset of pkg_tar as direct attributes to make simple forms of adding files, but for complex use cases you should create a pkg_tar yourself for full access to all of its features for adding/arranging files and setting their ownership.

    The names you see in that PR is a variable in a Python helper tool which the BUILD file rules use as part of the implementation. There's a layer of abstraction between what you write in a BUILD file and that Python code.