简体   繁体   English

如何在设置正确的用户时将 VSCode 附加到远程 Docker 容器

[英]How to attach VSCode to a remote Docker container while setting the correct user

I start a Docker container with a special bash script that runs the container and then creates a user X with a dynamic name, UID and GUID in the container.我使用运行容器的特殊 bash 脚本启动 Docker 容器,然后在容器中创建具有动态名称、UID 和 GUID 的用户 X。 I can then bash into the container and perform actions as this user X. The script also creates an 'alias' user named vscode with the same UID as the earlier created dynamic user X.然后我可以将 bash 放入容器中,并以该用户 X 的身份执行操作。该脚本还创建了一个名为 vscode 的“别名”用户,其 UID 与之前创建的动态用户 X 相同。

In VSCode I can attach to this container.在 VSCode 中,我可以附加到这个容器。 Two questions:两个问题:

  1. How can I setup VSCode to perform all actions as the 'vscode' user or as the user X?如何设置 VSCode 以作为“vscode”用户或用户 X 执行所有操作? (When using devcontainer.json to create the container this is trivial, but now I attach to an existing container and devcontainer.json is not used). (当使用 devcontainer.json 创建容器时,这很简单,但现在我附加到现有容器并且 devcontainer.json 未使用)。
  2. In devcontainer.json you have the option to automatically install extensions.在 devcontainer.json 中,您可以选择自动安装扩展。 Which settings file do I need to create to automatically install extensions when attaching to a container?附加到容器时,我需要创建哪个设置文件来自动安装扩展?

The solution should be automated.解决方案应该是自动化的。 Eg.例如。 manual intervention and committing the image as suggested below is possible but will make it much harder for users to just use my Docker image.可以按照下面的建议进行手动干预和提交图像,但会使用户更难仅使用我的 Docker 图像。

I updated to vscode 1.39 and tried to add:我更新到 vscode 1.39 并尝试添加:

ADD server-env-setup /root/.vscode-server/server-env-setup

But "server-env-setup" seems to be only used for WSL.但是“server-env-setup”似乎只用于 WSL。

I'll answer your questions in reverted order:我将按相反的顺序回答您的问题:

VSCode installs extensions after creating the container by using docker exec command. VSCode 在使用docker exec命令创建容器后安装扩展。

And now recipe: The easiest way is to take container already created by VSCode:现在配方:最简单的方法是使用 VSCode 已经创建的容器:

  1. Run "Open folder on container" for creating dev container.运行“在容器上打开文件夹”以创建开发容器。
  2. After container has done and you can work with VSCode.容器完成后,您可以使用 VSCode。 Stop your environment by clicking "Close remote connection".通过单击“关闭远程连接”停止您的环境。
  3. Run docker ps -a .运行docker ps -a You should see last died containers something as:您应该看到最后死亡的容器如下: 最后运行的 docker 图像
  4. How you can see the latest running container is: a7aa5af7ec08 vsc-typescript-2ea9f347739c5397afc431028000c02b.如何查看最新运行的容器是:a7aa5af7ec08 vsc-typescript-2ea9f347739c5397afc431028000c02b。 This your container with all extensions installed.这是您安装了所有扩展的容器。 And it doesn't matter how you install extensions manually or by configuring via devcontainer.json.无论您如何手动安装扩展或通过 devcontainer.json 进行配置,都无关紧要。
  5. Run docker commit a7aa5af7ec08 all-installed-vscode-image:latest .运行docker commit a7aa5af7ec08 all-installed-vscode-image:latest Now you have a docker image with all your loved software installed.现在您有了一个 docker 映像,其中安装了您喜爱的所有软件。 You can upload this image to your favorite docker registry and use also on other machines.您可以将此图像上传到您最喜欢的 docker 注册表并在其他机器上使用。
  6. Now you can run docker run -i -u vscode all-installed-vscode-image:latest .现在您可以运行docker run -i -u vscode all-installed-vscode-image:latest And attach vscode to this container.并将 vscode 附加到这个容器。 This is an answer to your first question.这是您第一个问题的答案。

Also, you can review vscode documentation and use devcontainer.json configurations when you attach to already running containers and even containers running on remote machines .此外,当您附加到已经运行的容器甚至在远程机器上运行的容器时,您可以查看vscode 文档并使用 devcontainer.json 配置。

VSCode now implements a "remoteUser" property ehich you can set in the image configuration. VSCode 现在实现了一个“remoteUser”属性,您可以在图像配置中设置它。 This will ensure that VSCode logs into the container as the correct user.这将确保 VSCode 以正确的用户身份登录到容器中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 VSCode 中,“Python:远程连接”无法连接到正在运行的 Docker 容器 - In VSCode, 'Python: Remote Attach' fails to connect to a running Docker Container 如何从 vscode 远程容器查看 docker 日志? - How to view docker logs from vscode remote container? VSCODE如何在远程服务器docker容器的conda环境中调试python - VSCODE how to debug python in conda environment in docker container in remote server VSCode:如何通过远程服务器在 docker 容器中运行 Jupyter notebook? - VSCode: How to run a Jupyter notebook in a docker container, over a remote server? VScode docker - 无法附加到运行 Docker 容器的 bash - VScode docker - Can't attach to bash running the Docker container 在 VSCode 中连接到远程 docker 容器的依赖项? - Dependencies to connect to a remote docker container in VSCode? 如何在 vscode 上使用开发容器时查看 docker compose 日志 - How to view docker compose logs while using dev container on vscode 使用非 root 用户附加到 Visual Studio 代码中的远程 docker 容器 - attach to remote docker container in visual studio code with user other than root 如何使用 VSCode 的远程开发扩展在容器外的 Docker 容器中打开文件 - How to open files in a Docker container outside of container using the Remote Development extension for VSCode VSCode 远程容器 — 如何打开 VSCode 的容器实例中的默认文件夹? - VSCode Remote Container — how to open the default folder in the container instance of VSCode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM