简体   繁体   中英

Git repository not detected inside a container created by VS Code Remote Container extension

Describe The Issue

So I am playing around with VS Code Remote Container extension to setup my app development environment.

I was able to set it up ok and it is working great, However? it seems it can't detect the git repo inside the container?

So the reason, I think, why it didn't detect the git repo is the worktree in the git config is still pointing to my hosts machine path.

在此处输入图像描述

在此处输入图像描述

So is there anything I can do to make the worktree dynamically change to pointing now into the path inside the container? Been googling about this problem with no success.


Configuration

Below is my setup

在此处输入图像描述

devcontainer.json

{
  "name": "foobar-dev-env",
  "dockerComposeFile": "docker-compose.yml",
  "extensions": [
    // Git
    "github.vscode-pull-request-github",
    "eamodio.gitlens",
    "mhutchie.git-graph",

    // Code
    "coenraads.bracket-pair-colorizer-2",
    "aaron-bond.better-comments",
    "streetsidesoftware.code-spell-checker",
    "alefragnani.numbered-bookmarks",
    "pflannery.vscode-versionlens",
    "visualstudioexptteam.vscodeintellicode",
    "redhat.vscode-yaml", // YAML
    "kumar-harsh.graphql-for-vscode", // GraphQL

    // Prettier
    "esbenp.prettier-vscode",

    // Todo
    "gruntfuggly.todo-tree",
    "wayou.vscode-todo-highlight",

    // Theme
    "pkief.material-icon-theme",
    "zhuangtongfa.material-theme"
  ],
  "settings": {
    "workbench.colorTheme": "One Dark Pro",
    "workbench.iconTheme": "material-icon-theme",
    "workbench.sideBar.location": "right",

    "oneDarkPro.editorTheme": "Onedark Pro",
    "oneDarkPro.bold": true,
    "oneDarkPro.vivid": true,
    "oneDarkPro.italic": false,

    "editor.minimap.enabled": false,
    "editor.tabSize": 2,
    "editor.wordWrapColumn": 120,
    "editor.rulers": [120],
    "editor.formatOnSave": true,

    "[typescript, javascript]": {
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    "typescript.updateImportsOnFileMove.enabled": "always",
    "javascript.updateImportsOnFileMove.enabled": "always",

    "terminal.integrated.shell.linux": "/bin/bash"
  },
  "service": "app",
  "workspaceFolder": "/workspace",
  "shutdownAction": "stopCompose"
}

docker-compose.yml

version: "3.3"

services:
  app-db:
    image: postgres:12
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: app_db
      POSTGRES_PASSWORD: secret
    ports:
      - 54321:5432
    volumes:
      - app-db-data:/var/lib/postgresql/data

  app:
    image: node:12-stretch
    restart: always
    depends_on:
      - app-db
    command: /bin/sh -c "while sleep 1000; do :; done"
    ports:
      - 4000:4000
    volumes:
      # Mounts the project folder to '/workspace'. The target path inside the container
      # should match what your application expects. In this case, the compose file is
      # in a sub-folder, so we will mount '..'. You would then reference this path as the
      # 'workspaceFolder' in '.devcontainer/devcontainer.json' so VS Code starts here.
      - ..:/workspace:cached

volumes:
  app-db-data:

Thanks in advance.


Note

Reason why I used node:12-stretch on my image on docker-compose.yml file is if I use node:12-alpine, it does not have git installed with it, so now VS Code complains of not having git installed.

node:12-stretch image have git pre-installed in it

在此处输入图像描述

I do wish to use node:12-alpine tho if I can coz I want to mimic the prod env which this dev env is going to be deployed. Hope you guys can help me with that as well.

Cheers.


Env

  • Docker Desktop Version 2.3.0.3 (Using WSL 2 based engine)
  • Windows 10 Pro Version 2004

today i stumbled across the same problem in my setup:

  • Windows Docker Desktop with WSL2 Integration
  • VS Code Dev Container with Dockerfile

As you mentioned, there is an absolute win path in the git config file inside your repository, this causes the rep inside the container to fail.

My workaround was, to make this path relative, so the source control works now inside the docker container.

Here's my config:

.git/config

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    worktree = ../
    symlinks = false
    ignorecase = true

devcontainer.json

{
    "name": "Tensorflow GPU",
    "dockerFile": "Dockerfile",
    "settings": {
        "git.path": "/usr/bin/git"
    },
    "extensions": [
        "ms-python.python"
    ]
}

Dockerfile

FROM tensorflow/tensorflow:latest-gpu
RUN apt-get update && apt-get install -y git

PS: unfortunately with this method the local repository gets freaked out and wants you to commit all files after leaving the container

Simply remove the worktree property from your .git/config file.

Rebuild the container and git should be working fine. Check with git status .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM