Search code examples
dockervisual-studio-codevscode-remote

How to make VSCode run custom script when attaching to a running remote container


I have a running Docker container and would like to use the VSCode remote container plugin to attach to it.

  1. Is it possible to have VSCode run a script when it attaches? Some custom actions are required to setup the container. These actions cannot be baked into the Dockerfile/Image.

  2. Is it possible to configure the Docker exec arguments when attaching to a running container. (This is possible for Docker Run using .devcontainer when creating new containers, but I haven't found anything about Docker exec regarding already running containers).


Solution

  • There is a "postAttachCommand" that lets you execute a custom command after the vscode attached to the running container.

    However my preference would be to use a login shell, for that there is an undocumented property called

    "userEnvProbe": "loginInteractiveShell"

    Below github issue explains this parameter (This is where i learnt about the parameter as well) :

    https://github.com/microsoft/vscode-remote-release/issues/3585

    The userEnvProbe and postAttachCommand is per docker container, you have to add them to "Container Configuration File", hover your mouse on the tip of the red arrow and you will see a settings icon, when you press it you can access to the "Container Configuration File"

    enter image description here

    For further customization there is a great github page that explains what else you can do to further customize the way you execute docker commands as well

    https://github.com/microsoft/vscode-docker/issues/1596