How can I add a file to the docker context that is...
# general
t
How can I add a file to the docker context that is located in the root of my repo, e.g.
Copy code
entrypoint.sh
pants.toml
/serviceA
    Dockerfile
    BUILD
And the COPY in the Dockerfile looks like this:
COPY entrypoint.sh /
What needs to be added to the BUILD file? It is always complaining that the file cannot be found.
f
See https://www.pantsbuild.org/docs/docker#build-docker-image-example. Add a
BUILD
in root of your repo and add a
file
target for it. Then depend on that
file
target in your
docker_image
target.
t
serviceA
BUILD
file:
Copy code
docker_image(
    name="service-runtime",
    source="runtime.Dockerfile",
    dependencies = ["//:entrypoint"],
)
root
BUILD
file:
Copy code
file(
    name='entrypoint',
    source="lambda_entrypoint.sh",
)
Result:
Copy code
#9 [4/5] COPY lambda_entrypoint.sh /
#9 sha256:4262b25edb23c1b1e7dd5b18cf34f449f18864e45c0ce62ee4fa0b0b2329e8b2
#9 ERROR: "/lambda_entrypoint.sh" not found: not found
What am I missing?
f
time to debug then: pass
--no-process-cleanup
to your Pants invocation. The log will print out the directory where the execution sandbox has been preserved. Let’s see if the file made it into the execution sandbox. (There will also be a
__run.sh
script with the
docker build
invocation.)
c
In order to include files in your docker build context from “outside” your subtree where your
docker_image
target is defined, you need to adjust the
context_root
(see
./pants docker_image --help
)
t
Copy code
15:35:00.22 [INFO] Preserving local process execution dir /tmp/pants-sandbox-Kugjdt for "Building docker image service-runtime:latest"

...

vscode ➜ /nefino_li (feature/DEV-2928-add-all-services ✗) $ ls -la /tmp/pants-sandbox-Kugjdt
total 20
drwxr-xr-x 3 vscode vscode 4096 Jun 29 15:35 .
drwxrwxrwt 1 root   root   4096 Jun 29 15:35 ..
-rwxr-xr-x 1 vscode vscode  378 Jun 29 15:35 __run.sh
-rw-r--r-- 1 vscode vscode  185 Jun 29 15:35 lambda_entrypoint.sh
drwxr-xr-x 2 vscode vscode 4096 Jun 29 15:35 services
vscode ➜ /nefino_li (feature/DEV-2928-add-all-services ✗) $
Here services=serviceA directory
c
in your
__run.sh
I would expect to see a docker invocation, that uses a subdirectory of your sandbox as build context.
hence, the lambda script is present in the sandbox, but would not be included in the build context sent to docker.
Normally, there should’ve been some additional logging from pants regarding the build context, which files was present, and mention of the context_root option to possibly include missing files.
t
__run.sh
content:
Copy code
#!/bin/bash
# This command line should execute the same process as pants did internally.
export FUNCTION_DIR=/function PACKAGE_DOWNLOAD_PATH=/packages VARIANT=3.8-bullseye
cd /tmp/pants-sandbox-Kugjdt
/usr/bin/docker build --tag $'service-runtime:latest' --build-arg FUNCTION_DIR --build-arg PACKAGE_DOWNLOAD_PATH --build-arg VARIANT --file services/runtime.Dockerfile services
c
yep, indeed. the final
services
on the docker build line there is the context root.
try running it with
docker_image(context_root="", ...)
in order to include all files in you sandbox in the build context.
🙌 1
that will affect your paths you need to use for your
COPY
instructions, mind you.
t
context_root=""
did the trick. Thank you! Learned something new again!
👍 1
c
(thanks for jumping in to assist @fast-nail-55400)
👍 1