为什么在PyCharm中使用docker?(Why docker in PyCharm?)
There can be many different reasons for using a Docker interpreter in PyCharm. But the ones that interest me are:
在PyCharm中使用Docker解释器可能有许多不同的原因。 但是令我感兴趣的是:
- 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.我想在部署阶段而不是在开发阶段享受docker的灵活性,并节省部署期间测试我的代码对不同OS的兼容性的时间。
(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.
主要有两种解决方法。 1)从启用了交互式GUI的docker会话中运行PyCharm。 或2)在Pycharm中设置docker解释器。
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.
第一种选择的优点是,当您更新PyCharm版本(对我来说每2到3个月更新一次)时,您不必担心以前的解释器设置是否可以使用。 启动交互式docker会话后,从容器内的终端打开PyCharm,并像在主机计算机上一样对其进行操作。 根据您所使用的操作系统,Pycham的安装位置以及是否允许特定目录的卷映射,此选项不能始终可用。 第二种方法的优点是,您可以像virtualenv或conda解释器一样自然地使用解释器,并且无需每次都在单独的终端中运行docker会话。
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.
我将在Ubuntu 20.04和PyCharm-professional 2020.2.1中解释这两个选项。 对于Windows或iOS,以下显示的某些设置可能会有所不同。
Option 1: PyCharm inside a docker container
选项1:Docker容器内的PyCharm
- 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:
3.通过批量安装到PyCharm安装位置以交互方式运行docker映像:
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 shell just like your host machine. If you want to make changes to your project and save them in your local machine. You can mount your project directory with -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.
现在,您可以像从主机一样从外壳打开PyCharm。 如果要更改项目并将其保存在本地计算机中。 您可以使用-v选项挂载项目目录。 您还可以通过在挂载命令末尾附加:ro来使用只读挂载来读取可能位于其他位置的数据集。
Option 2: Run docker interpreter inside PyCharm
选项2:在PyCharm中运行docker解释器
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.
打开PyCharm并创建您的项目。 首次要求您选择解释器时,请选择virtualenv作为选项。 即使您不想使用该解释器,也请选择该选项。 这是为了避免Jetbrains docker解释器选项中的长期错误。 如果您在创建新的python项目时选择提供docker解释器,它将要求提供“远程项目位置”,并且没有正确的答案来设置该远程路径。
When you choose docker as an interpreter for a new pure python project, “Remote project location” is to be set.
当您选择docker作为新的纯python项目的解释器时,将设置“远程项目位置”。
There is no correct answer for the remote project location as the warning suggests “This interpreter type does not support remote project creation”. This is a bug.
对于远程项目位置,没有正确的答案,因为警告提示“此解释器类型不支持远程项目创建”。 这是一个错误。
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.
使用virtualenv创建新项目后,打开项目设置->项目解释器,然后单击滚轮图标以添加新的解释器。
Notice that the current interpreter of this project is system installed python.
请注意,此项目的当前解释器是系统安装的python。
The next step is to choose your custom image and create a docker interpreter by clicking the “New” button.
下一步是选择您的自定义映像,然后单击“新建”按钮来创建docker解释器。
On left choose docker to create the docker interpreter. Choose the image of your choice for the interpreter. 在左侧,选择docker创建docker解释器。 为口译员选择所需的图像。
Wait until you see the message “Connection successful” at the bottom of this window. 等待直到您在该窗口底部看到消息“连接成功”。
When you will click ok to 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.
单击所有窗口的确定后,将返回项目解释器窗口,在该窗口中为当前项目选择新的docker解释器。 在解释器下方,有一个选项可以设置路径映射。 您可以将此留空。
Leave the Path mappings entry. 保留路径映射条目。
The one last crucial step is to set up a run/debug configuration of the scripts.
最后一个至关重要的步骤是设置脚本的运行/调试配置。
This is where you can set volume mappings and run-time options for the container. 您可以在此处设置容器的卷映射和运行时选项。
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.
单击确定进行此配置后,您可以简单地运行脚本,而docker解释器将按应有的方式发挥作用。 最好使用这些设置来创建模板,以便您可以轻松地为将来的脚本创建多个调试/运行配置。
Happy coding!
编码愉快!
翻译自: https://medium.com/swlh/still-wondering-how-to-set-up-a-docker-interpreter-with-pycharm-5bfdb8e1e65d