Remote Python Debug to Docker Container over Ssh by using PyCharm
A few months ago I was looking for how I can debug remotely using PyCharm; since debugging python code using vim is really tough and annoying. Also, I believed that if I can use PyCharm for remote debugging, it will make my life easier. As a result, I decided to make some researches.
After conducting quite an extensive research, I found a lot of sources about remote debug, dockerize an ssh service etc. However, I could not find a lot about them together. So, I would like to write this guide to help those who want to use it. Let’s start.
Part 1: Docker Container Creation
First of all, we assume that Docker is installed successfully. If you have not already installed Docker, you can follow the docs below.
Docker is available in two editions: Community Edition (CE) and Enterprise Edition (EE). Docker Community Edition (CE…
To check that docker is installed, we use the command below;
$ docker info
After assuring that docker is installed, we should first pull Ubuntu 16.04 Docker image. It will take time a bit.
$ docker pull ubuntu:16.04
After pulling the Ubuntu image, we will create a docker container by giving port parameter. This point is important. Because we will forward host port to container port.
In this example, we forward 8022 port from host to 22 port from the container.
# docker run –it –p 8022:22 ubuntu:16.04
You can also find other container creating parameters which you need below:
Docker run reference
Docker run reference Docker runs processes in isolated containers. A container is a process which runs on a host. The…
First step is completed by creating Docker container by forwarding port. Now, we are in Docker container.
Part 2: Ssh Server Configuration
Now, we need to setup ssh service. Let’s start. Firstly, we will update ubuntu repository. After that, we will install openssh-server.
$ apt update && apt install -y openssh-server
After installation is completed, we create a folder to configure openssh server.
$ mkdir /var/run/sshd
Now, let’s configure ssh server.
$ echo 'root:testssh' | chpasswd
# Root password was changed with testssh$ sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config$ sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd$ echo "export VISIBLE=now" >> /etc/profile
You can also find more about using sed command below:
GNU sed - GNU Sed: a stream editor - GNU Project - Free Software Foundation
The Free Software Foundation is the principal organizational sponsor of the GNU Operating System. Support GNU and the…
We completed configuring ssh server. Now, we have to restart ssh service to activate what is configured before.
$ service ssh restart
Now, We need to test where it was forwarded to 22 port from the host port.
$ docker port <your container name> 22
And when this command is run, you should see this output below.
Now, you can connect docker container you created. Let’s check.
$ ssh root@<your host ip> -p 8022
If you could connect to docker container directly over ssh, we would complete Docker installation and configuration part.
Part 3: PyCharm Deployment Configuration
Now we will configure PyCharm. Let’s start.
Now, we need to navigate Tools > Deployment > Configuration from the menu bar. After popup opens, we need to add new server by clicking + button top left. After giving name to server, we need to select SFTP as a server type.
After entering server and user info, we need to set Root Path to your remote project’s path that you want to debug.
Following that, you need to set local path, deployment path on server and web path on server on mappings tab in the same window.
Optionally, you can exclude the folders or files that you don’t want to sync on excluded paths tab.
Part 4: Remote Interpreter Configuration
Now we can go to the interpreter setup. Let’s start.
On File > Settings > Project > Project Interpreter page, we can show our Python Interpreter list that has already installed on our computer. Now, we need to add new remote interpreter.
By clicking setting icon on top-right of the page and selecting Add Remote option, we can add a new remote Python Interpreter.
Now, we need to select Deployment Configuration on the opened popup. It will take ~1 hour.
As a consequence, we completed all configurations and you can debug your Python projects remotely by using PyCharm.
It is definitely possible and even quite straightforward to debug your Python projects remotely as long as you use PyCharm and Docker.