Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a Docker container as a virtualenv for running Python tests from my IDE?

Don't get me wrong, virtualenv (or pyenv) is a great tool, and the whole concept of virtual environments is a great improvement on developer environments, mitigating the whole Snowflake Server anti-pattern.

But nowadays Docker containers are everywhere (for good reasons) and it feels odd having your application running on a container but also setting up a local virtual environment for running tests and such in the IDE.

I wonder if there's a way we could leverage Docker containers for this purpose?

like image 390
Alvaro Cavalcanti Avatar asked Mar 03 '23 04:03

Alvaro Cavalcanti


1 Answers

Summary

Yes, there's a way to achieve this. By configuring a remote Python interpreter and a "sidecar" Docker container.

This Docker container will have:

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

Get the sidecar container ready

The idea here is to duplicate your app's container and add SSH abilities to it. We'll use docker-compose to achieve this:

docker-compose.yml:

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

Dockerfile.dev

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"]

Setting up 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

Setting up 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 VS Code will restart and reload
  4. On the Explorer sidebar, click the open a folder button and then enter /code (this will be loaded from the remote container)
  5. On the Extensions sidebar, select the Python extension and install it on the container
  6. When prompet on which interppreter to use, select /usr/local/bin/python
  7. Open the Command Pallette and type Python: Configure Tests, then select the unittest framework

TDD Enablement

Now that you can run your tests directly from your IDE, use it to try out Test-Driven-Develop! One of its key points is a fast feedback loop, and not having to wait for the full test suite to finish execution just to see if your new test is passing is great! Just write it and run it right away!

Reference

The contents of this answer are also available in this GIST.

like image 70
Alvaro Cavalcanti Avatar answered Mar 26 '23 09:03

Alvaro Cavalcanti