Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Setup PyCharm IDE to remote debug code inside a Docker container using ssh
# Should work for any IDE that supports ssh but only tested on PyCharm
# These instructions are for systemd OS (Ubuntu 16.04 and up, Debian).
# For systems running upstart like 14.04, modify /etc/default/docker by adding DOCKER_OPTS
## First, we need to get Docker to accept ssh connections:
## Open /lib/systemd/system/docker.service
## Add EnviromentFile + add "$DOCKER_OPTS" at end of ExecStart
## DOCKER_OPTS="-H tcp:// -H unix:///var/run/docker.sock"
## After change execute "$ systemctl daemon-reload"
## may need to reboot
## Notes: ## port 2376 is traditionally used for encrypted comm, 2375 unsecured
## Example:
ExecStart=/usr/bin/dockerd -H fd:// -H tcp:// -H unix:///var/run/docker.sock
## create and run (replace paths, tags and username with whatever):
docker build -t dainis/ssh-debug -f Docker-ssh-rdebug .
docker run -ti -d -v /home/dainis/workspace/test/outputs:/outputs \
-v /home/dainis/workspace/test/inputs:/inputs \
-p 22222:22 --name ssh-debug dainis/ssh-debug
# login to look around (enter password as prompted)
ssh root@localhost -p 22222
# Remote debug enabled container. IDE and code on host/network, environment
# isolated in Docker. Tested with PyCharm 17.2 Professional Edition.
FROM tensorflow/tensorflow:latest-devel-gpu-py3
# ssh will mess up your paths so save to profile
# if you try to export them from outside gotta use a script
RUN echo "export PATH=$PATH" >> /etc/profile && \
echo "ldconfig" >> /etc/profile
# SSH support
RUN apt-get update && apt-get install -y openssh-server && apt-get install sudo
RUN mkdir /var/run/sshd
# set root password to root - change this to whatver
RUN echo 'root:root' | chpasswd
# configure sshd to let root login
RUN sed -ri 's/^PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
# expose SSH port
RUN service ssh restart
# this is where we will mount a directory to get inputs/outputs without having to ssh every time
RUN mkdir /root/inputs
RUN mkdir /root/outputs
# this is where our projects go
RUN mkdir /root/workspace
# set default directory we enter on logon to home directory
CMD ["/usr/sbin/sshd", "-D"]
In PyCharm, select project settings > interpreter > set to remote and set authentication to ssh
Enter the login details
To execute your code on remote machine you'll have to perform few steps
Define a remote interpreter for your project
Go to File -> Settings -> Project: {project_name} -> Project Interpreter.
Click on cog icon and select Add Remote.
Add your SSH host credentials and interpreter path (on remote machine).
As a result, you should see new position in project interpreter dropdown selector, spelled like Python Version (ssh://login@host:port/path/to/interpreter). Package list should be populated with records.
Define deployment settings
Go to File -> Settings -> Build, Execution, Deployment -> Deployment
Create new deployment settings and fill ssh host configuration
Type: SFTP
SFTP host: same as interpreter host
Root path: path where files will be uploaded
Click on button "Test SFTP connection" to check if provided data are correct.
Go to mappings and configure mapping between local path and deployment path.
Deployment path is relative to root path - / is equivalent to /my/root/path, /dir to /my/root/path/dir etc.
Deploy your code
Select Tools -> Deployment -> Upload to {deployment settings name}
Upload process will be started in background. Wait for upload to complete.
Go and edit your project Run configurations. You can also click on file you want to run and select "Run".
Code should run in the container.You can set up break points etc, then configure debug to use remote interpreter
in a similar manner as you just configured run.
Now you can setup any environment in 2 minutes and debug in it like on your own machine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.