Setting up a development environment is not easy if you are inexperienced, especially if a lot of technologies you wish to learn are involved. This tutorial aims to show you how to set up a basic Docker-based Python development environment with CUDA support in PyCharm or Visual Studio Code.
如果您没有经验,那么设置开发环境并不容易,尤其是涉及许多您想学习的技术时。 本教程旨在向您展示如何在PyCharm或Visual Studio Code中设置具有CUDA支持的基于Docker的基本Python开发环境。
At the time of writing, I was unable to use CUDA inside of Docker in Windows 10 Home (even with the Insider build) so this tutorial has been implemented with Linux in mind even though there’s basically nothing that is platform-specific.
在撰写本文时, 我无法在Windows 10 Home中的Docker内部使用CUDA (即使使用Insider构建),因此即使基本上没有特定于平台的内容,本教程也已在Linux上实现。
Using Docker as a remote Python interpreter with PyCharm is possible only with the Professional edition. 只有在专业版中,才可以将Docker用作带有PyCharm的远程Python解释器。I will assume that you already installed Docker on your machine.
我将假设您已经在机器上安装了Docker 。
I will assume that you already installed CUDA on your machine. If you are still setting up your Linux machine and you are not willing to research much about it I usually recommend Pop!_OS. In this article, you can find how to setup CUDA and cuDNN very easily on their platform. The article also provides instructions to use their packages on Ubuntu.
我将假定您已经在计算机上安装了CUDA 。 如果您仍在设置Linux计算机,并且您不愿意对其进行过多研究,那么我通常建议您使用Pop!_OS 。 在本文中 ,您可以找到如何在其平台上非常轻松地设置CUDA和cuDNN。 本文还提供了在Ubuntu上使用其软件包的说明。
For this tutorial I’m using a toy project with just 3 files:
在本教程中,我使用的玩具项目只有3个文件:
A Dockerfile to generate the container.
一个用于生成容器的Dockerfile 。
A requirements.txt file that contains the dependencies of the project.
包含项目依赖项的requirements.txt文件。
A single run.py file that contains some code to run. Obviously your personal project will most likely be more complex, you may use a different method for dependency management and you might also use a docker-compose.yaml file but for the sake of getting my point through that is pointless complexity.
一个包含一些要运行代码的single run.py文件。 显然,您的个人项目很可能会更复杂,您可以使用其他方法进行依赖项管理,也可以使用docker-compose.yaml文件,但是为了使我明白这一点,没有任何意义。
For an article more focused on Docker and Dockerfiles I recommend the Docker Beginner’s Guide. Here follow our Dockerfile and a brief explanation
对于更专注于Docker和Dockerfiles的文章,我建议使用Docker入门指南 。 在此遵循我们的Dockerfile和简要说明
FROM nvidia/cuda:10.2-devel # Miniconda install copy-pasted from Miniconda's own Dockerfile reachable # at: https://github.com/ContinuumIO/docker-images/blob/master/miniconda3/debian/Dockerfile ENV PATH /opt/conda/bin:$PATH RUN apt-get update --fix-missing && \ apt-get install -y wget bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 git mercurial subversion && \ apt-get clean RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \ /bin/bash ~/miniconda.sh -b -p /opt/conda && \ rm ~/miniconda.sh && \ /opt/conda/bin/conda clean -tipsy && \ ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \ echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \ echo "conda activate base" >> ~/.bashrc && \ find /opt/conda/ -follow -type f -name '*.a' -delete && \ find /opt/conda/ -follow -type f -name '*.js.map' -delete && \ /opt/conda/bin/conda clean -afy # Project setup WORKDIR /code COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "./run.py"]In layman’s terms a Dockerfile describes a procedure to generate a Docker image that is then used to create Docker containers. This Dockerfile builds on top of the nvidia/cuda:10.2-devel image made available in DockerHub directly by NVIDIA.
用外行术语来说, Dockerfile描述了生成Docker映像的过程,该映像随后用于创建Docker容器。 该Dockerfile建立在NVIDIA直接在DockerHub中提供的nvidia/cuda:10.2-devel 映像的基础上。
nvidia/cuda:10.2-devel is a development image with the CUDA 10.2 toolkit already installed Now you just need to install what we need for Python development and setup our project.
nvidia/cuda:10.2-devel是一个已安装CUDA 10.2工具包的开发映像。现在,您只需要安装Python开发所需的内容并设置项目即可。
In the middle section of the Dockerfile there is a Miniconda3 installation. I decided to use Miniconda instead of just using Python because it's my go-to platform for most of my projects. We are leveraging none of Miniconda capabilities so it is kind of overkill. Swapping out Miniconda with a vanilla installation of Python in the Dockerfile is left to the reader as an exercise (do not panic, just use the same commands you would use on a new Ubuntu box).
在Dockerfile的中间部分,安装了Miniconda3。 我决定使用Miniconda而不是仅使用Python,因为它是我大多数项目的首选平台。 我们没有利用Miniconda的任何功能,因此有点过头了。 作为练习,留给读者的是在Dockerfile中使用Python的Dockerfile安装Dockerfile操作(不要惊慌,只需使用与在新Ubuntu盒中使用的相同命令)即可。
The last section is about the project setup, we are just installing the dependencies and copying all the files inside the image work directory and choosing the command to launch when docker run is called without a specified command.
最后一部分是关于项目设置的,我们只是安装依赖项并复制映像工作目录中的所有文件,然后选择在没有指定命令的情况下调用docker run时要启动的命令。
To build the Docker image just navigate with your shell of choice to the path containing the Dockerfile and run:
要构建Docker映像,只需使用选择的shell导航到包含Dockerfile的路径并运行:
docker build -t <image_name> .This will generate the Docker image as described by the configuration and give it the name image_name. If in the name no tag is specified latest is used as a default. To specify the tag just write it after a colon. I will use the name pytorch-development-box for the image in the rest of the tutorial.
这将生成配置所描述的Docker映像,并将其命名为image_name 。 如果名称中未指定标签,则默认使用最新标签。 要指定标签,只需将其写在冒号后面。 在本教程的其余部分中,我将为图像使用名称pytorch-development-box 。
I’m using only Pytorch and Torchvision as the dependencies for this project. I use those packages a lot and I will use their CUDA availability method to check if everything is working. So the content of my requirements.txt is:
我仅使用Pytorch和Torchvision作为该项目的依赖项。 我经常使用这些软件包,并且将使用其CUDA可用性方法来检查一切是否正常。 所以我的requirements.txt的内容是:
torch torchvisionMy Python file is quite simple, I’m just checking if CUDA is available or not.
我的Python文件非常简单,我只是在检查CUDA是否可用。
import torch.cuda if torch.cuda.is_available(): print("CUDA is available :D") else: print("CUDA isn't available :(")Using a remote Python interpreter from Docker is available only on PyCharm Professional. So, let’s see how to set it up.
仅在PyCharm Professional上可以使用Docker的远程Python解释器。 因此,让我们看看如何进行设置。
Once you’ve built your Docker image and opened your project folder in PyCharm navigate to File > Settings > Project > Python Interpreter. You should see something like this:
构建Docker映像并在PyCharm中打开项目文件夹后,导航至“ 文件”>“设置”>“项目”>“ Python解释器” 。 您应该会看到以下内容:
Now click on the little gear near the top-right and add a new Python interpreter. Here you will need to select Docker and choose the image name you selected before in the drop-down menu called Image name like so:
现在,单击右上角附近的小齿轮,并添加一个新的Python解释器。 在这里,您需要选择Docker并在名为Image name的下拉菜单中选择之前选择的映像名称,如下所示:
After this configuration is confirmed, wait for the indexing to finish, and try running run.py.
确认此配置后,等待索引完成,然后尝试运行run.py
CUDA isn't available :(At this point, we didn’t configure the Run/Debug configuration to have Docker use the GPU but we can quickly fix it. Open the auto-generated Run/Debug configuration and add--gpus all at the end of the Docker container settings. You should end up with something like this:
在这一点上,我们没有将运行/调试配置配置为让Docker使用GPU,但是我们可以快速对其进行修复。 打开自动生成的运行/调试配置,并在Docker容器设置的末尾添加--gpus all 。 您应该以如下形式结束:
Confirm this configuration and run it. The CUDA result is available now!
确认此配置并运行它。 CUDA结果现在可用!
I will rely on the new Remote Development extensions for Visual Studio Code to setup development through Docker. The first step will be installing the Remote Development extension pack and opening the project folder.
我将依靠Visual Studio Code的新远程开发扩展来通过Docker设置开发。 第一步将是安装Remote Development扩展包并打开项目文件夹。
Use the Add Development Container Configuration Files command from the Visual Studio command palette. Choose to use your own Dockerfile.
使用Visual Studio命令面板中的“ 添加开发容器配置文件”命令。 选择使用您自己的Dockerfile。
At this point a devcontainer.json file will be created into a .devcontainer directory and it will look like this:
在这一点上devcontainer.json文件将被创建成.devcontainer目录,它看起来就像这样:
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: // https://github.com/microsoft/vscode-dev-containers/tree/v0.128.0/containers/docker-existing-dockerfile { "name": "Existing Dockerfile", // Sets the run context to one level up instead of the .devcontainer folder. "context": "..", // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. "dockerFile": "../Dockerfile", // Set *default* container specific settings.json values on container create. "settings": { "terminal.integrated.shell.linux": null }, // Add the IDs of extensions you want installed when the container is created. "extensions": [] // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], // Uncomment the next line to run commands after the container is created - for example installing curl. // "postCreateCommand": "apt-get update && apt-get install -y curl", // Uncomment when using a ptrace-based debugger like C++, Go, and Rust // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], // Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker. // "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ], // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. // "remoteUser": "vscode" }A prompt to reopen the folder in the container will pop-up. Before doing so we just need to select some extensions to use while developing in the container. Go to the Extensions tab, browse for the extensions you need and you can right-click and select Add to devcontainer.json to add them to the configuration.
弹出重新打开容器中文件夹的提示。 在这样做之前,我们只需要选择在容器中进行开发时要使用的一些扩展即可。 转到扩展选项卡,浏览所需的扩展,然后右键单击并选择Add to devcontainer.json以将其添加到配置中。
Now we just need to add a runArgs key for enabling the GPU and we will be ready to start development. Minus the comments you should end up with something like this:
现在我们只需要添加一个runArgs键来启用GPU,我们就可以开始开发了。 减去注释,您应该得到这样的结果:
{ "name": "Existing Dockerfile", "context": "..", "dockerFile": "../Dockerfile", "settings": { "terminal.integrated.shell.linux": null }, "extensions": [ "ms-python.python" ], // This was added! "runArgs": [ "--gpus=all" ] }Now from the command palette, we can Rebuild and Reopen in Container and we will be ready to go!
现在,从命令面板中,我们可以Rebuild and Reopen in Container ,我们就可以开始了!
Now you have quite a basic development environment configured in your IDE that is based on your own Docker image and all of this with GPU support. All that we need to do to customize it to fit your project needs and start having fun!
现在,您已经在IDE中配置了相当基本的开发环境,该环境基于您自己的Docker映像,并且所有这些都具有GPU支持。 我们需要做的所有事情来定制它以满足您的项目需求并开始玩乐!
Gregory Sech — Data Science Student
Gregory Sech —数据科学专业的学生
Data Science student at Ca’ Foscari University of Venice. Passionate about Deep Learning
威尼斯卡弗斯卡里大学数据科学系学生。 对深度学习充满热情
You can also read this article on our Mobile APP
您也可以在我们的移动应用程序上阅读本文
Originally published at https://www.analyticsvidhya.com on August 19, 2020.
最初于 2020年8月19日 发布在 https://www.analyticsvidhya.com 上。
翻译自: https://medium.com/analytics-vidhya/docker-based-python-development-with-cuda-support-on-pycharm-and-or-visual-studio-code-53068088d9a6