[英]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:
两个问题:
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 已经创建的容器:
docker ps -a
.docker ps -a
。 You should see last died containers something as: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 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. 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.