python-uv icon indicating copy to clipboard operation
python-uv copied to clipboard

Using a .devcontainer setup causes issues with volume mounts for .venv within the dev container.

Open tiefenthaler opened this issue 1 year ago • 5 comments

I tried to define a dev container setup with vscode for development. The purpose is to define a template to use uv to develop python projects (source code only) within a devcontainer with vs code alongside without using vs code. Therefore I want to define a dev container setup with a Docker.debug, docker-compose.yml and devcontainer.json file.

Issue: The "new (in the container)" create .venv to reflect the container os does neither exists in the defined (standard) root folder of the project when creating it in the config of the devcontainer.json via the "postCreateCommand": {"uv-sync--frozen--no-install-project": "uv sync --frozen --no-install-project"} nor when using the terminal within the running container uv sync --frozen --no-install-project. The .venv file is excluded during build by using .dockerignore. The mount should enable volume bind for the project code on the one side and a separate mount of the container .venv should persist the .venv within the container.

Find the setup in the git repo: https://github.com/tiefenthaler/dev-container-uv-datascience/tree/venv-mount-issue

FYI: The repo also contains the use of docker for production, which all works perfekt.

Would be great for any information how to solve this issue.

tiefenthaler avatar Jan 28 '25 23:01 tiefenthaler

Just needed to add sudo chown -R vscode:vscode /vscode/workspace/.venv to postCreateCommand and to postStartCommand in the devcontainer.json to solve the issue.

tiefenthaler avatar Jan 29 '25 01:01 tiefenthaler

@tiefenthaler Thank you for your issue!

Do you mean that you want to place the .venv file under /workspaces/*/? If so, you should be able to achieve this by removing UV_PROJECT_ENVIRONMENT from .devcontainer/devcontainer.json.

For more details, please refer to the official documentation: https://docs.astral.sh/uv/configuration/environment/#uv_project_environment

a5chin avatar Jan 29 '25 03:01 a5chin

@a5chin yes I want to place the .venv file in the directory /vscode/workspace/. The intension is to use the dir /vscode as it is the standard for vs code dev containers, and /workspace is an alias (can also be named different) that relates to the project. This ensures a standard directory structure. I did set up the devcontainer.json config "UV_PROJECT_ENVIRONMENT": "/home/vscode/workspace/.venv". Why would I need to remove this? Since it reflects the exact path where the virtual environment should be and ensures this venv is used by uv.

The only think I would change is to add those ENV configs to my docker-compose.yml so any interaction with uv (e.g. using any IDE or command line tool) has set those ENV configs if I want to use my setup with VS Code (.devcontainer.json).

tiefenthaler avatar Jan 29 '25 12:01 tiefenthaler

It is caused by a permission issue. Just needed to add sudo chown -R vscode:vscode /vscode/workspace/.venv to postCreateCommand and to postStartCommand in the devcontainer.json to solve the issue.

It is still strange that even without the permission the .venv was created. Since I could use it. Still the .venv folder was empty (probably due to permission restriction - ls -a in the terminal of the .venv folder). But then still could not be detected.

Also to mention that permission for the folder should be set with the config (as it was): "remoteUser": "vscode" since the .venv folder is within the vscode folder. But seems not to be the case when mounted.

@a5chin I did set up the devcontainer.json config "UV_PROJECT_ENVIRONMENT": "/home/vscode/workspace/.venv". Why would I need to remove this? Since it reflects the exact path where the virtual environment should be and ensures this venv is used by uv.

tiefenthaler avatar Jan 31 '25 10:01 tiefenthaler

@tiefenthaler I mentioned removing it for simplicity, but if you want to specify the exact path for the virtual environment, setting "UV_PROJECT_ENVIRONMENT": ".venv" should meet your requirements.

Like this:

{
    "name": "uv",
    "build": {
        "context": "..",
        "dockerfile": "Dockerfile",
        "args": {
            "UV_VERSION": "0.5.13",
            "DEBIAN_VERSION": "bookworm"
        }
    },
    "features": {
        "ghcr.io/dhoeric/features/hadolint:1": {}
    },
    "customizations": {
        "vscode": {
            "extensions": [
                "charliermarsh.ruff",
                "exiasr.hadolint",
                "mosapride.zenkaku",
                "ms-azuretools.vscode-docker",
                "ms-python.python",
                "redhat.vscode-yaml",
                "shardulm94.trailing-spaces",
                "yzhang.markdown-all-in-one"
            ]
        }
    },
    "containerEnv": {
        "DISPLAY": "dummy",
        "PYTHONUNBUFFERED": "True",
        "UV_LINK_MODE": "copy",
        "UV_PROJECT_ENVIRONMENT": ".venv"
    },
    "postCreateCommand": "uv sync --frozen",
    "postStartCommand": "uv run pre-commit install",
    "remoteUser": "vscode"
}

a5chin avatar Jan 31 '25 10:01 a5chin