microsoft / microsoft/vscode-remote-release

Export `LOCAL_WORKSPACE_FOLDER` env var to docker compose

Open
#6,844 5 comments 2 reactions 1 assignee View on GitHub

@chrmarti is already working on this.

Since Jun 20, 2022.

containers feature-request
Dominant language
Dockerfile
Stars
4.2k
Forks
470
Avg merge
1d 1h
Merged PRs (30d)
1

Description

On my devcontainers, I need to mount my workspace folder to the same location as they are in my host machine.

That's required because:

  1. Otherwise bind mounts when running docker run inside of the devcontainer won't work
  2. Otherwise docker-compose commands within the devcontainer won't work (because docker-compose identifies projects based on their folder names, and they would differ from host to inside the devcontainer - the latter usually is always workspace or so)

When I don't use docker-compose, the solution is simple:

// .devcontainer/devcontainer.json

{
  // Mounts the repository in the devcontainer in the same location of the repository in the host.
  // This is needed for running docker containers inside of the devcontainer when mounting volumes
  // inside of the repository, as docker in the devcontainer will talk to the docker daemon in the host.
  "workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind,consistency=cached",
  "workspaceFolder": "${localWorkspaceFolder}",
}

But when using docker-compose, the only solution I found was a lot trickier:

.devcontainer/initialize.sh

#!/bin/bash

set -euo pipefail

readonly wanted_line_key="LOCAL_WORKSPACE_FOLDER"
readonly wanted_line="${wanted_line_key}='${PWD}'"
readonly file=".env"

echo "Writing ${wanted_line} to ${file}" >&2
if [[ -f "${file}" ]] && grep --quiet "^${wanted_line_key}=" "${file}"; then
    sed --in-place "s,^${wanted_line_key}=.*,${wanted_line}," "${file}"
else
    echo "${wanted_line}" >>"${file}"
fi

.devcontainer/devcontainer.json

{
  "dockerComposeFile": "../docker-compose.yml",
  "service": "dev",
  "workspaceFolder": "${localWorkspaceFolder}",
  "shutdownAction": "stopCompose",
  "initializeCommand": ".devcontainer/initialize.sh"
}

docker-compose.yml

version: '3'
services:
  dev:
    user: node
    build: .devcontainer
    env_file:
      - .env
    volumes:
      - .:${LOCAL_WORKSPACE_FOLDER}:cached

The above could be greatly simplified if VS Code could export the LOCAL_WORKSPACE_FOLDER before running docker-compose commands so that I could simply refer it there.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.