Search code examples
gitdockerwindowvolumes

Mapping a windows git directory into a linux docker container


I want to map a folder on a windows machine which contains code from a git repository into a linux docker container. I then connect via vscode and ssh to that container and open the mapped folder.

The issue is: when the folder is opened, vscode shows all text based files as "modified", Even though those files do not have any pending changes on the host file system.

My docker-compose looks like this:

version: "3.9"
services:
  srv:
    image: image:latest
    restart: always
    ports:
      - "22:22"
    volumes:
      - ./:/workdir  

My expected behavior would be to see no difference in the "modified" state of the files between host and container.

I made sure that all files have a "LF" line ending. I even tried to run dos2unix on all files from that repository from within the container but nothing changed.

I guess I have to try to check out the code directly into the container. However I am still curious why this happens.

I would be greatful if someone could explain this to me.


Solution

  • I was facing a similar problem, here https://code.visualstudio.com/remote/advancedcontainers/connect-multiple-containers I found out that you have to put the "cached" param in your docker-compose.yml file, like this:

    version: "3.9"
    services:
      srv:
        image: image:latest
        restart: always
        ports:
          - "22:22"
        volumes:
          # Mount the root folder that contains .git
          - .:/workdir:cached
    

    and to solve the many modified files I put this in my .gitignore file:

     * text=auto eol=lf
    *.{cmd,[cC][mM][dD]} text eol=crlf
    *.{bat,[bB][aA][tT]} text eol=crlf
    

    and run git config --global core.autocrlf input on the host computer (not inside the docker container) make sure you rebuild the container:

    • on Dev Containers: (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS) and searching for "Dev Containers: Rebuild Without Cache and Reopen in Container"
    • on Docker: docker-compose build --no-cache backend

    here is a full explanation https://code.visualstudio.com/docs/remote/troubleshooting#_resolving-git-line-ending-issues-in-wsl-resulting-in-many-modified-files