Search code examples
dockersshpycharmssh-tunnel

Pycharm - using remote Docker container as interpreter


I have a remote machine, with a running Docker container, and I want to set-up Pycharm to use the Python interpreter within the container. So, the situation is something like this:

    PyCharm
      ^
local_machine -->  remote_machine
                         |
                         V
                   docker_container   

On the local machine, I have a ~/.ssh/config file:

Host remote_machine
        HostName remote_machine_ip
        User user_name

Host docker_vie01_00
    HostName docker_container_ip_address
    User root
    ProxyJump remote_machine   

I can ssh to the docker container from my local machine using:

ssh docker_vie01_00

I'm asked for a password (I configured the Docker root password in the Dockerfile that I used to create the container), and I'm in the container.

Now I'm trying to configure Pycharm to do the same.
If I check the SSH Configurations section, everything seems to be working fine:

enter image description here enter image description here

However, using the same SSH configuration for the deployment fails:

enter image description here enter image description here

I tried creating the interpreter using the SSH server: enter image description here

Which seems to be successful, this is the output:

Successfully connected to root@docker_vie01_00:22

> pwd
/root
Command finished with exit code 0

Checking rsync connection...
/usr/bin/rsync -n -e "ssh -p 22 " root@docker_vie01_00:

[email protected]'s password: 

drwx------        4096 2023/05/04 15:12:02 .
-rw-------          15 2023/05/04 15:12:02 .bash_history
-rw-r--r--        3106 2019/12/05 15:39:21 .bashrc
-rw-r--r--         161 2019/12/05 15:39:21 .profile
drwxrwxr-x        4096 2023/04/17 11:26:00 .aws
drwxr-xr-x        4096 2023/04/17 17:00:43 .cache
drwxr-xr-x        4096 2023/04/17 17:00:37 .npm
drwxr-xr-x        4096 2023/05/04 15:10:10 work
Process finished with exit code 0

Starting introspection for Python...
> echo ${SHELL}
/bin/bash
Command finished with exit code 0
> /usr/bin/which python3
/usr/bin/python3
Command finished with exit code 0
> echo ${HOME}
/root
Command finished with exit code 0

Introspection completed

But then, Pycharm can't navigate the container folders to let me find the Python interpreter:

enter image description here

I also tried setting the interpreter path manually, but then it seems that nothing is loaded: enter image description here

If I try to run the code, I get this error window: enter image description here


Solution

  • I was told on the PyCharm forums that this is likely to be a bug, so not much to do at the moment. However, if you use PyCharm Pro, there's a new Beta functionality which allows you to SSH into remote hosts...it's the "Remote Development → SSH" option, accessible through the "Welcome to PyCharm" window

    enter image description here

    Selecting New Project, you can select between the possible SSH configurations available in PyCharm (you can set them up through Setting → Tools → SSH Configurations). The whole thing is actually much more similar to how VS Code works. It downloads some helpers and open a remote PyCharm instance directly on the remote machine, and you can work there. I tested it, and it works in remote docker containers as well.

    Another alternative is to use VS Code, I kinda like how this remote deployment is implemented there.