dockerdockerfileuv

Docker: fail to resolve: RUN uv sync --frozen --no-cache


So, for context: I am trying to start working on a FastApi proj using UV framework, which is great. I am using Python and a Windows computer. Now, the app is still straightforward and runs well on local. When trying to build the image using Docker, however, an error pops up, saying:

=> ERROR [stage-0 5/5] RUN uv sync --frozen --no-cache                                                                                             0.4s 

[stage-0 5/5] RUN uv sync --frozen --no-cache:
0.330 /bin/sh: 1: uv: not found

Dockerfile:11

   9 |     # Install the application dependencies.
  10 |     WORKDIR /app
  11 | >>> RUN uv sync --frozen --no-cache
  12 |
  13 |     # Run the application.

ERROR: failed to solve: process "/bin/sh -c uv sync --frozen --no-cache" did not complete successfully: exit code: 127

My Dockerfile contains:

FROM python:3.11

# Install uv.
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /Scripts/

# Copy the application into the container.
COPY . /app

# Install the application dependencies.
WORKDIR /app
RUN uv sync --frozen --no-cache

# Run the application.
CMD ["/app/.venv/Scripts/uvicorn", "run", "src.main:app", "--port", "80", "--host", "0.0.0.0"]

I also have a screen shot of my Visual Studio Code setup but it has all of the same information that's in the text above.

I changed the original Dockerfile, because in UV documentation the Dockerfile looks this, but I guess the bin folder only applies to IOs, and for windows its called Scripts, so I changed lines 4 and 14 from /bin to /Scripts (I don't think that's the problem)

Can someone have any idea on what can be happening? I will appreciate any help you can provide me!


Solution

  • I changed the original Dockerfile, because in UV documentation the Dockerfile looks this, but I guess the bin folder only applies to IOs, and for windows its called Scripts, so I changed lines 4 and 14 from /bin to /Scripts (I don't think that's the problem)

    That is absolutely the problem.

    You're not running in a Windows container; you're running a Linux container. The default $PATH (the variable in which the system will search for unqualified binary names like uv) typically includes /bin, but it does not include /Scripts.

    By placing the uv binary in /Scripts, you have placed it where it won't be found.

    But the easiest solution is to not makes changes just for the sake of changing things: put the binaries in /bin as documented and they will work as expected.


    With the following Dockerfile, everything works as expected:

    FROM python:3.11
    
    # Install uv.
    COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
    
    # Copy the application into the container.
    COPY . /app
    
    # Install the application dependencies.
    WORKDIR /app
    RUN uv sync --frozen --no-cache
    
    # Run the application.
    CMD ["/app/.venv/bin/uvicorn", "src.main:app", "--port", "80", "--host", "0.0.0.0"]