Skip to content
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

Docker Compose: Add Git safe directory when needed #7923

Closed
chrmarti opened this issue Jan 27, 2023 · 27 comments
Closed

Docker Compose: Add Git safe directory when needed #7923

chrmarti opened this issue Jan 27, 2023 · 27 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug containers Issue in vscode-remote containers info-needed Issue requires more information from poster

Comments

@chrmarti
Copy link
Contributor

I'm adding a fix to the single container case (Docker Compose case needs more investigation) where the repository root folder is added as a safe directory in the container if the current user in the container is not the same as the owner of the repository root folder and on the local machine the current user and the owner of the repository root folder are the same.

This is available in Dev Containers 0.274.0-pre-release. Will open a new issue to investigate adding this for Docker Compose setups too.

Originally posted by @chrmarti in #7628 (comment)

@chrmarti chrmarti self-assigned this Jan 27, 2023
@chrmarti chrmarti added bug Issue identified by VS Code Team member as probable bug containers Issue in vscode-remote containers labels Jan 27, 2023
gzurowski added a commit to ResearchHub/researchhub-backend that referenced this issue Mar 15, 2024
@markedwards
Copy link

Any rough idea of timeline on this? This doesn't always happen, and it's difficult to understand what triggers it, but when it happens it doesn't go away.

The "fix" using postStartCommand is pretty annoying because it insists on popping up a notification on every single rebuild.

@markedwards
Copy link

❤️

@chrmarti
Copy link
Contributor Author

Could you give Dev Containers 0.373.0-pre-release a try and let me know if that works? That fixes the Docker Compose case and also covers cases where the Git root folder is between the workspace folder and the mount point.

@andreamah
Copy link

@markedwards have you had a chance to test using the pre-release? Thanks!

@andreamah andreamah added the author-verification-requested Issues potentially verifiable by issue author label Jun 26, 2024
@markedwards
Copy link

@markedwards have you had a chance to test using the pre-release? Thanks!

I haven't, and the problem is I haven't experienced this issue in quite some time. I have no idea how to reproduce it consistently.

@andreamah
Copy link

@chrmarti is there an alternative way to verify this bug?

@pamelafox
Copy link
Member

I did test with the pre-release, and I did get the error in the middle of the session. I haven't seen it again yet at the beginning of a session (ie. right after Dev Container is built).

@andreamah
Copy link

Hmm. @chrmarti I will reopen this bug so that you can investigate further.

@andreamah andreamah reopened this Jun 26, 2024
@andreamah andreamah added verification-found Issue verification failed author-verification-requested Issues potentially verifiable by issue author and removed author-verification-requested Issues potentially verifiable by issue author labels Jun 26, 2024
@andreamah
Copy link

Thanks, @pamelafox!

@chrmarti chrmarti removed verification-found Issue verification failed author-verification-requested Issues potentially verifiable by issue author labels Jun 27, 2024
@chrmarti chrmarti removed this from the June 2024 milestone Jun 27, 2024
@chrmarti
Copy link
Contributor Author

@pamelafox If it is only in the middle of a session I wonder what might have changed at that point. Could you check the ownership of the Git root folder when this occurs? Is this with the repository on a local folder or in a Docker volume?

@chrmarti chrmarti added the info-needed Issue requires more information from poster label Jun 27, 2024
@pamelafox
Copy link
Member

pamelafox commented Jun 27, 2024

Here's a screenshot of it happening- this is inside the Dev Container from https://github.com/Azure-Samples/rag-postgres-openai-python/tree/main/.devcontainer

Screenshot 2024-06-27 at 12 45 39 AM

Here's what it says for ownership of .git inside workspace:

vscode ➜ /workspace $ ls -ld .git
drwxr-xr-x 16 vscode vscode 512 Jun 27 07:44 .git

@pamelafox
Copy link
Member

Here's what it says run I check ownership from my local Terminal:

rag-postgres-openai-python % ls -ld .git
drwxr-xr-x  16 pamelafox  staff  512 Jun 27 00:47 .git

@chrmarti
Copy link
Contributor Author

@pamelafox What ownership does the /workspace folder have?

@pamelafox
Copy link
Member

vscode ➜ /workspace $ ls -ld ../workspace/
drwxr-xr-x 24 vscode vscode 768 Jun 24 13:30 ../workspace/

@chrmarti
Copy link
Contributor Author

That doesn't match my previous understanding of the error message. I thought that would only show when the ownership/permissions are such that someone else might have manipulated the repository, but that does not appear to be the case here. 🤔

