Skip to content

Instantly share code, notes, and snippets.

@kenrmayfield
Forked from Cordtus/newfile.md
Created September 30, 2025 13:29
Show Gist options
  • Select an option

  • Save kenrmayfield/83f8bb322b79c7d7df926a8f8ed1c131 to your computer and use it in GitHub Desktop.

Select an option

Save kenrmayfield/83f8bb322b79c7d7df926a8f8ed1c131 to your computer and use it in GitHub Desktop.
Enabling GPU access by LXC containers

NVIDIA GPU Access in LXC/LXD Containers

This guide explains how to properly configure NVIDIA GPU access for your Telegram Sticker Bot running in an LXC/LXD container.

🎯 Prerequisites

Host System Requirements

  • NVIDIA GPU with drivers installed
  • LXD/LXC properly installed
  • NVIDIA Container Runtime

Verify Host GPU Setup

# 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

πŸ”§ Method 1: LXD GPU Passthrough (Recommended)

Step 1: Configure LXD for GPU Support

# Initialize LXD if not done already
sudo lxd init

# Enable GPU support in LXD
sudo snap refresh lxd --channel=latest/stable

Step 2: Add GPU to Container

# 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

Step 3: Install NVIDIA Drivers in Container

# 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

Step 4: Verify GPU Access

# 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 -

πŸ”§ Method 2: Manual Device Mapping

Step 1: Find GPU Devices on Host

# 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

Step 2: Add Devices to Container

# 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

Step 3: Set Proper Permissions

# 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

πŸ”§ Method 3: LXD Profile for GPU Containers

Create GPU Profile

# Create a reusable GPU profile
sudo lxc profile create gpu-profile

# Configure the profile
sudo lxc profile edit gpu-profile

GPU Profile Configuration

# 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 Profile to Container

# 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-profile

🐳 Method 4: Docker Alternative in LXD

If LXD GPU passthrough is problematic, run the bot in Docker within LXD:

Step 1: Install Docker in Container

sudo lxc exec stickerbot -- bash

# Install Docker
curl -fsSL https://get.docker.com | sh
usermod -aG docker ubuntu

Step 2: Docker with GPU Support

# 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

πŸ§ͺ Testing GPU Access

Complete Test Script

#!/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"

πŸ”§ Troubleshooting

Common Issues and Solutions

Issue: nvidia-smi not found in container

# Solution: Install matching NVIDIA drivers
sudo lxc exec stickerbot -- bash
apt update && apt install nvidia-driver-470  # Match host version

Issue: Permission denied on /dev/nvidia*

# 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

Issue: FFmpeg NVENC fails with "No NVENC capable devices found"

# 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

Issue: Container can't access GPU after host driver update

# 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-*

Verification Commands

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

πŸ“Š Performance Comparison

Method Setup Complexity Performance Compatibility Recommended
LXD GPU passthrough Medium Excellent High βœ… Yes
Manual device mapping High Good Medium ⚠️ If needed
GPU Profile Low Excellent High βœ… Yes
Docker in LXD Medium Good Medium ⚠️ Alternative

πŸš€ Final Container Configuration

Recommended Container Setup

# 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

Container Startup Script

#!/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 start

Following this guide should give your bot full NVIDIA GPU acceleration within the LXC container, dramatically improving video processing performance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment