I have a bazel project (monorepo), that has some go and python code. I use bazel for building. For each of these, I want to create separate container. For creating python image, I am following this instruction. https://github.com/aspect-build/bazel-examples/blob/main/oci_python_image/hello_world/BUILD.bazel
It seems, rules_docker is deprecated and hasn't been maintained for over a year, and rules_oci is what is recommended.
My python app runs using bazel run without any issues. Also, when creating the image, and loading it to docker images all work. But when I try to run, I get
exec /parser/parser_bin: no such file or directory even though the file is present in the docker container, I was able to look using docker cp.
WORKSPACE file
http_archive(
name = "rules_oci",
sha256 = "cf6b8be82cde30daef18a09519d75269650317e40d917c8633cf8e3ab5645ea5",
strip_prefix = "rules_oci-1.7.2",
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.7.2/rules_oci-v1.7.2.tar.gz",
)
load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")
rules_oci_dependencies()
load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "oci_register_toolchains")
oci_register_toolchains(
name = "oci",
crane_version = LATEST_CRANE_VERSION,
# Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks.
# Note that it does not support docker-format images.
# zot_version = LATEST_ZOT_VERSION,
)
# You can pull your base images using oci_pull like this:
load("@rules_oci//oci:pull.bzl", "oci_pull")
oci_pull(
name = "ubuntu2204",
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
image = "marketplace.gcr.io/google/ubuntu2204",
platforms = [
"linux/amd64",
"linux/arm64",
],
)
parser/BUILD.bazel file
py_binary(
name = "parser_bin",
srcs = ["main.py"],
main = "main.py",
visibility = ["//visibility:public"],
deps = [":fizz"],
)
pkg_tar(
name = "parser_tar",
srcs = [":parser_bin"],
)
py_oci_image(
name = "parser_image",
base = "@ubuntu2204",
binary = "parser_bin",
entrypoint = ["/parser/parser_bin"],
)
oci_tarball(
name = "parser_tarball",
image = ":parser_image",
repo_tags = ["myname/fizzparser:v0.0.1"],
)
oci_push(
name = "parser_push",
image = ":parser_image",
remote_tags = ["v0.0.1"],
repository = "myname/fizzparser",
)
Run on the host without docker works.:
bazel run //parser:parser_bin
Building image and running in docker. Build tarball worked
bazel build //parser:parser_tarball
INFO: Analyzed target //parser:parser_tarball (9 packages loaded, 46 targets configured).
INFO: Found 1 target...
Target //parser:parser_tarball up-to-date:
bazel-bin/parser/parser_tarball/tarball.tar
INFO: Elapsed time: 0.345s, Critical Path: 0.23s
INFO: 6 processes: 4 internal, 2 darwin-sandbox.
INFO: Build completed successfully, 6 total actions
Loading to docker locally worked
docker load --input bazel-bin/parser/parser_tarball/tarball.tar
Loaded image: myname/fizzparser:v0.0.1
Docker images also shows
myname/fizzparser v0.0.1 b1210a6e568a 12 months ago 19.6MB
Docker run fails.
docker run -it myname/fizzparser:v0.0.1
exec /parser/parser_bin: no such file or directory
To verify if the file is present, I ran docker cp and copied the file to a local directory, and ls parser/ listed
ls -l
total 0
drwxr-xr-x 2 jd staff 64 Feb 28 22:02 bin
drwxr-xr-x 2 jd staff 64 Dec 31 1969 boot
drwxr-xr-x 5 jd staff 160 Feb 28 21:49 dev
drwxr-xr-x 25 jd staff 800 Feb 28 21:49 etc
drwxr-xr-x 3 jd staff 96 Dec 31 1969 home
drwxr-xr-x 4 jd staff 128 Dec 31 1969 lib
drwxr-xr-x 5 jd staff 160 Feb 28 22:02 parser
drwxr-xr-x 2 jd staff 64 Dec 31 1969 proc
drwx------ 2 jd staff 64 Dec 31 1969 root
drwxr-xr-x 2 jd staff 64 Dec 31 1969 run
drwxr-xr-x 2 jd staff 64 Dec 31 1969 sbin
drwxr-xr-x 2 jd staff 64 Dec 31 1969 sys
drwxrwxrwt 2 jd staff 64 Dec 31 1969 tmp
drwxr-xr-x 9 jd staff 288 Dec 31 1969 usr
drwxr-xr-x 11 jd staff 352 Dec 31 1969 var
ls -l parser
total 56
-rwxr-xr-x 1 jd staff 3561 Jan 1 2023 main.py
-rwxr-xr-x 1 jd staff 21830 Jan 1 2023 parser_bin
drwxr-xr-x 6 jd staff 192 Jan 1 2023 parser_bin.runfiles
What am I missing? The ubuntu base image doesn't seem to have any utilities. I am not sure following the instructions includes the python interpreter within the image.
Note: To check what happens if a docker couldn't really find the file, I changed the entrypoint.
py_oci_image(
name = "parser_image",
base = "@ubuntu2204",
binary = "parser_bin",
entrypoint = ["/parser/parser_bin1"], # This is incorrect
)
The doing the same steps, and finally running the docker container gave slightly different results.
docker run -it myname/fizzparser:v0.0.1
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "/parser/parser_bin1": stat /parser/parser_bin1: no such file or directory: unknown.
ERRO[0000] error waiting for container: context canceled
This seems to be the correct message when the file is actually not present. But when it was present when the entrypoint was set correctly to /parser/parser_bin, it was giving a different error.
How to debug this issue? If not with docker, is there any other way to run the oci container?