Skip to content

Usage in docker: `error: Project virtual environment directory /app/.venv cannot be used because it is not a compatible environment but cannot be recreated because it is not a virtual environment #9423

Description

@uxduck

Thanks for the detailed docker docs! I'm trying to use uv in docker compose for local development. Trying to bind mount my application code and an anonymous volume for app/.venv. This is similar to the approach shown in docs with docker run which aims to avoid overwriting host vs docker container venv files, and keep them separate.

Sadly, uv sync inside the container errors with:

error: Project virtual environment directory /app/.venv cannot be used because it is not a compatible environment but cannot be recreated because it is not a virtual environment

This might be because uv tries to replace the .venv folder, but it can't since it's a volume?

Docker compose settings

services:
  reproduce:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: "tail -f /dev/null"
    volumes:
      - ./:/app
      - /app/.venv

Details

  • version: uv 0.5.4
  • OS: debian 12

Reproducible setup

  • git clone git@github.com:ben-xD/reproduce-uv-docker-compose-error.git
  • docker-compose up -d
  • Enter shell: docker exec -it uv-reproduce-error-reproduce-1 bash
  • Run uv: uv sync
  • Observe error:
error: Project virtual environment directory `/app/.venv` cannot be used because it is not a compatible environment but cannot be recreated because it is not a virtual environment

Following documentation's docker run

I also tried following the docs, by running:

docker run -it --rm --volume .:/app --volume /app/.venv uv-reproduce-error-reproduce bash

Then running

uv sync

but i get the same error:

root@ea61fab74354:/app# uv sync
Using CPython 3.11.10
error: Project virtual environment directory `/app/.venv` cannot be used because it is not a compatible environment but cannot be recreated because it is not a virtual environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    great writeupA wonderful example of a quality contribution 💜

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions