Still Wondering, How to Set Up a Docker Interpreter With PyCharm?
Why docker in PyCharm?
There can be many different reasons for using a Docker interpreter in PyCharm. But the ones that interest me are:
- I do not want to set up and install Cuda libraries in my host machine, and
- I want to enjoy the flexibility of docker not at the stage of deployment but also at the stage of development and save time on testing the compatibility of my code for different OS during deployment.
PyCharm inside docker or docker inside PyCharm?
There are mainly two ways to go about it. 1) Run PyCharm from an interactive GUI enabled docker session. or 2) Setup a docker interpreter in Pycharm.
The advantage of the first option is that when you update your PyCharm version (which happens every 2 to 3 months for me), you do not need to worry if your previous interpreter setup will work. After starting an interactive docker session, open the PyCharm from the terminal inside the container, and work on it as if you are working on your host computer. This option cannot be always available depending on what OS you are using, where is your Pycham is installed, and whether the volume mapping of a certain directory is allowed.
The advantage of the second way is that you can work naturally with the interpreter just like virtualenv or conda interpreters and there is no need to run the docker session in a separate terminal every time.
I will explain both of these options here with Ubuntu 20.04 and PyCharm-professional 2020.2.1. There is a chance that some of the settings shown below are different for Windows or iOS.
Option 1: PyCharm inside a docker container
- Install docker
- Create a docker image with java installation if it is not already in your base image. In my case, my base image is tensorflow 2 with GPU and Python 3.
# Dockerfile for tensorflow 2 with gpu
FROM tensorflow/tensorflow:latest-gpu-py3
# Install OpenJDK-8
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME# build your custom image using the following command
docker build -t myimage .
3. Run docker image interactively with volume mounting to the location of your PyCharm installation:
docker run
-it #Make docker session interactive
--rm #Automatically remove the container when it exits
--gpus all #Enable GPU support in the container
-e DISPLAY=${DISPLAY} #Pass environment variable display
--net=host #Share network configu of the host with this container
--user "$(id -u):$(id -g)" #Pass user id and group id
-v <path to pycharm in your host machine>:<path to pycharm in docker container> #Volume mounting to access pycharm from the container
myimage
Now you can open PyCharm from the interactive docker container just like your host machine. Your python interpreter needs to be set as a system interpreter. To check the path of the docker container python installation, run the “which” command.
which python
If you want to make changes to your project and save them in your local machine. You can mount your project directory with the -v option. You can also read the dataset which might be on a different location using a read-only mount by appending:ro at the end of the mounting command.
Note: To be able to use PyCharm configs it is important to map the PyCharm config directory as well. To ease the process of mapping, I usually map my entire home directory into the docker container.
Option 2: Run docker interpreter inside PyCharm
Open PyCharm and create your project. When you are first asked to choose the interpreter, choose virtualenv as an option. Even though you do not want to use that interpreter, choose that as an option. This is to avoid the long-standing bug in the Jetbrains docker interpreter option. If you choose to provide a docker interpreter when you create a new python project, it asks for “Remote project location” and there is no correct answer to set that remote path.
Once you create a new project using virtualenv, open the project settings->project interpreter and click on the wheel icon to add a new interpreter.
The next step is to choose your custom image and create a docker interpreter by clicking the “New” button.
When you will click ok on all the windows, you will return to the project interpreter window where the new docker interpreter is chosen for the current project. Below the interpreter, there is an option to set the path mappings. You can leave this empty.
The one last crucial step is to set up a run/debug configuration of the scripts.
After clicking ok for this configuration you can simply run your script and the docker interpreter will serve its purpose the way it should. It is nice to create a template with these settings so that you can easily create multiple debug/run configs for future scripts.
Happy coding!