Search code examples
gobazelbazel-rules

Bazel docker_image copy extra folder


I am currently using bazel for my GoLang app.

container_image(
     name = "my-golang-app",
     base = "@ubuntu_base//image",
     cmd = ["/bin/my-golang-app"],
     directory = "/bin/",
     files = [":my-golang-app"],
     tags = [
         "manual",
         VERSION,
     ],
     visibility = ["//visibility:public"],
 )

Except my-golang-app folder I need to copy, while building this image, another folder named my-new-folder and everything in it.

How does one do that with bazel? I can't seem to find the solution in the bazel docs.

dockerfile {
             run 'mkdir -p /usr/local/bin'
             // add the lines below
             add {
                 from 'docker/my-new-folder/'
                 into '/'
             }

Solution

  • Use pkg_tar like the container_image docs reference. Something like this:

    pkg_tar(
        name = "my-files",
        srcs = glob(["my-new-folder/**"]),
        strip_prefix = ".",
    )
    
    container_image(
        name = "my-golang-app",
        files = [":my-golang-app"],
        <everything else you already have>,
        tars = [":my-files"],
    )
    

    pkg_tar has various options to control where your files end up, if you want something beyond just taking an entire directory. For more complicated arrangements, I find multiple pkg_tar rules for various directories linked together via deps a helpful pattern.