Comment on page

Part 1 — Remote Developing with PyCharm [Docker SSH Server]

In this part, you will learn how to start developing using PyCharm and Docker.

Table of contents

Step 1 — Create a working directory

The first thing you need to do is to create a directory in which you can store docker and ssh related files.
As an example, we will create a directory named remote_dev inside our project and move into that directory with the command:
mkdir remote_dev && cd remote_dev

Step 2 — Create SSH key

On your client system – the one you’re using to connect to the server – you need to create a pair of key codes.
To generate a pair of SSH key codes, enter the command:
ssh-keygen -t rsa -b 4096 -f my_key
Files my_key and will be created in the working directory.
SSH Keys

Step 3 — Docker Image

Let's create all the files necessary for building the container.
1. Create Dockerfile
Let's create a simple image in which we will deploy the SSH server:
RUN apt-get update && apt-get install -y openssh-server
RUN apt-get install -y sudo
RUN mkdir -p /run/sshd
ARG home=/root
RUN mkdir $home/.ssh
COPY $home/.ssh/authorized_keys
RUN chown root:root $home/.ssh/authorized_keys && \
chmod 600 $home/.ssh/authorized_keys
RUN chmod 755 /
CMD ["/"]
ENTRYPOINT ["sh", "-c", "/"]
Add a script to start the server:
#!/bin/bash -l
echo $PATH
/usr/sbin/sshd -D
2. Create docker-compose
Since we need a GPU inside the container, we will take Image with pre-installed CUDA as a basis and set runtime to nvidia. For convenience, let's create a docker-compose file:
version: "2.2"
shm_size: '8gb'
runtime: nvidia
context: .
IMAGE: nvidia/cuda:11.1.1-devel-ubuntu18.04
- "1234:22"
- "./data:/data"
3. Build container
curl | sh \
&& sudo systemctl --now enable docker
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -s -L | sudo apt-key add - \
&& curl -s -L$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt update
sudo apt install -y nvidia-docker2
sudo systemctl restart docker
The basic syntax used to build an image using a docker-compose is:
docker-compose up --build -d
Build docker image
Once the image is successfully built, you can verify whether it is on the list of containers with the command:
docker ps | grep remote_dev_service

Step 4 — Connect to container over SSH

Add server with the ports specified in docker-compose.yml
~/.ssh/config (example)
Host docker_remote_container
HostName ip_of_your_docker_container
User root
Port 1234
IdentityFile path_to_ssh_secret_key
To connect to container by SSH, use command:
ssh docker_remote_container

Step 5 — Connect to container in PyCharm

1. Create new project

PyCharm create new project
PyCharm create new project

2. Add new interpreter

  • Open Preferences -> Python Interpreter
  • Show all
  • Plus button
PyCharm create new interpreter
PyCharm create new interpreter
3. Configure interpreter
PyCharm add python interpreter
PyCharm add python interpreter
4. Run simple code
PyCharm run code