Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tiefenthaler opened this issue Jan 28, 2025 · 8 comments

Comments

@tiefenthaler
Copy link

tiefenthaler commented Jan 28, 2025

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 layout) 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.

@zanieb
Copy link
Member

zanieb commented Jan 28, 2025

I'm sorry I don't entirely follow the question.

Did you read the documentation at https://docs.astral.sh/uv/guides/integration/docker/#developing-in-a-container

@tiefenthaler
Copy link
Author

tiefenthaler commented Jan 28, 2025

Thank you for the quick reply. Yes, I read the documentation. The issue is, that when creating the .venv in the container as described above respectively using uv venv the virtual environment is not being created in the root folder of the project in the container. Therefore it is not being detected. Even though it is mounted to the target "/vscode/${WORKSPACE_NAME}/.venv" with WORKSPACE_NAME=workspace where workspace reflects the project root dir in the container which relates to the project root folder of the host.
I prefer a volume mount over watch since this guarantees file sync both ways, which is preferred in my case.

@zanieb
Copy link
Member

zanieb commented Jan 29, 2025

A sync both ways won't always work, e.g., the virtual environment references the Python interpreter and if it's not in the exact same place the virtual environment will broken.

The issue is, that when creating the .venv in the container as described above respectively using uv venv the virtual environment is not being created in the root folder of the project in the container. Therefore it is not being detected. Even though it is mounted to the target "/vscode/${WORKSPACE_NAME}/.venv" with

You can use uv venv <path> or uv sync will create a virtual environment in the project root (the top-most directory with a pyproject.toml).

How are you confirming the virtual environment is not present? How are you testing detection of it; with VS Code?

@tiefenthaler
Copy link
Author

tiefenthaler commented Jan 29, 2025

@zanieb thanks for the questions. It does not matter which way I use to create the venv in the container (uv sync, uv venv, ...) in the terminal respectively in the devcontainer.json, of course always in the root of the project where the pyproject.toml is.

How are you confirming the virtual environment is not present?

  • The .venv folder in the root of the project dir does exist but it is empty.
  • Execution of the command succeed. And provides activation command source home/vscode/workspace/.venv/bin/activate, which can be activated. But the folder is still empty.

How are you testing detection of it; with VS Code?

  • Yes, VS Code does not detect the .venv. Even tough configs are set.
  • As above, the .venv can be activated by source home/vscode/workspace/.venv/bin/activate and also using uv commands. Which is strange, since the folder remains emtpy.
  • But it should be properly detected to use the setup for development.

Trying your suggestion to explicit set the default path using uv venv <path>with uv venv .venv just raised a permission error. This might cause the issue. I will give an update soon.

@tiefenthaler
Copy link
Author

tiefenthaler commented Jan 29, 2025

@zanieb thank you again for the hint. 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.

@zanieb
Copy link
Member

zanieb commented Jan 29, 2025

Interesting, I'm glad you figured it out!

I'm happy to add something to the uv documentation about the required permissions, but might need a simple example to understand it fully.

@tiefenthaler
Copy link
Author

Let me see if I can provide a simple example. I will keep you updated

@tiefenthaler
Copy link
Author

@zanieb I just created a simple example to reproduce the error.

I am not a 100% sure what causes the error. But it seems that anonymous mounts for the .venv causes permission issues.
The solution I mentioned earlier using sudo chown -R vscode:vscode /vscode/workspace/.venv as a pre-command before uv sync --frozen --no-install-project in the .devcontainer.json is not the solution.
The solution that fixes the error is to properly provide a volume at the dev container for the .venv instead of using an anonymous mount. See the file docker-compose.yml file replacing volumes: - /vscode/${WORKSPACE_NAME}/.venv with:

service:
  myproject:
    volumes:
            - type: volume
            source: venv
            target: /vscode/${WORKSPACE_NAME}/.venv

volumes:
  venv: # Volume for the virtual environment for persistent in the container.

Find the related simple example here: https://github.com/tiefenthaler/dev-container-uv-datascience/tree/venv-mount-issue-example
or more specific in the .devcontainer folder: https://github.com/tiefenthaler/dev-container-uv-datascience/tree/venv-mount-issue-example/.devcontainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants