I'm developing using VSCode's devcontainer, and I used pip with a requirements.txt
file for a few years with no problems. Works like charm.
I'd like to upgrade to using uv, but I'm encountering a problem. My Dockerfile
has the following lines:
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod -R 655 /install.sh && /install.sh && rm /install.sh
ENV PATH="/root/.local/bin:$PATH"
WORKDIR /app
COPY ./pyproject.toml .
RUN uv sync --dev
These commands install all the modules under /app/.venv
.
However, when I open the container via VSCode, my working directory is /workspaces/
, which is the mount directory, and I need to run uv sync --dev
again, to install the modules under /workspaces/<project>/.venv
.
I guess it worked well with pip because there's no .venv
there. So my question is: how can I avoid this duplication?
When VSCode launches a Dev Container, it bind-mounts your project into /workspaces/<project>
, and sets that as the working directory. However, your Dockerfile installs dependencies into /app/.venv
, which VSCode doesn't see.
To fix this, simply align your WORKDIR
in the Dockerfile with the directory VSCode uses. That way, uv
will create the .venv
right where VSCode expects it.
Something like this should work:
FROM python:3.12-slim
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod +x /install.sh && /install.sh && rm /install.sh
ENV PATH="/root/.local/bin:$PATH"
WORKDIR /workspaces/<project>
COPY pyproject.toml ./
RUN uv sync --dev