Skip to content

Instantly share code, notes, and snippets.

@alvarocavalcanti
Last active July 11, 2024 17:33
Show Gist options
  • Save alvarocavalcanti/24a6f1470d1db724a398ea6204384f00 to your computer and use it in GitHub Desktop.
Save alvarocavalcanti/24a6f1470d1db724a398ea6204384f00 to your computer and use it in GitHub Desktop.
Configuring Python Remote Interpreter using Docker
version: '3.3'
services:
dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 127.0.0.1:9922:22
volumes:
- .:/code/
environment:
DEV: 'True'
env_file: local.env
FROM python:3.7
ENV PYTHONUNBUFFERED 1
WORKDIR /code
# Copying the requirements, this is needed because at this point the volume isn't mounted yet
COPY requirements.txt /code/
# Installing requirements, if you don't use this, you should.
# More info: https://pip.pypa.io/en/stable/user_guide/
RUN pip install -r requirements.txt
# Similar to the above, but with just the development-specific requirements
COPY requirements-dev.txt /code/
RUN pip install -r requirements-dev.txt
# Setup SSH with secure root login
RUN apt-get update \
&& apt-get install -y openssh-server netcat \
&& mkdir /var/run/sshd \
&& echo 'root:password' | chpasswd \
&& sed -i 's/\#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

Remote Interpreter using Docker

Pre-requisites

A running Docker container with:

  • A volume mounted to your source code (henceforth, /code)
  • SSH setup
  • SSH enabled for the root:password creds and the root user allowed to login

Refer to docker-compose.yaml and Dockerfile.dev on how to achieve this.

PyCharm Professional Edition

  1. Preferences (CMD + ,) > Project Settings > Project Interpreter
  2. Click on the gear icon next to the "Project Interpreter" dropdown > Add
  3. Select "SSH Interpreter" > Host: localhost, Port: 9922, Username: root > Password: password > Interpreter: /usr/local/bin/python, Sync folders: Project Root -> /code, Disable "Automatically upload..."
  4. Confirm the changes and wait for PyCharm to update the indexes

Visual Studio Code

  1. Install the Python extension
  2. Install the Remote - Containers extension
  3. Open the Command Pallette and type Remote-Containers, then select the Attach to Running Container... and selecet the running docker container
  4. VS Code will restart and reload
  5. On the Explorer sidebar, click the open a folder button and then enter /code (this will be loaded from the remote container)
  6. On the Extensions sidebar, select the Python extension and install it on the container
  7. When prompet on which interppreter to use, select /usr/local/bin/python
  8. Open the Command Pallette and type Python: Configure Tests, then select the unittest framework

Expected Results

  1. Code completion works
  2. Code navigation works
  3. Organize imports works
  4. Import suggestions/discovery works
  5. (VS Code) Tests (either classes or methods) will have a new line above their definitions, containing two actions: Run Test | Debug Test, and will be executed upon clicking on them
  6. (PyCharm) Tests (either classes or methods) can be executed by placing the cursor on them and then using Ctrl+Shift+R
@blaisep
Copy link

blaisep commented May 11, 2020

I like this, but I wonder how it is different from using the Pycharm Pro docker-compose integration?

@alvarocavalcanti
Copy link
Author

To be honest, although I have PyCharm Pro I've never used its docker-compose integration in full. I only remember using it to start the containers.

@glitchwizard
Copy link

glitchwizard commented Jan 24, 2023

There's a pretty big problem with this if you're using VScode with WSL2. Once it's attached to the docker container, any new files created are created as root user and make git go haywire when trying to commit unless you mod the files beforehand with chown and reset the owner to be the same as all the rest, and not root user.

There are some workarounds to this, but they're clunky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment