This guide explains how to properly configure NVIDIA GPU access for your Telegram Sticker Bot running in an LXC/LXD container.
- NVIDIA GPU with drivers installed
- LXD/LXC properly installed
- NVIDIA Container Runtime
# Check NVIDIA driver
nvidia-smi
# Check NVIDIA container runtime
which nvidia-container-runtime
# If not installed:
sudo apt update
sudo apt install -y nvidia-container-runtime# Initialize LXD if not done already
sudo lxd init
# Enable GPU support in LXD
sudo snap refresh lxd --channel=latest/stable# For existing container
sudo lxc config device add stickerbot gpu gpu
# For new container with GPU
sudo lxc launch ubuntu:22.04 stickerbot
sudo lxc config device add stickerbot gpu gpu
# Alternative: Add specific GPU (if multiple GPUs)
sudo lxc config device add stickerbot gpu0 gpu id=0# Enter container
sudo lxc exec stickerbot -- bash
# Install NVIDIA drivers (must match host version)
apt update
apt install -y nvidia-driver-470 # Use same version as host
# Install NVIDIA container toolkit
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add - \
&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
apt update
apt install -y nvidia-docker2
# Restart container
exit
sudo lxc restart stickerbot# Test GPU access in container
sudo lxc exec stickerbot -- nvidia-smi
# Test FFmpeg NVENC access
sudo lxc exec stickerbot -- ffmpeg -f lavfi -i testsrc=duration=1:size=64x64:rate=1 -c:v h264_nvenc -f null -# List NVIDIA devices
ls -la /dev/nvidia*
# Should show something like:
# /dev/nvidia0
# /dev/nvidia-uvm
# /dev/nvidia-uvm-tools
# /dev/nvidiactl
# /dev/nvidia-modeset# Add each NVIDIA device
sudo lxc config device add stickerbot nvidia0 unix-char path=/dev/nvidia0
sudo lxc config device add stickerbot nvidiactl unix-char path=/dev/nvidiactl
sudo lxc config device add stickerbot nvidia-uvm unix-char path=/dev/nvidia-uvm
sudo lxc config device add stickerbot nvidia-uvm-tools unix-char path=/dev/nvidia-uvm-tools
sudo lxc config device add stickerbot nvidia-modeset unix-char path=/dev/nvidia-modeset
# Add DRI devices for rendering
sudo lxc config device add stickerbot dri unix-char path=/dev/dri/renderD128
sudo lxc config device add stickerbot dri-card unix-char path=/dev/dri/card0# In container, add user to video group
sudo lxc exec stickerbot -- usermod -a -G video root
sudo lxc exec stickerbot -- usermod -a -G video ubuntu # or your user
# Restart container
sudo lxc restart stickerbot# Create a reusable GPU profile
sudo lxc profile create gpu-profile
# Configure the profile
sudo lxc profile edit gpu-profile# Add this content to the profile
config:
nvidia.runtime: "true"
nvidia.driver.capabilities: all
description: GPU-enabled profile for containers
devices:
gpu:
type: gpu
gpu0:
id: "0"
type: gpu
name: gpu-profile# Apply to existing container
sudo lxc profile add stickerbot gpu-profile
# Create new container with profile
sudo lxc launch ubuntu:22.04 stickerbot-gpu --profile default --profile gpu-profileIf LXD GPU passthrough is problematic, run the bot in Docker within LXD:
sudo lxc exec stickerbot -- bash
# Install Docker
curl -fsSL https://get.docker.com | sh
usermod -aG docker ubuntu# Inside container, run bot with GPU
docker run --gpus all \
-e BOT_TOKEN=your_token \
-v /opt/stickerbot:/app \
node:18 \
/app/src/bot.js#!/bin/bash
# test-gpu-in-lxd.sh
echo "π§ͺ Testing GPU access in LXD container..."
# Test 1: Basic GPU detection
echo "Test 1: NVIDIA Driver"
sudo lxc exec stickerbot -- nvidia-smi || echo "β nvidia-smi failed"
# Test 2: CUDA functionality
echo "Test 2: CUDA Runtime"
sudo lxc exec stickerbot -- nvidia-smi --query-gpu=name --format=csv,noheader || echo "β CUDA query failed"
# Test 3: FFmpeg NVENC
echo "Test 3: FFmpeg NVENC Encoder"
sudo lxc exec stickerbot -- ffmpeg -f lavfi -i testsrc=duration=0.1:size=64x64:rate=1 -c:v h264_nvenc -f null - 2>/dev/null && echo "β
NVENC working" || echo "β NVENC failed"
# Test 4: Device permissions
echo "Test 4: Device Access"
sudo lxc exec stickerbot -- ls -la /dev/nvidia* || echo "β NVIDIA devices not accessible"
# Test 5: Bot's GPU detection
echo "Test 5: Bot GPU Detection"
sudo lxc exec stickerbot -- bash -c "cd /opt/stickerbot && node -e \"
import('./src/modernVideoProcessor.js').then(m =>
m.detectSystemCapabilities().then(caps => {
console.log('FFmpeg available:', caps.ffmpegAvailable);
console.log('NVIDIA GPU:', caps.hasNvidiaGPU);
console.log('Running in container:', caps.isLXC);
})
)\"" || echo "β Bot GPU detection failed"
echo "π GPU testing complete"# Solution: Install matching NVIDIA drivers
sudo lxc exec stickerbot -- bash
apt update && apt install nvidia-driver-470 # Match host version# Solution: Fix device permissions
sudo lxc config device add stickerbot nvidia0 unix-char path=/dev/nvidia0 uid=0 gid=0 mode=0666
sudo lxc config device add stickerbot nvidiactl unix-char path=/dev/nvidiactl uid=0 gid=0 mode=0666# Solution: Add missing devices and restart
sudo lxc config device add stickerbot nvidia-uvm unix-char path=/dev/nvidia-uvm
sudo lxc config device add stickerbot nvidia-modeset unix-char path=/dev/nvidia-modeset
sudo lxc restart stickerbot# Solution: Restart LXD and containers
sudo systemctl restart snap.lxd.daemon
sudo lxc restart stickerbot
# Update drivers in container to match host
sudo lxc exec stickerbot -- apt update && apt upgrade nvidia-driver-*# Host GPU info
nvidia-smi --query-gpu=driver_version,name --format=csv
# Container GPU access
sudo lxc exec stickerbot -- nvidia-smi --query-gpu=driver_version,name --format=csv
# FFmpeg capabilities in container
sudo lxc exec stickerbot -- ffmpeg -encoders | grep nvenc
# Bot system detection
sudo lxc exec stickerbot -- bash -c "cd /opt/stickerbot && npm run test-gpu"| Method | Setup Complexity | Performance | Compatibility | Recommended |
|---|---|---|---|---|
| LXD GPU passthrough | Medium | Excellent | High | β Yes |
| Manual device mapping | High | Good | Medium | |
| GPU Profile | Low | Excellent | High | β Yes |
| Docker in LXD | Medium | Good | Medium |
# Create optimized container for bot
sudo lxc launch ubuntu:22.04 stickerbot-prod
# Add GPU support
sudo lxc config device add stickerbot-prod gpu gpu
# Increase resource limits
sudo lxc config set stickerbot-prod limits.cpu 4
sudo lxc config set stickerbot-prod limits.memory 4GB
# Add storage for temp files
sudo lxc config device add stickerbot-prod temp-storage disk source=/tmp/stickerbot path=/tmp/stickerbot
# Enable autostart
sudo lxc config set stickerbot-prod boot.autostart true#!/bin/bash
# /opt/stickerbot/start-in-lxd.sh
# Verify GPU access
if ! nvidia-smi >/dev/null 2>&1; then
echo "β GPU not accessible, running in CPU mode"
fi
# Start bot with GPU detection
cd /opt/stickerbot
export NODE_ENV=production
export FORCE_GPU_CHECK=true
npm startFollowing this guide should give your bot full NVIDIA GPU acceleration within the LXC container, dramatically improving video processing performance!