VS Code on Container
Long story short, in my workplace I had to use VSCode on a Container, running inside a docker host, which is accessible only via a Jump Server (security measures). Looked up at many tutorials on the net to get this done, but more or less all of them involved installing docker on my window laptop. Tinkered around a few steps and made it work. Below given is the step by step instruction on how to do this, if you had a similar problem.
I had already setup passwordless login to the Jump server
and DockerHost
, there are already many articles on how to get this done, so I am going to skip that part.
So first, start the container on the docker host and setup SSH Server in the container.
#On Container:
#install ssh server
apt update && apt install openssh-server
#change root password
passwd
#Enable to login as root via ssh
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
#Start SSH Server
/etc/init.d/ssh start
Once done get the IP address of the container with the following command
#From Host
docker inspect -f "{{ .NetworkSettings.IPAddress }}" <DockerContainer_Name>
Check if the login works with the IP identified in the earlier step.
ssh <container_username>@<container_IP>
Next we need to install ‘Remote - SSH’ in our local system. This helps connecting your local VSCode with VSCode Server on the remote server.
Add necessary configurations in ssh_config
of ‘Remote - SSH’, so that you can login without password to the main server
Host JumpHost
HostName <JumpHost_IP>
User <JumpHost_username>
IdentityFile <Location_of_id_rsa_configured_for_passwordless_login>
Host DockerHost
HostName <DockerHost_IP>
User <DockerHost_Username>
ProxyCommand ssh.exe -q -W %h:%p Jump
IdentityFile <Location_of_id_rsa_configured_for_passwordless_login>
Host DockerContainer
HostName <DockerContainer_IP>
User <DockerContainer_Username>
ProxyCommand ssh.exe -q -W %h:%p DockerHost
Once the setup is completed, login to the docker container through VSCode.
If you are using Jupyter with VSCode ensure to Disable experiments to make sure the python connect works with .
#Edit in settings.json
{
"notebook.experimental.useMarkdownRenderer": false,
"terminal.integrated.experimentalLinkProvider": false,
"python.languageServer": "Pylance",
"terminal.integrated.shell.linux": "/bin/bash",
"python.defaultInterpreterPath": "/usr/bin/python3",
"python.experiments.enabled": false
}
Note: If re-connecting to a same IP address with a new container:
Edit path of "known_hosts" given in output window to remove entry for the IP address.