Docker toolchain for building and running OpenCV and Pytorch C++ projects with CUDA support in CLion
The main feature to use docker container for development is to encapsulate all dependencies in a container. This allows to build and run the code on any host system without the need to install dependencies (or only a minimal installation is required)
- Build all dependencies encapsulated in a Docker container (minimal host dependencies)
- Use local NVIDIA GPU in docker
- Use Pytorch with CUDA support
- Use OpenCV with local devives (Webcam, screen output) in docker
- NVIDIA CUDA 12.2.2
- NVIDIA cuDNN8
- CMAKE 3.28.3
- GCC 12
- LLVM/CLang 17
- Ninja 1.11.1
- Pytorch (libtorch) 2.3.x
- OpenCV 4.x
- My host OS is TUXEDO-OS, based on Ubuntu.
- This example is to be used with JetBrains CLion, so you need to have it installed.
- I used NVIDIA RTX-4070 GPU, but it should work with other NVIDIA GPU with CUDA support.
First, install Docker on your host system. Use the common instructions from web, like Install Docker Engine on Ubuntu or How To Install and Use Docker on Ubuntu 22.04.
Note: Don't forget to add your user to the docker group to avoid using sudo for docker commands:
sudo usermod -aG docker $USER
To apply the new group membership, log out of the server and back in, or type the following:
su - $USER
Check docker availability:
docker --version
You should see the version of docker installed, like this:
Docker version 25.0.4, build 1a576c5
- Add the NVIDIA repository to the system
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt update
- Install actual driver if not already installed
sudo apt install -y nvidia-driver-535
- Install the NVIDIA Container Toolkit
sudo apt install -y nvidia-container-toolkit
-
Restart system
-
Check NVIDIA GPU is available
nvidia-smi
You should see the output of the NVIDIA System Management Interface, like this:
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.146.02 Driver Version: 535.146.02 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 4070 ... Off | 00000000:01:00.0 Off | N/A |
| N/A 53C P0 29W / 65W | 1185MiB / 8188MiB | 49% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
Go to the directory where you want to clone the repository and run the following command:
git clone https://github.com/sleepingwithshoes/torchopencv.git
cd torchopencv
To build the docker image, run the following command (Please note that build process will take some time, depending on the performance of your host system.)
docker build --no-cache --build-arg UID=$(id -u) -t cliontorch -f docker/Dockerfile .
- Open Clion and open the project folder (where you cloned the repository)
- An overlay window should appear to configure the cmake profile. If not, go to
File -> Settings -> Build, Execution, Deployment -> CMake
and add a new profile. - Add a new profile by clicking the
+
.
- Set a
Name
like Debug-docker, selectBuild type
Debug. - Select
Manage toolchains...
behindToolchain:
to add a new toolchain. The following window should appear: - Click the
+
button and selectDocker
from the dropdown menu. - Set a
Name
(like Docker-torch-opencv) and select the dockerImage
you built before (cliontorch:latest
). - Modify the
Containter Settings
to selectGearwheel
- Click
OK
through all windows to save the settings.
Now you have set up the toolchain to build and run the project in a docker container.
Perhaps this little repository helps you to develop C++ projects with Pytorch and OpenCV in a docker container.
Have fun and happy coding!