creamy-branch-34921
01/01/2025, 6:53 PMpython_sources(
name="app",
sources=["app.py"],
)
docker_image(
name="book-creator",
source="Dockerfile",
dependencies=[
"src/services/utils:utils",
"src/pants_poc/db:db",
":app"
]
)
python_tests(
name="tests",
dependencies=[
":app",
"//:tests"
]
)
However the problem is for some reason the dependencies are not found in the docker build context. Below is the output when i run the command : pants run src/services/book_creator/Dockerfile
:
19:51:33.51 [INFO] Completed: Building docker image book-creator:latest
19:51:33.51 [WARN] Docker build failed for `docker_image` src/services/book_creator:book-creator. The src/services/book_creator/Dockerfile has `COPY` instructions for source files that may not have been found in the Docker build context.
The following files were not found in the Docker build context:
* .
* src/services/book_creator
* src/services/utils
19:51:33.52 [ERROR] 1 Exception encountered:
Engine traceback:
in `run` goal
ProcessExecutionFailure: Process 'Building docker image book-creator:latest' failed with exit code 1.
stdout:
stderr:
#0 building with "default" instance using docker driver
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 1.15kB done
#1 DONE 0.0s
#2 [auth] elevait/python-deployment-base:pull token for <http://harbor.elevait.io|harbor.elevait.io>
#2 DONE 0.0s
#3 [internal] load metadata for <http://harbor.elevait.io/elevait/python-deployment-base:v3.12|harbor.elevait.io/elevait/python-deployment-base:v3.12>
#3 DONE 0.3s
#4 [internal] load .dockerignore
#4 transferring context: 2B done
#4 DONE 0.0s
#5 [build 1/3] FROM <http://harbor.elevait.io/elevait/python-deployment-base:v3.12@sha256:ae335c36461032f6d557c95af16d3af8301d7ec4c13e7c7dc3316ec6b3f2fd20|harbor.elevait.io/elevait/python-deployment-base:v3.12@sha256:ae335c36461032f6d557c95af16d3af8301d7ec4c13e7c7dc3316ec6b3f2fd20>
#5 DONE 0.0s
#6 [internal] load build context
#6 transferring context: 1.28kB done
#6 DONE 0.0s
#5 [build 1/3] FROM <http://harbor.elevait.io/elevait/python-deployment-base:v3.12@sha256:ae335c36461032f6d557c95af16d3af8301d7ec4c13e7c7dc3316ec6b3f2fd20|harbor.elevait.io/elevait/python-deployment-base:v3.12@sha256:ae335c36461032f6d557c95af16d3af8301d7ec4c13e7c7dc3316ec6b3f2fd20>
#5 CACHED
#7 [build 3/3] RUN --mount=type=cache,sharing=locked,id=pants_poc-pip-cache,target=~/.cache/pip pip install --target /service/site-packages .[service] --constraint requirements.txt
#7 CACHED
#8 [stage-1 2/4] COPY --from=build /service/site-packages /home/elevait/.local/lib/sitepackages
#8 CACHED
#9 [stage-1 3/4] COPY src/services/utils /home/elevait/.local/lib/sitepackages/utils
#9 ERROR: failed to calculate checksum of ref SYJ3:OJS6:3SFC:5MHZ:E3AE:MBNF:XA26:QHF7:4UJD:G7MW:45EC:DJUI::lnqiea0yfwuq5ux0udfzmj8lk: "/src/services/utils": not found
#10 [build 2/3] COPY --chown=1000 . .
#10 CANCELED
------
> [stage-1 3/4] COPY src/services/utils /home/elevait/.local/lib/sitepackages/utils:
------
Dockerfile:25
--------------------
23 | COPY --from=build /service/site-packages ${TARGET_DIR}
24 |
25 | >>> COPY src/services/utils ${TARGET_DIR}/utils
26 |
27 | COPY src/services/book_creator .
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref SYJ3:OJS6:3SFC:5MHZ:E3AE:MBNF:XA26:QHF7:4UJD:G7MW:45EC:DJUI::lnqiea0yfwuq5ux0udfzmj8lk: "/src/services/utils": not found
Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.
However when i do a peek
into the dockerfile i can see the dependencies, so ideally i would assume that pants would copy them over in the context:
> pants peek src/services/book_creator/Dockerfile
[
{
"address": "src/services/book_creator:book-creator",
"target_type": "docker_image",
"build_network": null,
"build_platform": null,
"cache_from": null,
"cache_to": null,
"context_root": null,
"dependencies": [
"src/pants_poc/db/__init__.py",
"src/pants_poc/db/books_model.py",
"src/services/book_creator/app.py:app",
"src/services/utils/__init__.py",
"src/services/utils/common.py"
],
"dependencies_raw": [
"src/services/utils:utils",
"src/pants_poc/db:db",
":app"
],
"description": null,
"extra_build_args": [],
"extra_build_hosts": null,
"extra_run_args": [],
"goals": [
"package",
"publish",
"run"
],
"image_labels": null,
"image_tags": [
"latest"
],
"instructions": null,
"output": {
"type": "docker"
},
"output_path": "${spec_path_normalized}/${target_name_normalized}${file_suffix}",
"pull": false,
"registries": [
"<all default registries>"
],
"repository": null,
"restartable": false,
"secrets": null,
"skip_push": false,
"source_raw": "Dockerfile",
"sources": [
"src/services/book_creator/Dockerfile"
],
"sources_fingerprint": "55bbea08a13b77e619eb0b8582aa5ec3525ff228b7b01165c88561bd9b0b9edc",
"squash": false,
"ssh": [],
"tags": null,
"target_stage": null
}
]
elegant-florist-94385
01/02/2025, 11:37 AMpants --keep-sandboxes=always package ::
and check your logs for something like Preserving sandbox dir /tmp/<somethingsomething> for: Build docker image
and then you can cd
into that directory. There should be a file called __run.sh
which is the exact command pants runs to build your docker image. You can inspect/modify/re-run for debugging purposes to see if you can see what the issue is. (Translating these fixes back into your build files is an exercise for the reader).
The rest of this sandbox dir will be a subset of your repo/filesystem, so you can check to make sure all the expected files are there (ie. if dependencies were brought in correctly)elegant-florist-94385
01/02/2025, 11:41 AMpex_binary
executable, which depends only on the runtime environment containing a suitable python interpreter, and then your Dockerfile just becomes:
FROM base_image
COPY <name of pex> target_location/app.pex # pants will dependency inference to find the pex binary
CMD [./target_location/app.pex]
Great blog post about this at https://www.pantsbuild.org/blog/2022/08/02/optimizing-python-docker-deploys-using-pants