@pamelafox
Copy link
Member

I just had the git issue with a new Dev Container rebuild.
Here are results right after the dev container starts:

vscode ➜ /workspace $ ls -ld .git
drwxr-xr-x 15 vscode vscode 480 Jul 1 23:12 .git

vscode ➜ /workspace $ ls -ld ../workspace/
drwxr-xr-x 27 root root 864 Jun 11 20:39 ../workspace/

@chrmarti
Copy link
Contributor Author

chrmarti commented Jul 2, 2024

@pamelafox Do you know why /workspace is owned by root? Docker Desktop would normally set ownership of mounted local folders to the user in the container. Are you on Windows working with a local Docker Desktop install?

@pamelafox
Copy link
Member

I am on a Mac M1 with local Docker Desktop. I don't know why, no. I could jump on a Teams call so you could poke at my system, if that'd help. I actually just got it a few minutes ago with another repo after container rebuild, so it might be reproducible in a call. Or let me know what other logs/commands to run.

@nikostro
Copy link

nikostro commented Jul 5, 2024

For what it's worth, I have the same issue on Mac M1 with local Docker Desktop.

@anz-ableton
Copy link

anz-ableton commented Jul 11, 2024

I ran into this issue a couple times as well, with a simple devcontainer setup that is not using docker-compose.
Sometimes /workspace/$project is owned by root, sometimes not. It does not matter what USER is set to in the Dockerfile, nor which value containerUser in devcontainer.json is set to.

My current workaround is to prepare the workspace folder in my devcontainer Dockerfile like this: This workaround is not reliable.

RUN install --owner vscode --group vscode --directory /workspaces/app

You can find my VSCode and Docker version below:

macOS Sonoma 14.5 (23F79)

Docker Desktop 4.32.0 (157355)

$ docker --version
Docker version 26.1.4-1, build 5650f9b10226d75e8e9a490a31cc3e5b846e0034

VSCode:

Version: 1.91.0 (Universal)
Commit: ea1445cc7016315d0f5728f8e8b12a45dc0a7286
Date: 2024-07-01T18:54:18.541Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Darwin arm64 23.5.0

@nikostro
Copy link

nikostro commented Jul 30, 2024

My workaround, which seems to be reliable, is:

  1. Map $HOME/.gitconfig:/home/vscode/.gitconfig in the devcontainer docker-compose, so that the host .gitconfig is available in the devcontainer (I was doing this anyway, you may be too.)
  2. To your host .gitconfig, add
[safe]
    directory = /workspace

or whatever your workspace directory is that you're having issues with.

  1. Rebuild devcontainer.

@skwde
Copy link

skwde commented Aug 24, 2024

Today I also encountered this issue for the first time.

I am using a single container (no docker compose).
In my Dockerfile I add user and in my devcontainer.json I set "remoteUser": "user",.

So the fix mentioned by @chrmarti seems not to work in this case.

Possible workarounds (from within the container):

  • run git config --global --add safe.directory $(pwd)
  • change the ownership of the repo folder sudo chown $(whoami):$(whoami) $(pwd)

@duncan-oxd
Copy link

@nikostro the .gitconfig file is automatically copied into the devcontainer, so there's no need to copy/mount it manually.

Workarounds suggest using postStartCommand or postAttachCommand to run that git config command inside the container, but your idea of running it on the host machine means the config is already set when the file gets copied in. 💡
This neatly avoids the notice that you'd need to dismiss every time you open a dev container.

I just tried it on my machine*

# On my host machine
# Note: there is no /app in my filesystem -
# it's the project root (directory with .git) in my dev container
git config --global --add safe.directory /app

That seems like the cleanest solution, as long as you're comfortable with a non-existent directory marked as safe on your host machine. 👍


* My machine is also an M1 Mac with Docker Desktop and a docker-compose project as others have reported.
Inside the container, the workspace directory (app/backend/ or app/frontend/) is owned by node but app/ is owned by root 🤷

@chrmarti
Copy link
Contributor Author

chrmarti commented Sep 2, 2024

Related and maybe fixed by it: #8656

Copy link

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

@vs-code-engineering vs-code-engineering bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2024
@chrmarti
Copy link
Contributor Author

The fix for #8656 is in Dev Containers 0.385.0-pre-release. Could you give that a try and let me know if that fixes it?

@chrmarti chrmarti reopened this Sep 10, 2024
Copy link

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

@vs-code-engineering vs-code-engineering bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2024
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug containers Issue in vscode-remote containers info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

8 participants