[Python] How can I solve this issue running a uv command when building a docker image

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am using uv in a python project to manage dependencies. I worte the following dockerfile based on some examples provided by the uv github repo.

    # Use a Python image with uv pre-installed
    FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

    # Install the project into `/app`
    WORKDIR /app

    COPY pyproject.toml uv.lock ./

    # Enable bytecode compilation
    ENV UV_COMPILE_BYTECODE=1

    # Copy from the cache instead of linking since it's a mounted volume
    ENV UV_LINK_MODE=copy

    # Print Python and uv version for debugging
    RUN python --version && which python && uv --version

    # Install dependencies
    RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --locked --no-install-project --no-dev


    # # Then, add the rest of the project source code and install it
    # # Installing separately from its dependencies allows optimal layer caching
    COPY . /app
    # RUN --mount=type=cache,target=/root/.cache/uv \
    # uv sync --locked --no-dev

    # Place executables in the environment at the front of the path
    ENV PATH="/app/.venv/bin:$PATH"

    # Reset the entrypoint, don't invoke `uv`
    ENTRYPOINT []

    # Run the python script using `uv run`
    CMD ["uv", "run", "services/trades/src/trades/main.py"]


    When I try to build the image using the docker build --no-cache -t trades:dev -f docker/trades.Dockerfile . command I get this error:

    5.509 error: The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`.
    ------
    trades.Dockerfile:19
    --------------------
    18 | # Install dependencies
    19 | >>> RUN --mount=type=cache,target=/root/.cache/uv \
    20 | >>> --mount=type=bind,source=uv.lock,target=uv.lock \
    21 | >>> --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    22 | >>> uv sync --locked --no-install-project --no-dev
    23 |
    --------------------
    ERROR: failed to solve: process "/bin/sh -c uv sync --locked --no-install-project --no-dev" did not complete successfully: exit code: 2
    make: *** [Makefile:5: build] Error 1


    Of course I tried with running uv lock but the issue was not resolved. I also checked the python version of the base image and compared with the python version of the local project, and they are the same.

    I deleted the --locked tag and the docker image was built without issues, so I extracted the uv.lock file from the relying container and compared it with the uv.lock file under the local project folder. Both files are absolutely the same.

    I still want to build the image using the --locked tag to guarantee a stable environment between the docker container and the local folder.

    Any suggestions please?

    Continue reading...

Compartilhe esta Página