Search code examples
dockerfilebazel

How to specify equivalent of Dockerfile RUN for bazel container_image rule?


I'm converting a Dockerfile to a bazel container_image rule. How would I specify a RUN specification for container_image?


Solution

  • container_run_and_commit will run commands and make a new layer with the result, which is similar to RUN. Something like this:

    load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")
    
    container_run_and_commit(
      name = "x",
      commands = ["touch /xyz"],
      image = ":my_base.tar",
    )
    

    The image argument is a docker save-style tarball. You can bring this in via an external repository, or use the implicit .tar output from a container_image rule. My example above will work if you have a container_image(name = "my_base") in the same package.