简体   繁体   中英

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. 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.

In VSCode I can attach to this container. Two questions:

  1. How can I setup VSCode to perform all actions as the 'vscode' user or as the user 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).
  2. In devcontainer.json you have the option to automatically install extensions. 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.

I updated to vscode 1.39 and tried to add:

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

But "server-env-setup" seems to be only used for WSL.

I'll answer your questions in reverted order:

VSCode installs extensions after creating the container by using docker exec command.

And now recipe: The easiest way is to take container already created by VSCode:

  1. Run "Open folder on container" for creating dev container.
  2. After container has done and you can work with VSCode. Stop your environment by clicking "Close remote connection".
  3. Run 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. This your container with all extensions installed. And it doesn't matter how you install extensions manually or by configuring via devcontainer.json.
  5. Run docker commit a7aa5af7ec08 all-installed-vscode-image:latest . Now you have a docker image with all your loved software installed. You can upload this image to your favorite docker registry and use also on other machines.
  6. Now you can run docker run -i -u vscode all-installed-vscode-image:latest . And attach vscode to this container. 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 now implements a "remoteUser" property ehich you can set in the image configuration. This will ensure that VSCode logs into the container as the correct user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM