How can I bind mount to a vscode devcontainer without hard-coding it in devcontainer.json

3.1k Views Asked by At

In this question it is recommended that .devcontainer.json should be committed.

I agree, but for one problem. I need to bind mount a local directory into my devcontainer. That requires a local path that my colleagues won't have. The purpose is to mount my GCP credentials into my devcontainer for use with the gcloud CLI.

My .devcontainer.json looks like:

{
  "image": "some/image",
  // ... settings, extensions, forwardPorts, postCreateCommand, remoteUser, runArgs ...

  // Mount gcloud configuration directory
  "mounts": [
    "source=/Users/myname/.config/gcloud,target=/gcp/config,type=bind,consistency=cached"
  ]
}

This works, but if I commit this it'll break everybody else's setups.

How can I create a bind mount to a vscode devcontainer that's defined locally by me, without adding an entry to .devcontainer.json?

WORKAROUNDS

One workaround is to turn this into a docker-compose setup then define a docker-compose.developer-overrides.yml file which doesn't get committed, but is required. But that seems like the wrong solution and there should be a settings override somewhere that I've missed.

Another is to mount the user's home directory (the vscode user guide shows how to do that for both windows and linux users using: "source=${localEnv:HOME}${localEnv:USERPROFILE},target=/host-home-folder,type=bind,consistency=cached" ...but then all users have to have their gcloud config installed in the same location in their local machine; I'm not sure if this is possible.

1

There are 1 best solutions below

0
On

I keep my configs within Docker volumes. There is an example:

devcontainer.json file

{
  "initializeCommand": ["init-host.sh"],
  "mounts": [
    "type=volume,source=devvolume-gcloud,target=/root/.config/gcloud"
  ]
}

init-host.sh file

#!/usr/bin/env sh
set -e

docker context use default
docker volume create devvolume-gcloud