user7659932
user7659932

Reputation: 169

How to preserve git modified files in docker dev container

I am using remote container extension in VSCode to work with my docker containers. While I am able to make a remote connection to my client container for example, the git history is showing most of the files as modified or deleted. How do I go about keeping git in-sync with my local project structure?

My local setup looks as the following,

- app
   - client
      - .devcontainer
      - Dockerfile
   - server
      - .devcontainer
      - Dockerfile
   - docker-compose.yml

My docker-compose.yml

version: '3'
services:
  server:
    build: ./server
    volumes:
      - ~/.ssh:/root/.ssh
      - ./server/src:/app/src
      - api-modules:/app/node_modules
  client:
    build: ./client
    depends_on:
      - server
    volumes:
      - ~/.ssh:/root/.ssh
      - ./.git:/app/.git
      - web-modules:/app/node_modules
      - ./client/src:/app/src
      - ./client/public:/app/public
volumes:
  api-modules:
  web-modules:

Client Dockerfile

FROM node:10

COPY . /app/

WORKDIR /app

ENTRYPOINT yarn start;

Upvotes: 2

Views: 1462

Answers (3)

Murillo Bastos
Murillo Bastos

Reputation: 46

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

version: '3'
services:
  server:
    build: ./server
    volumes:
      - ~/.ssh:/root/.ssh
      - ./server/src:/app/src
      - api-modules:/app/node_modules
      # Mount the root folder that contains .git
      - .:/workspace:cached
  client:
    build: ./client
    depends_on:
      - server
    volumes:
      - ~/.ssh:/root/.ssh
      - ./.git:/app/.git
      - web-modules:/app/node_modules
      - ./client/src:/app/src
      - ./client/public:/app/public
      # Mount the root folder that contains .git
      - .:/workspace:cached
volumes:
  api-modules:
  web-modules:

in /server/devcontainer.json you make something like this:

{
  "name": "Server Container",
  "dockerComposeFile": ["../docker-compose.yml"],
  "service": "server",
  "shutdownAction": "none",
  // Open the sub-folder with the source code
  "workspaceFolder": "/workspace/server"
}

in /client/devcontainer.json you make something like this:

{
  "name": "Client Container",
  "dockerComposeFile": ["../docker-compose.yml"],
  "service": "client",
  "shutdownAction": "none",
  // Open the sub-folder with the source code
  "workspaceFolder": "/workspace/client"
}

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:

  • (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS) and searching for Dev Containers: Rebuild Without Cache and Reopen in Container

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

Upvotes: 0

Zakaria Maaraki
Zakaria Maaraki

Reputation: 1

In you docker-compose file you can create and mount the volume client_path to /app

example:

  client:
    volumes:
    - <client_path>:/app

Upvotes: 0

Jay
Jay

Reputation: 3950

Let's see, currently...

  • ... you copy /app into the container (with same path) during container creation.
  • ... You mount ./.git as volume into /app/.git

That means /app is at the state your local directory was in when the container was created the last time.
While /app/.git is reflecting your current local state (because it's a mounted volume reading from your local ./.git).
That's why git in the container will see working directory differences as its contents are not in the state git expects them to be (based on git's history/state stored in .git).

Solutions:

Either remove the /app/.git-volume from docker-compose.yml and re-create the container whenever you changed the code (to update /app/).

Or mount ./ as volume to /app/ in the container (not just the subdir .git), instead of copying /app in the Dockerfile.

Upvotes: 1

Related Questions