Introduction

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.

Disclaimers

  • 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.
  • Using Docker as a remote Python interpreter with PyCharm is possible only with the Professional edition.
  • I will assume that you already installed Docker on your machine.
  • 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.

Project structure

For this tutorial I’m using a toy project with just 3 files:

  • Dockerfile to generate the container.
  • requirements.txt file that contains the dependencies of the project.
  • 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.

Dockerfile

For an article more focused on Docker and Dockerfiles I recommend the Docker Beginner’s Guide.

Here follow our Dockerfile and a brief explanation

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

#docker #cuda #python #visual-studio-code #pycharm

Docker-based Python Development with CUDA Support on PyCharm
1.95 GEEK