batshitlazy
batshitlazy

Reputation: 81

In Docker, sync lock file with pinned dependencies back to local machine

I would like to install some packages which are not available for my local machine on a Linux container and run it. I am using a Dockerfile with Python installed on a Linux (bullseye) machine. I have a pyproject.yml file with the unpinned dependencies. I am using uv as the dependency manager. This is how the initial part of the file looks like:

# Use a Python image with uv pre-installed
FROM python:3.11.9-slim-bullseye
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv

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

# 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

# Install the project's dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv lock

This uv.lock file that gets generated with the uv lock command is what I would like to be synced locally so that I can maintain it with version control. What would be the best approach for this? Since the dependencies are not available for my machine's architecture, I can't generate it locally. I realize this is probably an antipattern, and if others have cleaner solutions to the problem, I would be happy to hear the ideas too.

Upvotes: 0

Views: 302

Answers (1)

0xn0b174
0xn0b174

Reputation: 1022

change your RUN command in dockerfile to this:

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    --mount=type=bind,source=./local-lockfile-dir/uv.lock,target=/app/uv.lock \
    uv lock

mkdir -p locked_dependencies
touch locked_dependencies/uv.lock

you can use volume mount during running your image after build as follows:

docker run -v /path/to/local-lockfile-dir/uv.lock:/app/uv.lock your-image-name


Upvotes: 0

Related Questions