pantsbuild / pants

The Pants Build System
https://www.pantsbuild.org
Apache License 2.0
3.3k stars 633 forks source link

Docker Image Building Error #21236

Open HusseinLakkis01 opened 2 months ago

HusseinLakkis01 commented 2 months ago

Describe the bug I am new to pants but I cannot seem to able to build a docker image using package :: . I always get the error where I cannot copy contents of the context directory to the container. It seems that it is missing the context. I provide the context in the build file but it still doesnt work. Is there anything to set in the config toml?

FROM python:3.10
RUN mkdir -p /app
# Set the working directory in the container
WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

# tun uvicorn with the main module
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

Pants version 2.21.0

OS MacOS Additional info Add any other information about the problem here, such as attachments or links to gists, if relevant.

lilatomic commented 2 months ago

Can you show the BUILD file too?

Pants assembles the context for the Docker build itself, the doc here explains in more detail.

There are 2 way around this:

  1. use a pex_binary to package the python source and dependencies, and then copy the resulting PEX into the container. This will need some modification to your dockerfile (detailed on the same doc)
  2. use files/file targets to pull everything in as files (something like files(name="srcs",sources=["*.py","requirements.txt"]) `). This is generally not what Pants wants to do, since it won't benefit from dependency inference, dependency pruning, and so on.
HusseinLakkis01 commented 2 months ago

Okay so I was using python_sources to provide the files, which explains why it wasnt working. I will try the first option as it is what I am using pants for. Thank you for the quick reply.

HusseinLakkis01 commented 2 months ago

So python binaries work now but I have an issue setting up pants to work on self-hosted github runner; I get this error when I try to package:

BinaryNotFoundError: Cannot find docker on ['/__w/_temp/docker-actions-toolkit-t6W7SB/buildx-bin-standalone', '/bin', '/github/home/.local/bin', '/home/.pyenv/bin', '/home/.pyenv/shims', '/sbin', '/usr/bin', '/usr/local/bin', '/usr/local/sbin', '/usr/sbin']. Please ensure that it is installed so that Pants can interact with the docker daemon.

I have setup docker using the docker setup action; Is there a thing to configure so that pants can interact with docker?

lilatomic commented 1 month ago

I'm not sure, I didn't need to do anything for our selfhosted runner, but we don't use an action to setup docker. (you might need to set the DOCKER_HOST envvar if the action puts the socket in a nonstandard place) can you post your workflow file (or the relevant portion)? I'm not sure which action you're using (probably docker/setup-buildx-action@v3?). Can you also confirm that your workflow works without Pants? For example, can you build a normal docker container? where is the docker binary located when you do? The error seems to be that it can't find the docker binary. By default, it searches the PATH variable. I'm not sure if the action adds this to the PATH, or if your selfhosted env preserves this. you might try the executable_search_paths setting or setting the PATH for the action that runs Pants.