为什么 container_layer 没有添加到 container_image 中?
Why container_layer is not getting added in the container_image?
我有以下 bazel 定义:
docker/project/BUILD.bazel
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//docker/package_managers:download_pkgs.bzl", "download_pkgs")
load("@io_bazel_rules_docker//docker/package_managers:install_pkgs.bzl", "install_pkgs")
load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
package(default_visibility = ["//visibility:public"])
download_pkgs(
name = "apt_pkgs_installables",
image_tar = "@ubuntu_bionic_base_image//image",
packages = [
"build-essential",
"libedit-dev",
"libpq-dev",
"libpython3.7",
"libpython3.7-dev",
"python",
"python-dev",
"python3.7",
"python3-dev",
"python3-pip",
"python3-setuptools",
"python3-venv",
"python3-wheel",
],
)
install_pkgs(
name = "apt_pkgs",
image_tar = "@ubuntu_bionic_base_image//image",
installables_tar = ":apt_pkgs_installables.tar",
output_image_name = "apt_pkgs_image",
)
container_run_and_commit(
name = "python3_extras",
commands = [
"ln -s /usr/share/doc/python3-venv /usr/bin/python3-venv",
"ln -s --force /usr/bin/python3 /usr/bin/python",
"ln -s --force /usr/bin/pip3 /usr/bin/pip",
"pip install behave==1.2.6",
"pip install kubernetes==21.7.0",
"pip install polling==0.3.2",
"pip install jinja2==3.0.3",
],
image = ":apt_pkgs.tar",
)
container_image(
name = "project-base",
base = "@ubuntu_bionic_base_image//image",
visibility = ["//visibility:public"],
tars = [
"python3_extras_commit.tar"
]
)
container_layer(
name = "project-src",
data_path = "//project",
directory = "/opt/app/",
files = [
"//project",
],
mode = "0o755",
)
container_image(
name = "project",
base = "@ubuntu_bionic_base_image//image",
layers = [
":project-src"
],
tars = [
"python3_extras_commit.tar",
]
)
项目结构为:
.
├── BUILD.bazel
├── README.md
├── WORKSPACE
├── docker
│ ├── BUILD.bazel
│ └── project
│ └── BUILD.bazel
├── go.mod
├── go.sum
├── project
│ ├── BUILD.bazel
│ ├── Makefile
│ ├── behave.ini
project/BUILD.bazel
有一个文件组:
package(default_visibility = ["//visibility:public"])
filegroup(
name = "project",
srcs = [
"Makefile",
"behave.ini",
],
)
预期的输出是 /opt/app/
下的 Makefile
和 bahave.ini
文件在 project-src
container_layer
中声明,但最终的 docker 图片没有这些文件。
bazel 4.0.0
构建命令:
bazel build //docker/project:project --verbose_failures --sandbox_debug
原来Bazel命令应该是:
bazel run //docker/project:project --verbose_failures --sandbox_debug
和 base
对于 container_image
project
应该是 :python3_extras_commit.tar
container_run_and_commit
生成一个新图像,因此您不需要从中创建另一个图像。解决此问题的最直接方法是删除 project-base
并在您使用它的任何地方使用 python3_extras_commit.tar
。另外,将 project
的 base
更改为 python3_extras_commit.tar
:
container_image(
name = "project",
base = ":python3_extras_commit.tar",
layers = [
":project-src"
],
)
如果您出于某种原因希望安装的 apt 包作为单独的层,请改用 container_run_and_commit_layer。请注意,结果在 layers
中,而不是 tars
。但是,那不会有来自 download_pkgs
的 apt 包,因为它们将是一个单独的层,所以如果你走那条路,你将不得不单独管理它们。
作为参考,rules_docker 有很多很好的例子。 testing/examples/run_instruction_arbitrary/BUILD 展示了大部分作品。
我有以下 bazel 定义:
docker/project/BUILD.bazel
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@io_bazel_rules_docker//docker/package_managers:download_pkgs.bzl", "download_pkgs")
load("@io_bazel_rules_docker//docker/package_managers:install_pkgs.bzl", "install_pkgs")
load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")
load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
package(default_visibility = ["//visibility:public"])
download_pkgs(
name = "apt_pkgs_installables",
image_tar = "@ubuntu_bionic_base_image//image",
packages = [
"build-essential",
"libedit-dev",
"libpq-dev",
"libpython3.7",
"libpython3.7-dev",
"python",
"python-dev",
"python3.7",
"python3-dev",
"python3-pip",
"python3-setuptools",
"python3-venv",
"python3-wheel",
],
)
install_pkgs(
name = "apt_pkgs",
image_tar = "@ubuntu_bionic_base_image//image",
installables_tar = ":apt_pkgs_installables.tar",
output_image_name = "apt_pkgs_image",
)
container_run_and_commit(
name = "python3_extras",
commands = [
"ln -s /usr/share/doc/python3-venv /usr/bin/python3-venv",
"ln -s --force /usr/bin/python3 /usr/bin/python",
"ln -s --force /usr/bin/pip3 /usr/bin/pip",
"pip install behave==1.2.6",
"pip install kubernetes==21.7.0",
"pip install polling==0.3.2",
"pip install jinja2==3.0.3",
],
image = ":apt_pkgs.tar",
)
container_image(
name = "project-base",
base = "@ubuntu_bionic_base_image//image",
visibility = ["//visibility:public"],
tars = [
"python3_extras_commit.tar"
]
)
container_layer(
name = "project-src",
data_path = "//project",
directory = "/opt/app/",
files = [
"//project",
],
mode = "0o755",
)
container_image(
name = "project",
base = "@ubuntu_bionic_base_image//image",
layers = [
":project-src"
],
tars = [
"python3_extras_commit.tar",
]
)
项目结构为:
.
├── BUILD.bazel
├── README.md
├── WORKSPACE
├── docker
│ ├── BUILD.bazel
│ └── project
│ └── BUILD.bazel
├── go.mod
├── go.sum
├── project
│ ├── BUILD.bazel
│ ├── Makefile
│ ├── behave.ini
project/BUILD.bazel
有一个文件组:
package(default_visibility = ["//visibility:public"])
filegroup(
name = "project",
srcs = [
"Makefile",
"behave.ini",
],
)
预期的输出是 /opt/app/
下的 Makefile
和 bahave.ini
文件在 project-src
container_layer
中声明,但最终的 docker 图片没有这些文件。
bazel 4.0.0
构建命令:
bazel build //docker/project:project --verbose_failures --sandbox_debug
原来Bazel命令应该是:
bazel run //docker/project:project --verbose_failures --sandbox_debug
和 base
对于 container_image
project
应该是 :python3_extras_commit.tar
container_run_and_commit
生成一个新图像,因此您不需要从中创建另一个图像。解决此问题的最直接方法是删除 project-base
并在您使用它的任何地方使用 python3_extras_commit.tar
。另外,将 project
的 base
更改为 python3_extras_commit.tar
:
container_image(
name = "project",
base = ":python3_extras_commit.tar",
layers = [
":project-src"
],
)
如果您出于某种原因希望安装的 apt 包作为单独的层,请改用 container_run_and_commit_layer。请注意,结果在 layers
中,而不是 tars
。但是,那不会有来自 download_pkgs
的 apt 包,因为它们将是一个单独的层,所以如果你走那条路,你将不得不单独管理它们。
作为参考,rules_docker 有很多很好的例子。 testing/examples/run_instruction_arbitrary/BUILD 展示了大部分作品。