Bazel docker_image 复制额外文件夹

huangapple go评论89阅读模式
英文:

Bazel docker_image copy extra folder

问题

我目前正在使用bazel来构建我的GoLang应用程序。

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"],
 )

除了my-golang-app文件夹之外,在构建这个镜像时,我需要复制另一个名为my-new-folder的文件夹及其内部的所有内容。

在bazel中如何实现这一点?我在bazel文档中找不到解决方案。

dockerfile {
             run 'mkdir -p /usr/local/bin'
             // 添加以下行
             add {
                 from 'docker/my-new-folder/'
                 into '/'
             }
}
英文:

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 '/'
             }

答案1

得分: 2

使用pkg_tar,就像container_image文档中的参考一样。类似这样的代码:

pkg_tar(
    name = "my-files",
    srcs = glob(["my-new-folder/**"]),
    strip_prefix = ".",
)

container_image(
    name = "my-golang-app",
    files = [":my-golang-app"],
    <你已经有的其他内容>,
    tars = [":my-files"],
)

pkg_tar有各种选项来控制文件的存放位置,如果你想要更复杂的安排,我发现通过deps将多个目录链接在一起的多个pkg_tar规则是一个有用的模式。

英文:

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

pkg_tar(
    name = &quot;my-files&quot;,
    srcs = glob([&quot;my-new-folder/**&quot;]),
    strip_prefix = &quot;.&quot;,
)

container_image(
    name = &quot;my-golang-app&quot;,
    files = [&quot;:my-golang-app&quot;],
    &lt;everything else you already have&gt;,
    tars = [&quot;:my-files&quot;],
)

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.

huangapple
  • 本文由 发表于 2021年6月10日 13:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/67915098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定