如何将挂载绑定到 vscode devcontainer 而无需在 devcontainer.json 中对其进行硬编码

发布于 2025-01-13 05:06:24 字数 1261 浏览 4 评论 0原文

这个问题< /a> 建议提交.devcontainer.json

我同意,但有一个问题。我需要将本地目录绑定挂载到我的 devcontainer 中。这需要我的同事没有的本地路径。目的是将我的 GCP 凭证挂载到我的 devcontainer 中,以便与 gcloud CLI 一起使用。

我的 .devcontainer.json 看起来像:

{
  "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"
  ]
}

这可行,但如果我提交它,它会破坏其他人的设置。

如何创建到我本地定义的 vscode devcontainer 的绑定挂载,而不向 .devcontainer.json 添加条目?

解决方法

一种解决方法是将其转换为docker-compose 设置然后定义一个 docker-compose.developer-overrides.yml 文件,该文件不会被提交,但是是必需的。但这似乎是错误的解决方案,应该有一个我错过的设置覆盖。

另一种方法是挂载用户的主目录(vscode 用户指南显示了如何执行对于 Windows 和 Linux 用户,使用: “source=${localEnv:HOME}${localEnv:USERPROFILE},target=/host-home-folder,type=bind,consistency=cached” ...但是所有用户都必须将其 gcloud 配置安装在本地计算机的同一位置;我不确定这是否可能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-20 05:06:24

我将配置保存在 Docker 卷中。有一个示例:

devcontainer.json 文件

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

init-host.sh 文件

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

docker context use default
docker volume create devvolume-gcloud

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文