diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a1a65ca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM nvidia/cuda:latest + +ENV NVIDIA_VISIBLE_DEVICES all +ENV NVIDIA_DRIVER_CAPABILITIES compute,video,utility + +RUN mkdir -p /usr/local/bin /patched-lib +COPY patch.sh docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/patch.sh /usr/local/bin/docker-entrypoint.sh + +ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] diff --git a/README.md b/README.md index 01335b9..16a57ba 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,17 @@ If something got broken you may restore patched driver from backup: bash ./patch.sh -r ``` +## Docker support + +It is possible to use this patch with nvidia-docker containers, even if host machine hasn't patched drivers. See `Dockerfile` for example. + +Essentially all you need to do during build is: + +* `COPY` the `patch.sh` and `docker-entrypoint.sh` files into your container. +* Make sure `docker-entrypoint.sh` is invoked on container start. + +`docker-entrypoint.sh` script does on-the-fly patching by means of manipulating dynamic linker to workaround read-only mount of Nvidia runtime. Finally it passes original docker command to shell, like if entrypoint was not restricted by `ENTRYPOINT` directive. So `docker run --runtime=nvidia -it mycontainer echo 123` will print `123`. Also it can be just invoked from your entrypoint script, if you have any. + ## See also * Plex Media Server: enable HW **decoding**: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..f70139a --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +echo "/patched-lib" > /etc/ld.so.conf.d/000-patched-lib.conf && \ +mkdir -p "/patched-lib" && \ +PATCH_OUTPUT_DIR=/patched-lib /usr/local/bin/patch.sh && \ +cd /patched-lib && \ +for f in * ; do + suffix="${f##*.so}" + name="$(basename "$f" "$suffix")" + [ -h "$name" ] || ln -sf "$f" "$name" + [ -h "$name" ] || ln -sf "$f" "$name.1" +done && \ +ldconfig +[ "$OLDPWD" ] && cd - +exec "$@" diff --git a/patch.sh b/patch.sh index 21eaa2b..f2c2bcd 100755 --- a/patch.sh +++ b/patch.sh @@ -95,7 +95,7 @@ declare -A object_list=( ["430.34"]='libnvcuvid.so' ) -NVIDIA_SMI="$(which nvidia-smi)" +NVIDIA_SMI="$(command -v nvidia-smi)" if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then echo 'Something went wrong. Check nvidia driver' @@ -151,8 +151,8 @@ else fi sha1sum "$backup_path/$object.$driver_version" sed "$patch" "$backup_path/$object.$driver_version" > \ - "$driver_dir/$object.$driver_version" - sha1sum "$driver_dir/$object.$driver_version" + "${PATCH_OUTPUT_DIR-$driver_dir}/$object.$driver_version" + sha1sum "${PATCH_OUTPUT_DIR-$driver_dir}/$object.$driver_version" ldconfig echo "Patched!" fi