【问题标题】:Running docker build with bazel genrule and a dockerfile使用 bazel genrule 和 dockerfile 运行 docker build
【发布时间】:2022-11-23 03:34:38
【问题描述】:

我有一个包含多种语言和工件的 monorepo,我想过渡到 Bazel。 我们想使用我们现有的 Dockerfiles 构建 docker 镜像,使用 genrule - 避免将所有 dockerfiles 转换为 docker-rules(至少在这一点上)。

我们知道这不是 Bazel 的最佳实践,但我们认为它可以让我们轻松过渡。

我正在测试这个 Dockerfile

FROM alpine:3.8
ENTRYPOINT ["echo"]
CMD ["Hello Bazel!"]

我尝试关注this post,但在运行 docker build 命令时(甚至在 Bazel 之外)我得到了这个 -

> tar -czh . | docker build -t hello-bazel -

[+] Building 0.1s (2/2) FINISHED                                                                                                                        
 => [internal] load remote build context                                                                                                           0.0s
 => ERROR copy /context /                                                                                                                          0.1s
------
 > copy /context /:
------
failed to solve with frontend dockerfile.v0: failed to read dockerfile: Error processing tar file(gzip: invalid header): 

我尝试将 genrule 与基本的 docker build 命令一起使用 -

genrule(
    name = "gc-hello-bazel",
    srcs = ["Dockerfile"],
    outs = ["imagesha.txt"],
    cmd = "docker build -t hello-bazel -f $(location Dockerfile) . > $@",
    tools = ["Dockerfile"],
)

但是构建失败了

failed to solve with frontend dockerfile.v0: failed to read dockerfile: open Dockerfile: no such file or directory

如果重要的话,这是我的目录结构:

-WORKSPACE
-<some-root-dirctories>
-<a-root-directory>
    -<subdir>
       -<subsubdir1>
       -my_docker
           -Dockerfile
           -BUILD.bazel

我究竟做错了什么?

TL;DR:我正在寻找一个使用 Dockerfile 和 Bazel 的 Genrule 构建 docker 的工作示例

【问题讨论】:

    标签: build dockerfile bazel bazel-rules bazel-genrule


    【解决方案1】:

    这不是您问题的确切答案,因为它使用的是规则而不是流派。但我认为它应该解决你的根本问题。

    在 bazelbuild/rules_docker 下有一个(非密封的)rule for building docker images from a Dockerfile

    要使用它,您需要将以下内容添加到您的 WORKSPACE 文件中;

    # file: //WORKSPACE
    http_archive(
        name = "io_bazel_rules_docker",
        sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
        urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
    )
    

    然后在您的构建文件中,您可以添加以下内容;

    # file: BUILD.bazel
    load("@io_bazel_rules_docker//contrib:dockerfile_build.bzl", "dockerfile_image")
    
    dockerfile_image(
        name = "my_non_hermetic_image",
        dockerfile = ":Dockerfile",
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多