34 Working in a container over ssh with Positron
Learning Objectives
- Explain how Positron works inside a running container over ssh
- Fix a missing ssh key and a changed host key
34.1 How it works
With the docker-uv-renv container running, you can work inside it from Positron, as if it were a remote Linux computer. Positron’s Remote SSH documentation has the steps. Here is how the pieces from the earlier chapters fit together:
- The image runs an ssh server when a container starts, because of its
CMD ["/usr/sbin/sshd", "-D"](see Python and R in one image). docker compose up --detachstarts the container in the background. Docker Compose maps port 2222 on your computer to the container’s port 22, and mounts your ssh public key as the key that may log in asroot(see Docker Compose, including how to make a key).- In Positron, run “Remote SSH: Show Remote Menu” from the Command Palette, pick “Connect to Host”, and type
root@localhost:2222. The first time, Positron installs its server inside the container, which takes a minute. - Open the folder
/workspace, which is your project folder. Positron’s terminals, and its Python and R sessions, now run in the container. - For Python, pick the interpreter
/opt/venv/bin/python, the environment the image built fromuv.lock. R uses the packages in/opt/renv/librarywithout any setup, because of theRENV_PATHS_LIBRARYline in R’sRenviron.site. - An ssh login does not see the image’s
ENVsettings, only the ones in/etc/environment. That is why theDockerfilealso writesUV_PROJECT_ENVIRONMENTthere, so thatuvin a Positron terminal uses/opt/venvtoo.
34.2 When it does not connect
These are the problems the docker-uv-renv README lists, and their fixes:
~/.ssh/id_ed25519.pubis a folder. Docker made it because the key did not exist when the container started. Rundocker compose down, delete the folder, make the key, and rundocker compose up --detachagain.Your key has a different name. Change the key’s path in
docker-compose.yml.Positron or
sshsays the remote host identification has changed. The container’s ssh server has its own keys, which the image made whenapt-getinstalledopenssh-server. Rebuilding that step makes new ones, so your computer no longer recognizes the container. Forget the old key, then connect again:Terminal
ssh-keygen -R "[localhost]:2222"On a Linux computer, the ssh server refuses the mounted key, because the file belongs to you, not to
root. Uncomment theStrictModes noline in theDockerfileand rundocker compose build.
34.3 An optional ssh shortcut
To type ssh docker-uv-renv instead of ssh -p 2222 root@localhost, and to pick docker-uv-renv from a list in Positron, add this to ~/.ssh/config on your computer:
Host docker-uv-renv
HostName localhost
Port 2222
User root
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
The last two lines skip the host key check, so that a rebuilt image does not cause the host key error above.