quaint-telephone-89068
02/28/2023, 12:32 PM/
- pants.toml
- project
- requirements.txt
- dirs_with_python_code
- sub_dir
- Dockerfile
- utils
- dirs_with_python_code
Dockerfile looks like this:
FROM ......
ENV PYTHONPATH .
WORKDIR /app
COPY project project
COPY utils utils
RUN pip3 install -r project/requirements.txt
My goal is to build a docker image that would include the utils and project directories, and whose root corresponds to my repository's root, so that I can install requirement as done in the dockerfile, and so that python scripts in the container can e.g. from utils import ....
This I can easily do without Pants, by simply running docker build from the repo's root. However, I'm not able to achieve the same with the docker_image Pants target. Here is what I have already tried and how I failed.
1. I have defined my docker_image in a BULID file next to the Dockerfile. I've tried setting different values for context_root unsure which one would correspond to the repo's root: ".", "./", "". I have set dependencies=["project:project", "utils@resolve=project"]. Outcome of running `pants package`: the COPY instruction failed stating that project and utils don't exist.
2. Suspecting the context may not be set to the root, I've moved my docker_image target to a BUILD file in the root dir and modified its source accordingly. The effect was still the same.
3. Instead of copying things manually via a Dockerfile, I decided to try using a pex binary. In project's BUILD, I've added the following:
pex_binary(
name="project_binary",
dependencies=[":project", "utils:utils"],
resolve="project",
layout="packed",
execution_mode="venv",
)
docker_image(
name="project_image",
repository="project",
instructions=[
"FROM ......,
"COPY project/project_binary.pex /bin/project",
"RUN pip3 install -r project/requirements.txt",
]
)
Result: it failed on not finding the requirements file.
4. I have removed the RUN instruction from the Dockerfile which allowed by to successfully build an image. I ran the container to look inside and found that bin/project/utils only contains the __init__.py file and no other files that are inside the utils dir. The same for the project dir, that's why no requirements file was found. This corresponds to the fact that running pants dependencies on my Dockerfile or pex binary target yields:
project/__init__.py
utils/__init__.py@resolve=project
No other files except for the inits are included.
Please advise me on what am I doing wrong! My goal is as stated above: just recreate with Pants what I can already do with docker build.
Pants version
2.14.1
OS
MacOS 12.5.1 with M1 chip
EDIT
I've created a minimum reproducible example which is much simpler and the same problem manifests in it.
At my repository's root, I've crated a Dockerfile that copies everything into the image:
FROM alpine
WORKDIR /app
COPY . .
Also at the root, I have the following BUILD file:
docker_image(
name="root_image",
)
Then, I run:
pants package :root_image
docker run -it root_image:latest
Inside the container:
ls
This only shows a Dockerfile, no other files or directories are included in the image.
I try adding different dependencies to docker_image, but whatever I pass there, no other files or directories are included in the docker image.
On the contrary, when I run docker build . -f Dockerfile -t root_image:latest from the repo's root, I obtain an image that has all the repo content in it. I would like to recreate this with Pants.
pantsbuild/pants