microsoft / vscode-remote-release

Visual Studio Code Remote Development: Open any folder in WSL, in a Docker container, or on a remote machine using SSH and take advantage of VS Code's full feature set.
https://aka.ms/vscode-remote
Other
3.62k stars 281 forks source link

Allow mounting workspace in multiple containers (docker compose) #5945

Open GabrieleVolpato opened 2 years ago

GabrieleVolpato commented 2 years ago

Hello everyone, I'm working on a Symfony project which comes with a docker-compose file that looks like the following:

version: '3'
services:
  service-backend:
    build:
      context: .
      dockerfile: Dockerfile
    image: service:tag
    container_name: service-backend
    restart: unless-stopped
    tty: true
    working_dir: /var/www/html
    networks:
      - app-network
    volumes:
      - ./:/var/www/html
      - ./docker-configs/php/php.ini:/usr/local/etc/php/php.ini

  service-webserver:
    image: nginx:alpine
    container_name: service-webserver
    restart: unless-stopped
    tty: true
    ports:
      - 80:80
    networks:
      - app-network
    volumes:
      - ./:/var/www/html
      - ./docker-configs/nginx/conf.d/:/etc/nginx/conf.d/

  service-database:
    image: postgres
    container_name: service-database
    restart: unless-stopped
    tty: true
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: db
    networks:
      - app-network
    volumes:
      - ./db-datadir:/var/lib/postgresql/data

#Docker Networks
networks:
  app-network:
    driver: bridge

Since my development machine is a Windows machine and I'm using the WSL Docker backend, sharing files between Docker and the host machine is very slow. Developing inside my service container would completely solve the issue, but I'm finding quite a lot of problems sharing code between the service container and the webserver container.

My devcontainer.json:

{
    "dockerComposeFile": [
        "../docker-compose.yml",
        "./docker-compose.yml"
    ],
    "service": "service-backend",
    "workspaceFolder": "/var/www/html",
    "settings": {},
    "extensions": [
        "bmewburn.vscode-intelephense-client",
        "redhat.vscode-yaml",
        "yzhang.markdown-all-in-one",
        "felixfbecker.php-pack",
        "ikappas.phpcs",
        "junstyle.php-cs-fixer"
    ],
    "postCreateCommand": "apk add git",
}

My devcontainer docker-compose (used to override volumes mounts):

version: '3'
services:
  service-backend:
    init: true
    volumes:
      - service-volume:/var/www/html

  service-webserver:
    volumes:
      - service-volume:/var/www/html

  service-database:
    volumes:
      - db-volume:/var/lib/postgresql/data

volumes:
  db-volume:

service-volume is the name I chose for my volume when I run Clone Repository in Named Container Volume; as folder I used ./. When the extension tries to boot up the complete docker-compose file, it fails because of the /tmp/docker-compose.repositoryContainer.yml file which is created automatically and tries to mount another volume. This is its content:

services:
  'service-backend':
    labels:
      - vsch.local.repository=https://git.example.com/repo.git
      - vsch.local.repository.volume=service-volume
      - vsch.local.repository.folder=./
    volumes:
      - service-volume:/

volumes:
  service-volume:
    external: true

I couldn't find any way to stop this last file creation. If I give a folder name different from ./ when I clone the repository, this file gets created anyway but changes the volume mount point to /workspaces, even if I set /var/www/html inside the devcontainer.json file.

The solution would just to be able to define the workspaceFolder for multiple containers, and this should obviously work and not be ignored.

Thanks in advance to anyone who will help me!

GabrieleVolpato commented 2 years ago

I managed to get remote dev containers working by using the following override docker-compose.yml:

version: '3'
services:
  service-backend:
    init: true
    command: ["/bin/sh", "-c", "cp -R /workspaces/service/. /var/www/html && && php-fpm"]
    volumes:
      - code-volume:/var/www/html

  service-webserver:
    volumes:
      - code-volume:/var/www/html

  service-database:
    volumes:
      - db-volume:/var/lib/postgresql/data

volumes:
  code-volume:
  db-volume:

Basically I created another volume, mounted on both the backend and the webserver containers, where i copy the whole workspace into and I start developing from there. It's a workaround but it works in the meantime!

armando-fandango commented 1 year ago

is there a solution fro this please ?

chrmarti commented 1 year ago

Try using "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}" in the devcontainer.json and mount:

    volumes:
      - ../..:/workspaces:cached

in the dev container's service in the docker-compose.yml to get the same mount points as when cloning in a volume. If you need the workspace files available at another location in the dev container (like /var/www/html), you could use a symlink. The volumes for other services can be mounted as before (these are not overridden by the extension).

armando-fandango commented 1 year ago

It still doesn't work. When I run one container with ../..:/workspaces, another container with the same volume spec shows an error.

armando-fandango commented 1 year ago

what is this volume mount in error logs : /run/user/1000/wayland-0

chrmarti commented 1 year ago

It still doesn't work. When I run one container with ../..:/workspaces, another container with the same volume spec shows an error.

I see, you would want the other services to mount the same volume.

what is this volume mount in error logs : /run/user/1000/wayland-0

We automatically mount the Wayland socket when WAYLAND_DISPLAY is set locally (or in WSL under Windows).

artm commented 9 months ago

We automatically mount the Wayland socket when WAYLAND_DISPLAY is set locally (or in WSL under Windows).

can this behavior be disabled? I'm getting an error about it (https://github.com/microsoft/vscode-remote-release/issues/9293) and I'm not sure why would I want wayland inside my dev container, I only very vaguelly know what it is.