I have the following bazel definition:
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",
]
)
The project structure is:
.
├── BUILD.bazel
├── README.md
├── WORKSPACE
├── docker
│ ├── BUILD.bazel
│ └── project
│ └── BUILD.bazel
├── go.mod
├── go.sum
├── project
│ ├── BUILD.bazel
│ ├── Makefile
│ ├── behave.ini
project/BUILD.bazel
have a filegroup:
package(default_visibility = ["//visibility:public"])
filegroup(
name = "project",
srcs = [
"Makefile",
"behave.ini",
],
)
The expected output is to have the Makefile
and bahave.ini
files under /opt/app/
declared in project-src
container_layer
, but the final docker image does not have these files.
bazel 4.0.0
Build command:
bazel build //docker/project:project --verbose_failures --sandbox_debug
2
Answers
It turns out the Bazel command should be:
and
base
forcontainer_image
project
should be:python3_extras_commit.tar
container_run_and_commit
produces a new image, so you don’t need to create another image from it. The most direct way to fix this is removingproject-base
and usepython3_extras_commit.tar
wherever you’re using it. Also, change thebase
forproject
topython3_extras_commit.tar
:If you want the installed apt packages as a separate layer for some reason, use container_run_and_commit_layer instead. Note that the result of that goes in
layers
, nottars
. However, that will not have the apt packages fromdownload_pkgs
because those will be a separate layer, so you’ll have to manage those separately if you go that route.For reference, rules_docker has lots of good examples. testing/examples/run_instruction_arbitrary/BUILD shows most of these pieces.