Make NVDEC/NVENC work again in containers that get only some of the host's NVIDIA GPUs, with one LD_PRELOAD.
English | 简体中文
Does ffmpeg (or any other NVDEC/NVENC program) fail like this inside a Docker or Kubernetes container on NVIDIA driver 570 or newer?
cuvidGetDecoderCaps(&caps) failed -> CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
OpenEncodeSessionEx failed: unsupported device (2): (no details)
If the container holds only some of the host's GPUs, this is most likely a known driver bug (CUDA itself keeps working). Preload this library and NVDEC/NVENC work again: no driver patch, no system changes. Verified on driver 595.71.05, see Tested.
curl -fLO https://github.com/Lixsp11/nvcodec-container-fix/releases/latest/download/libnvcodec_container_fix.so
curl -fLO https://github.com/Lixsp11/nvcodec-container-fix/releases/latest/download/libnvcodec_container_fix.so.sha256
sha256sum -c libnvcodec_container_fix.so.sha256
LD_PRELOAD=$PWD/libnvcodec_container_fix.so ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.mp4Put the library on the host, for example in /opt/nvcodec-container-fix/, mount it and preload it:
docker run --gpus '"device=1"' \
-v /opt/nvcodec-container-fix:/opt/nvcodec-container-fix:ro \
-e LD_PRELOAD=/opt/nvcodec-container-fix/libnvcodec_container_fix.so \
your-image ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.mp4With the library at /opt/nvcodec-container-fix/ on every GPU node (or built into the image):
spec:
containers:
- name: app
image: your-image
resources:
limits:
nvidia.com/gpu: 1
env:
- name: LD_PRELOAD
value: /opt/nvcodec-container-fix/libnvcodec_container_fix.so
volumeMounts:
- name: nvcodec-container-fix
mountPath: /opt/nvcodec-container-fix
readOnly: true
volumes:
- name: nvcodec-container-fix
hostPath:
path: /opt/nvcodec-container-fix
type: DirectoryDocker --gpus, the default nvidia-container-runtime and the GPU Operator's envvar strategy run in
libnvidia-container's legacy mode. That mode is handled by design but has not been run on hardware yet (see
Tested); reports are welcome. To preload into every process, including ones started without the variable,
see Global preload.
- Since driver 570 (still present in 595), the driver's GPU list queries return all of the host's GPUs, even in a
container that has only some of the
/dev/nvidiaNdevices. - On initialization,
libnvcuvid(NVDEC) andlibnvidia-encode(NVENC) open the "primary" GPU, the one with the lowest UUID in that list. If that GPU is not in the container, they fail. CUDA compute is not affected. - NVIDIA issues: nvidia-container-toolkit#1249, k8s-device-plugin#1282, open-gpu-kernel-modules#826. No official fix is confirmed yet, see Upstream status.
- Drop-in: one small
.sothat needs only GLIBC_2.17, so it also loads in old images. It patches no driver and changes no files or system settings. - Safe to preload everywhere: it removes only GPUs that are certainly not in the container, and keeps anything it cannot decide. Bare metal, whole-machine containers and CPU-only machines pass through untouched.
- Asks the driver, not /proc: membership comes from
NV_ESC_CARD_INFOplus a check of the 195:minor device node, so it does not rely on /proc listing the hidden GPUs. - Complete coverage: it filters
GET_ATTACHED_IDS,GET_PROBED_IDS(with its flags) andGET_ACTIVE_DEVICE_IDS(MIG entries kept), sent directly or wrapped inNV_ESC_IOCTL_XFER_CMD, for every library in the process, so CUDA, NVML and the codec libraries see the same GPU list. - Tested: unit tests that need no GPU, plus a GPU test script that compares runs with and without the library.
Comparison with similar projects
| Project | Layer / delivery | Commands filtered | XFER | Membership decided by | Unknown ids | License |
|---|---|---|---|---|---|---|
| this project | LD_PRELOAD (environment or /etc/ld.so.preload) |
0x201, 0x214 with gpuFlags, 0x288 | yes | CARD_INFO minor, /proc fallback; 195:minor character device | kept | MIT |
| flexgrip/nvidia-gpu-enumeration | LD_PRELOAD set in the image |
0x201 | no | GET_ID_INFO, then /proc |
dropped | GPLv3 |
| xerktech/fenrir nri-nvenc-fix (#45, #47) | NRI plugin, read-only /etc/ld.so.preload, i686 stub |
0x201 | no | GET_ID_INFO, then /proc (full PCI address) |
dropped | GPLv3 (shim) |
| tinkerborg/nvcodec-shim | LD_PRELOAD injected by an admission policy |
0x201, 0x214 | no | id encoding, /proc, stat of the node | kept | none stated |
| Avi130805 nvenc-gpu-filter | wraps ffmpeg | 0x201 | yes | GET_PCI_INFO, then /proc |
list left as is | MIT |
| Bl4ut0 tdarr-node-runpod #3 | LD_PRELOAD from the entrypoint |
0x201 | no | GET_PCI_INFO, then /proc, per query |
dropped | none stated |
| selkies pixelflux nvgpufilter.rs | GOT patch of ioctl in the NVIDIA libraries of its own process |
0x201, 0x214 | no | /proc, then opening the node | filters only to a strict subset | MPL-2.0 |
| WetFoxx vgnv.c | LD_PRELOAD for Sunshine |
0x201, 0x214 | no | GET_PCI_INFO against addresses from nvidia-smi |
kept | MIT |
| anlamm open-gpu-kernel-modules fork | kernel RM patch (570.86.16 open modules) | 0x201, 0x214 | n/a | kern_path() of the node in the caller's mount namespace |
hidden | MIT/GPLv2 |
Where this project differs:
- Coverage: 0x201, 0x214 (with
gpuFlagscompacted) and 0x288 (MIG entries kept), sent directly or wrapped inNV_ESC_IOCTL_XFER_CMD. Queries from every library in the process are handled, so CUDA and NVML see the same GPUs. - Membership from the driver:
NV_ESC_CARD_INFOgives each GPU's minor under the id RM uses in the lists, so membership neither depends on /proc listing the hidden GPUs nor on decoding the id. It is checked against a 195:minor character device. - Conservative: only GPUs that are certainly absent are removed; ids that cannot be placed are kept, and lists pass through when all or none of the host's GPUs are in the container.
- Low footprint:
errnois preserved, only GLIBC_2.17 symbols are needed, the hook does no allocation and no stdio, and no files or system settings are changed.
Where others are ahead:
- fenrir injects automatically into Kubernetes GPU containers through NRI. It uses a read-only
/etc/ld.so.preload, which an image's ownLD_PRELOADcannot override, and an i686 stub for 32-bit processes. - The anlamm kernel fork works inside the driver, so it also covers statically linked programs and processes started without the preload.
With tests/run_tests.sh, on hosts with 8 RTX 4090s and driver 595.71.05, in containers whose
/proc/driver/nvidia/gpus lists all of the host's GPUs (as in CDI mode):
| Setup | Status | Result |
|---|---|---|
| 2 of 8 GPUs, the host's primary GPU (lowest UUID) made unavailable inside the container, leaving one GPU that is not the primary | Fixed | Without the library, hevc_cuvid, h264_nvenc and hevc_nvenc failed and -hwaccel cuda reported CUDA_ERROR_NO_DEVICE and fell back to software decoding. With it, all of them worked. CUDA and nvidia-smi worked either way |
| 1 and 2 of 8 GPUs, including the primary (the bug does not show) | No regression | Every check passed with and without the library; in the 2-GPU container, both GPUs decoded and encoded with the library preloaded |
| Debug log of every run above | Confirmed | The lists from libcuda (0x201, 0x214, 0x288) and libnvcuvid (0x201) were filtered down to the container's GPUs, with membership from CARD_INFO |
| Earlier version (minors from /proc only), 8 or more single-GPU containers of the same kind | Used | Long real-world decoding runs |
Legacy mode: docker --gpus, the default nvidia-container-runtime, the GPU Operator's envvar strategy |
By design, not yet run on hardware | libnvidia-container (mount_procfs, mount_procfs_gpu) mounts a tmpfs over /proc/driver/nvidia and binds back only the container's GPUs; membership then comes from CARD_INFO |
| Several GPUs, none of them the primary; drivers other than 595.71.05 | Untested |
Tried it somewhere else? Please open an issue with your
driver version, container runtime and the output of bash tests/run_tests.sh. Successes help as much as failures.
To load it in every login shell, add this to ~/.bash_profile:
export LD_PRELOAD=/path/to/libnvcodec_container_fix.sossh host 'command' and tmux new-session 'command' do not read ~/.bash_profile; set LD_PRELOAD in the command
explicitly. An image that sets its own LD_PRELOAD overrides the one from ~/.bash_profile. To cover every process,
write the library's absolute path to /etc/ld.so.preload instead (write it again after the container restarts):
echo /path/to/libnvcodec_container_fix.so >> /etc/ld.so.preloadNVCODEC_CONTAINER_FIX_DEBUG=1: print every rewrite to stderrNVCODEC_CONTAINER_FIX_DISABLE=1: pass everything through unchanged
Needs x86-64 Linux, gcc, make, binutils (objdump) and glibc 2.35 or newer headers (for struct dl_find_object). The
built library needs only GLIBC_2.17, so it also loads into processes of older images.
make # builds libnvcodec_container_fix.so and checks that it needs no glibc symbol newer than GLIBC_2.17
make test # unit tests with a mocked driver, /proc and /dev; no GPU neededOn a GPU machine, tests/run_tests.sh runs the same checks without the library as a baseline, then with it preloaded:
bash tests/run_tests.sh
FFMPEG=/opt/ffmpeg/bin/ffmpeg PYTHON=/usr/bin/python3 bash tests/run_tests.sh # other binariesWhat the GPU test checks and needs
For each check the script prints the exit code and the last output lines: CUDA (tests/test_cuda.py), nvidia-smi,
and for each GPU ffmpeg NVDEC (hevc_cuvid, -hwaccel cuda) and NVENC (h264_nvenc, hevc_nvenc). Finally it prints
the library's debug log for one decode and one encode. The baseline is informational; the script exits non-zero if any
check fails with the library preloaded. It needs:
nvidia-smiPYTHON(defaultpython3): Python 3;tests/test_cuda.pyuses only the standard library (ctypes) and the driver'slibcuda.so.1FFMPEG(defaultffmpeg): an ffmpeg build with NVDEC/NVENC support (hevc_cuvid,-hwaccel cuda,h264_nvenc,hevc_nvenc). The HEVC test clip is generated from ffmpeg'stestsrcand encoded withlibx265if that ffmpeg has it, otherwise withhevc_nvencwith the library preloaded.
- A 32-bit process that loads this 64-bit library prints a
wrong ELF classwarning (it keeps running). - x86-64 only: the source pins
dlsymtoGLIBC_2.2.5, the x86-64 base symbol version. - The library depends on the layout of the driver structs listed in the source header (the same in 570.86.15,
580.65.06, 595.71.05 and 620.73.04). After a major driver upgrade, run
make testandtests/run_tests.shfirst. - Membership is decided by the
/dev/nvidia{minor}node. A device cgroup that denies an existing node is not seen. - Preloading does not reach statically linked programs, and the
LD_PRELOADvariable is ignored for setuid programs (/etc/ld.so.preloadis not).
No official fix is confirmed. In k8s-device-plugin#1282 (2026-06-08), an NVIDIA maintainer wrote that "610.x versions seem to have the fix" but had not tested it. Users report NVENC still failing on 610.43.03 and 610.57.04 (forum posts #11 and #13). nvidia-container-toolkit#1249 was closed because "a fix for this does not belong in NVIDIA Container Toolkit", and #1209 was closed as its duplicate. open-gpu-kernel-modules#826 is open.
Membership, CARD_INFO and the pass-through rules
The library intercepts ioctl. After the driver returns a GPU list on /dev/nvidiactl (RM controls
GET_ATTACHED_IDS 0x201, GET_PROBED_IDS 0x214 and GET_ACTIVE_DEVICE_IDS 0x288, sent directly or wrapped in
NV_ESC_IOCTL_XFER_CMD), it removes the GPUs that are certainly not in the container. Once the list holds only the
container's GPUs, the primary GPU is always one the container can open. Every GPU that is in the container is kept.
To decide which GPUs are in the container, the library asks the driver for each GPU's device minor with
NV_ESC_CARD_INFO on the same file descriptor. The driver reports each GPU under the same id that it puts in the
lists, so no /proc entry is needed. This matters because in legacy mode libnvidia-container hides the other GPUs'
/proc/driver/nvidia/gpus entries. A GPU is in the container if /dev/nvidia{minor} is the matching character
device (195:minor), and certainly not in it if that node is missing or is something else. For an id that CARD_INFO
does not list, or if CARD_INFO fails, the library falls back to the PCI address encoded in the id and the Device Minor
in /proc/driver/nvidia/gpus/<PCI>/information.
GPUs whose membership cannot be determined are kept. A list passes through unchanged unless at least one of the host's
GPUs is known to be in the container and at least one is known not to be (so bare metal, whole-machine containers and
CPU-only machines are untouched), and when filtering would leave it empty. So the library can be preloaded globally.
The hook uses only raw system calls and stack buffers, preserves errno and depends only on GLIBC_2.17 symbols. See
the header of nvcodec_container_fix.c for details.
References:
- NVIDIA open-gpu-kernel-modules 595.71.05:
nv_ioctl_card_info_t,nvidia_read_card_info,gpu_idassignment, ids in the RM lists,nvos.h(NVOS54_PARAMETERS),gpu.c(gpuGenerate32BitId) - libnvidia-container
nvc_mount.c - Issues: nvidia-container-toolkit#1249, #1209, k8s-device-plugin#1282, open-gpu-kernel-modules#826, NVIDIA forum thread
MIT, see LICENSE. Struct layouts and constants come from NVIDIA's open-gpu-kernel-modules 595.71.05 (dual MIT/GPL). They are interoperability facts; no code is copied.