Home > Net >  Copy directory containing Bazel build file to Docker container
Copy directory containing Bazel build file to Docker container

Time:11-14

I am trying to copy a directory to my docker container. What I have noticed is that if the source directory contains bazel build file then that directory doesn't get copied.

Source code:

load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

container_run_and_commit(
    name = "bitnami-grafana-base",
    commands = [
        "tdnf install yum -y",
    ],
    image = "@photon_base_image//image"
)

pkg_tar(
    name = "scripts-dir",
    srcs = glob(["scripts/**"]),
    strip_prefix = ".",
    package_dir = "/"
)


container_image(
    name = "bitnami-grafana",
    base = ":bitnami-grafana-base_commit.tar",
    visibility = ["//visibility:public"],
    files = ["test.sh"],
    workdir = "/",
    tars = [":scripts-dir"],
    cmd = ["/test.sh"]
)

the src in pkg_tar doesn't get copied when I bash into the container, whereas if I just replace it some other directory(which is inside the same location as the scripts directory) shows up. Also, if I remove all the Bazel build files from the scripts` directory then it gets copied.

So, why is it that a directory doesn't get copied if contains Bazel files? How to copy those directories?

CodePudding user response:

One part I know, not sure if this the best way.

Question: How do I copy directory containing bazel build files. Answer:

In the root directory of source directory(which you want to copy to the container) put/edit bazel build file and add following to this bazel build file:

filegroup(
    name = "scripts_dir",
    srcs = glob([
        "**/*",
    ]),
)

Above will get all the files and you can refer that in other bazel build files with //something/something/scripts:scripts_dir

And then on the build file which creates container and copies the dir add this: The rest part of this build file, please see in the question.

pkg_tar(
    name = "scripts-dir",
    srcs = [
        "//something/something/scripts:scripts_dir",
    ],
    strip_prefix = ".",
    package_dir = "/"
)

Why I can't just copy any directory without making the above changes is still puzzles me.

  • Related