EN ·
🌏 中文

Deploying GPU and Web Camera Applications with Docker

Introduction

Distributing applications, especially Python-based ones, often involves complex environmental configurations. Manual setup is not only labor-intensive but also prone to compatibility issues. Dependencies on host OS versions, filesystem structures, and pre-installed packages can lead to “it works on my machine” syndrome. Docker solves this by encapsulating the entire environment, ensuring consistency across different deployment targets.

Installation and Configuration

To set up a GPU-enabled Docker environment, you must first install Docker, followed by the NVIDIA Container Toolkit.

Installing Docker

Refer to the official documentation: https://docs.docker.com/engine/install/ubuntu

Installing NVIDIA Container Toolkit

Refer to the official guide: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker

Displaying GUI from Docker

If your application requires a graphical user interface (GUI), you need to configure the container to access the host’s display server.

First, authorize X Server access on the host:

xhost +local:docker

Or, for a more permissive setting:

xhost +

When running the container, mount the X11 socket and set the display environment variable:

-v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY

Full command example:

docker run --name mydocker --gpus all --shm-size=1g --ulimit memlock=-1 -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY snn-server:basic

Note: --gpus all uses all available GPUs; specify --gpus 0 to use a single GPU.

Accessing USB Cameras in Docker

To allow the container to access a USB camera, map the device path from the host to the container:

-v /dev/video0:/dev/video0 --device=/dev/video0

Full command example:

docker run --name mydocker --gpus all --shm-size=1g --ulimit memlock=-1 -v /dev/video0:/dev/video0 --device=/dev/video0 -it -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY myimage:latest

Exposing Ports

To access services running inside the container from the host, use port mapping:

-p 8080:8080

This maps the host’s port 8080 to the container’s port 8080.

Enabling Advanced GPU Features

If your application uses advanced GPU features like nvcuvid decoding and you encounter an error such as Cannot load libnvcuvid.so.1, you must explicitly enable these capabilities at startup:

--runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video

Ensure your host driver supports these features.

Setting Up a Local Image Registry

For large images (often several GBs), pulling from Docker Hub can be slow. Setting up a local registry in your LAN can significantly improve deployment speed:

docker run -d -p 5000:5000 --restart=always --name xy-registry -v /home/centos/registry:/registry registry:latest

Key Parameters

Pushing Images

docker commit container_id ip:port/image_name:tag
docker push ip:port/image_name:tag

Cleaning Up Disk Space

Check Docker disk usage with:

docker system df

Prune Unused Images

To remove all dangling images:

docker image prune

Remove Specific Images

List all images:

docker images

Remove by ID:

docker rmi image_id

GPU Docker with Anaconda

To use Anaconda within a GPU-enabled container, you can follow these methods:

1. Using a Dockerfile

FROM nvidia/cuda:11.3.0-devel-ubuntu20.04

# Add dependencies
RUN apt-get clean && apt-get update -y -qq
RUN apt-get install -y curl git build-essential

ENV PATH="/root/anaconda3/bin:${PATH}"

RUN curl --silent -O https://repo.anaconda.com/archive/Anaconda3-2023.03-Linux-x86_64.sh \
    && bash Anaconda3-2023.03-Linux-x86_64.sh -b -p /root/anaconda3

RUN pip install keras && conda install pygpu
RUN /bin/bash

2. Manual Construction

Pull the base GPU image and perform the installation steps manually inside the container.

References