---
# System prepended metadata

title: Docker build for yolov7
tags: [QT]

---

###### tags: `QT`

# Docker build for yolov7

## Dockerfile build (3060)
```dockerfile=
FROM nvcr.io/nvidia/pytorch:22.05-py3

RUN su - && apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install sudo -y

# 安装必要的软件
RUN apt-get update && \
    sudo DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && \
    apt-get install -y git build-essential yasm cmake libtool libc6 libc6-dev unzip wget pkg-config

# 安装必要的依赖
RUN apt-get update && \
    apt-get install -y libssl-dev libx264-dev libx265-dev libnuma-dev libvpx-dev libfdk-aac-dev libmp3lame-dev libopus-dev
    
# 从宿主机拷贝所需的库文件
#COPY /lib64/libnvcuvid.so.1 /usr/lib/x86_64-linux-gnu/
#COPY /lib64/libnvidia-encode.so.1 /usr/lib/x86_64-linux-gnu/

# Install NVIDIA Video Codec SDK 12.0
# RUN sudo rm nvidia_video_sdk_12.zip

RUN sudo wget https://github.com/a5372935/updateExecutable/blob/main/cuviddec.h?raw=true -O cuviddec.h && \ 
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/nvEncodeAPI.h?raw=true -O nvEncodeAPI.h && \ 
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/nvcuvid.h?raw=true -O nvcuvid.h && \
    sudo cp cuviddec.h /usr/local/cuda/include/ && \
    sudo cp nvEncodeAPI.h /usr/local/cuda/include/ && \
    sudo cp nvcuvid.h /usr/local/cuda/include/ && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvcuvid.so?raw=true -O libnvcuvid.so && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvidia-encode.so?raw=true -O libnvidia-encode.so && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvcuvid.so.525.105.17?raw=true -O libnvcuvid.so.525.105.17 && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvidia-encode.so.525.105.17?raw=true -O libnvidia-encode.so.525.105.17 && \
    sudo cp libnvcuvid.so /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvidia-encode.so /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvcuvid.so.525.105.17 /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvidia-encode.so.525.105.17 /usr/lib/x86_64-linux-gnu/ && \
    export PATH=$PATH:/usr/local/cuda/bin && \
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/ && \
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/
    
RUN wget https://github.com/FFmpeg/nv-codec-headers/archive/refs/tags/n12.0.16.0.tar.gz && \
    tar -xzvf n12.0.16.0.tar.gz && \
    cd nv-codec-headers-n12.0.16.0 && \
    make && \
    sudo make install && \
    pkg-config --modversion ffnvcodec
    
# 配置 FFmpeg，并开启 NVIDIA 硬解码器
RUN wget https://ffmpeg.org/releases/ffmpeg-4.4.tar.gz && \
    tar -xzvf ffmpeg-4.4.tar.gz && \
    cd ffmpeg-4.4 && \
    ./configure --prefix=/usr/local/ffmpeg --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree \
    --enable-gpl --enable-libx264 --enable-libx265 --enable-libvpx --enable-libfdk-aac --enable-libmp3lame --enable-libopus && \
    make -j$(nproc) && \
    make install

# 设置环境变量
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:/usr/local/ffmpeg/lib"
ENV LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH

# 完成
WORKDIR /

RUN mkdir /home/developer && \
    useradd developer -p 123 && \
    echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
    chmod 0440 /etc/sudoers.d/developer && \
    chown developer:developer -R /home/developer

USER developer
ENV HOME /home/developer

# Essential library
RUN sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y libavdevice-dev libavcodec-dev libavformat-dev libswscale-dev libavresample-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libfaac-dev libmp3lame-dev libvorbis-dev libjpeg-dev libpng-dev libtiff-dev libopencore-amrnb-dev libopencore-amrwb-dev libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils libgtk-3-dev libtbb-dev libatlas-base-dev gfortran libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# Install OpenCV-4.5.4
RUN sudo mkdir /provision && \
	sudo chown developer:developer -R /provision && \
	cd /provision && \ 
	sudo wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.4.zip && \
	sudo wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.4.zip && \
	sudo unzip opencv.zip && \
	sudo unzip opencv_contrib.zip && \
	sudo mv opencv-4.5.4 opencv && \
	sudo mv opencv_contrib-4.5.4 opencv_contrib
	
RUN cd /usr/local/cuda/include/ && \ 
	ls
	
# Install zh-hant
RUN sudo DEBIAN_FRONTEND="noninteractive" apt-get install language-pack-zh-hant fonts-arphic-ukai fonts-arphic-uming fonts-ipafont-mincho fonts-ipafont-gothic fonts-unfonts-core -y

WORKDIR /provision

RUN sudo apt install software-properties-common -y
	
# 加入 PPA 並安裝 Qt 5.14.2 相關套件
RUN echo "deb https://ppa.launchpadcontent.net/beineri/opt-qt-5.14.2-focal/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/beineri.list && \
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C65D51784EDC19A871DBDBB710C56D0DE9977759 && \
    sudo apt update && \
    sudo apt install -y qt514-meta-full

RUN source /opt/qt514/bin/qt514-env.sh

RUN wget -O qtcreator.7z https://github.com/qt-creator/qt-creator/releases/download/v4.12.0/QtCreator-Linux-85580873.7z && \
	sudo apt-get install p7zip && \
	p7zip -d qtcreator.7z && \
	sudo chmod 777 ./bin/qtcreator

RUN sudo wget https://bootstrap.pypa.io/get-pip.py && \
	sudo python3 get-pip.py && \
	sudo pip install virtualenv virtualenvwrapper && \
	sudo rm -rf ~/get-pip.py ~/.cache/pip && \
	sudo bash -c 'echo "# virtualenv and virtualenv wrapper" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "export WORKON_HOME=/home/developer/.virtualenvs" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/developer/.bashrc' && \
	source ~/.bashrc && \
	mkvirtualenv opencv_cuda -p python3 && \
	pip install numpy && \
	cd /provision/opencv && \
	sudo mkdir build && \
	cd build && \
	sudo apt install cmake -y
	
RUN cd /provision/opencv/build && \ 
	sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D WITH_TBB=ON \
	-D BUILD_TBB=ON \
	-D WITH_FFMPEG=ON \
	-D ENABLE_FAST_MATH=1 \
	-D CUDA_FAST_MATH=1 \
	-D WITH_CUBLAS=1 \
	-D WITH_CUDA=ON \
	-D WITH_CUDEV=ON \
	-D WITH_GTK_2_X=ON \
	-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
	-D WITH_CUDNN=ON \
	-D OPENCV_DNN_CUDA=ON \
	-D CUDA_ARCH_BIN=8.6 \
	-D CUDA_ARCH_PTX=8.6 \
	-D WITH_V4L=ON \
	-D WITH_LIBV4L=ON \
	-D WITH_QT=ON \
	-D WITH_OPENGL=ON \
	-D WITH_GSTREAMER=ON \
	-D BUILD_opencv_cudacodec=ON \
	-D WUTH_NVCUVID=ON \
	-D NVCUVID_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvcuvid.so.525.105.17 \
	-D NVCUVID_INCLUDE_DIR=/usr/local/cuda/include \
	-D WITH_NVCUVENC=ON \
	-D NVENCODE_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvidia-encode.so.525.105.17 \
	-D NVENCODE_INCLUDE_DIR=/usr/local/cuda/include \
	-D OPENCV_GENERATE_PKGCONFIG=ON \
	-D OPENCV_PC_FILE_NAME=opencv.pc \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/opencv_cuda/lib/python3.8/site-packages \
	-D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python \
	-D OPENCV_EXTRA_MODULES_PATH=/provision/opencv_contrib/modules \
	-D INSTALL_PYTHON_EXAMPLES=OFF \
	-D INSTALL_C_EXAMPLES=OFF \
	-D WITH_IPP=OFF \
	-D BUILD_IPP_IW=OFF \
	-D WITH_LAPACK=OFF \
	-D WITH_EIGEN=OFF \
	-D WITH_ZLIB=ON \
	-D BUILD_ZLIB=ON \
	-D WITH_JPEG=ON \
	-D BUILD_JPEG=ON \
	-D WITH_PNG=ON \
	-D BUILD_PNG=ON \
	-D WITH_TIFF=ON \
	-D BUILD_TIFF=ON \
	-D BUILD_EXAMPLES=OFF ..

RUN cd /provision/opencv/build && \ 
	sudo make install
	
RUN sudo apt-get install libqt5serialport5-dev libqt5serialbus5-dev -y

RUN sudo apt install tesseract-ocr libtesseract-dev -y

RUN cd /provision/ && \
    git clone https://github.com/tesseract-ocr/tessdata.git
RUN sudo cp /provision/tessdata/chi_tra.traineddata /usr/share/tesseract-ocr/4.00/tessdata
RUN export TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00
RUN export TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00/tessdata

ENV PATH="${PATH}:/provision/bin"

# Set Timezone
ENV TZ="Asia/Taipei"

WORKDIR /root
```

## Dockerfile run

```shell=
#!/bin/sh
docker run --gpus all -it --rm --add-host host.docker.internal:host-gateway --network host -p 5679:1234 -v /tmp/.X11-unix:/tmp/.X11-unix -v /home/rd3:/root -e DISPLAY=$DISPLAY --cap-add=SYS_PTRACE --security-opt seccomp=unconfined jamespytorchtrt8 sudo /provision/bin/qtcreator 
```

## Dockerfile build (4060)
```dockerfile=
FROM nvcr.io/nvidia/pytorch:22.12-py3

RUN su - && apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install sudo -y

# 安装必要的软件
RUN apt-get update && \
    sudo DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && \
    apt-get install -y git build-essential yasm cmake libtool libc6 libc6-dev unzip wget pkg-config

# 安装必要的依赖
RUN apt-get update && \
    apt-get install -y libssl-dev libx264-dev libx265-dev libnuma-dev libvpx-dev libfdk-aac-dev libmp3lame-dev libopus-dev
    
# 从宿主机拷贝所需的库文件
#COPY /lib64/libnvcuvid.so.1 /usr/lib/x86_64-linux-gnu/
#COPY /lib64/libnvidia-encode.so.1 /usr/lib/x86_64-linux-gnu/

# Install NVIDIA Video Codec SDK 12.0
# RUN sudo rm nvidia_video_sdk_12.zip

RUN sudo wget https://github.com/a5372935/updateExecutable/blob/main/cuviddec.h?raw=true -O cuviddec.h && \ 
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/nvEncodeAPI.h?raw=true -O nvEncodeAPI.h && \ 
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/nvcuvid.h?raw=true -O nvcuvid.h && \
    sudo cp cuviddec.h /usr/local/cuda/include/ && \
    sudo cp nvEncodeAPI.h /usr/local/cuda/include/ && \
    sudo cp nvcuvid.h /usr/local/cuda/include/ && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvcuvid.so?raw=true -O libnvcuvid.so && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvidia-encode.so?raw=true -O libnvidia-encode.so && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvcuvid.so.525.105.17?raw=true -O libnvcuvid.so.525.105.17 && \
    sudo wget https://github.com/a5372935/updateExecutable/blob/main/libnvidia-encode.so.525.105.17?raw=true -O libnvidia-encode.so.525.105.17 && \
    sudo cp libnvcuvid.so /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvidia-encode.so /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvcuvid.so.525.105.17 /usr/lib/x86_64-linux-gnu/ && \
    sudo cp libnvidia-encode.so.525.105.17 /usr/lib/x86_64-linux-gnu/ && \
    export PATH=$PATH:/usr/local/cuda/bin && \
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/ && \
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/
    
RUN wget https://github.com/FFmpeg/nv-codec-headers/archive/refs/tags/n12.0.16.0.tar.gz && \
    tar -xzvf n12.0.16.0.tar.gz && \
    cd nv-codec-headers-n12.0.16.0 && \
    make && \
    sudo make install && \
    pkg-config --modversion ffnvcodec
    
# 配置 FFmpeg，并开启 NVIDIA 硬解码器
RUN wget https://ffmpeg.org/releases/ffmpeg-4.4.tar.gz && \
    tar -xzvf ffmpeg-4.4.tar.gz && \
    cd ffmpeg-4.4 && \
    ./configure --prefix=/usr/local/ffmpeg --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree \
    --enable-gpl --enable-libx264 --enable-libx265 --enable-libvpx --enable-libfdk-aac --enable-libmp3lame --enable-libopus && \
    make -j$(nproc) && \
    make install

# 设置环境变量
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:/usr/local/ffmpeg/lib"
ENV LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH

# 完成
WORKDIR /

RUN mkdir /home/developer && \
    useradd developer -p 123 && \
    echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
    chmod 0440 /etc/sudoers.d/developer && \
    chown developer:developer -R /home/developer

USER developer
ENV HOME /home/developer

# Essential library
RUN sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y libavdevice-dev libavcodec-dev libavformat-dev libswscale-dev libavresample-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libfaac-dev libmp3lame-dev libvorbis-dev libjpeg-dev libpng-dev libtiff-dev libopencore-amrnb-dev libopencore-amrwb-dev libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils libgtk-3-dev libtbb-dev libatlas-base-dev gfortran libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# Install OpenCV-4.7.0
RUN sudo mkdir /provision && \
	sudo chown developer:developer -R /provision && \
	cd /provision && \ 
	sudo wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.7.0.zip && \
	sudo wget -O opencv.zip https://github.com/opencv/opencv/archive/4.7.0.zip && \
	sudo unzip opencv.zip && \
	sudo unzip opencv_contrib.zip && \
	sudo mv opencv-4.7.0 opencv && \
	sudo mv opencv_contrib-4.7.0 opencv_contrib
	
RUN cd /usr/local/cuda/include/ && \ 
	ls
	
# Install zh-hant
RUN sudo DEBIAN_FRONTEND="noninteractive" apt-get install language-pack-zh-hant fonts-arphic-ukai fonts-arphic-uming fonts-ipafont-mincho fonts-ipafont-gothic fonts-unfonts-core -y

WORKDIR /provision

RUN sudo apt install software-properties-common -y

# 加入 PPA 並安裝 Qt 5.14.2 相關套件
RUN echo "deb https://ppa.launchpadcontent.net/beineri/opt-qt-5.14.2-focal/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/beineri.list && \
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C65D51784EDC19A871DBDBB710C56D0DE9977759 && \
    sudo apt update && \
    sudo apt install -y qt514-meta-full

RUN source /opt/qt514/bin/qt514-env.sh

RUN wget -O qtcreator.7z https://github.com/qt-creator/qt-creator/releases/download/v4.12.0/QtCreator-Linux-85580873.7z && \
	sudo apt-get install p7zip && \
	p7zip -d qtcreator.7z && \
	sudo chmod 777 ./bin/qtcreator

RUN sudo wget https://bootstrap.pypa.io/get-pip.py && \
	sudo python3 get-pip.py && \
	sudo pip install virtualenv virtualenvwrapper && \
	sudo rm -rf ~/get-pip.py ~/.cache/pip && \
	sudo bash -c 'echo "# virtualenv and virtualenv wrapper" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "export WORKON_HOME=/home/developer/.virtualenvs" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> /home/developer/.bashrc' && \
	sudo bash -c 'echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/developer/.bashrc' && \
	source ~/.bashrc && \
	mkvirtualenv opencv_cuda -p python3 && \
	pip install numpy && \
	cd /provision/opencv && \
	sudo mkdir build && \
	cd build && \
	sudo apt install cmake -y
	
RUN cd /provision/opencv/build && \ 
	sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D WITH_TBB=ON \
	-D BUILD_TBB=ON \
	-D WITH_FFMPEG=ON \
	-D ENABLE_FAST_MATH=1 \
	-D CUDA_FAST_MATH=1 \
	-D WITH_CUBLAS=1 \
	-D WITH_CUDA=ON \
	-D WITH_CUDEV=ON \
	-D WITH_GTK_2_X=ON \
	-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
	-D CUDA_CUDA_LIBRARY=/usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudart.so \
	-D WITH_CUDNN=ON \
	-D OPENCV_DNN_CUDA=ON \
	-D CUDA_ARCH_BIN='8.9' \
	-D WITH_V4L=ON \
	-D WITH_LIBV4L=ON \
	-D WITH_QT=OFF \
	-D WITH_OPENGL=ON \
	-D WITH_GSTREAMER=ON \
	-D BUILD_opencv_cudacodec=OFF \
	-D WUTH_NVCUVID=ON \
	-D NVCUVID_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvcuvid.so.525.105.17 \
	-D NVCUVID_INCLUDE_DIR=/usr/local/cuda/include \
	-D WITH_NVCUVENC=ON \
	-D NVENCODE_LIBRARY=/usr/lib/x86_64-linux-gnu/libnvidia-encode.so.525.105.17 \
	-D NVENCODE_INCLUDE_DIR=/usr/local/cuda/include \
	-D OPENCV_GENERATE_PKGCONFIG=ON \
	-D OPENCV_PC_FILE_NAME=opencv.pc \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/opencv_cuda/lib/python3.8/site-packages \
	-D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python \
	-D OPENCV_EXTRA_MODULES_PATH=/provision/opencv_contrib/modules \
	-D INSTALL_PYTHON_EXAMPLES=OFF \
	-D INSTALL_C_EXAMPLES=OFF \
	-D WITH_IPP=OFF \
	-D BUILD_IPP_IW=OFF \
	-D WITH_LAPACK=OFF \
	-D WITH_EIGEN=OFF \
	-D WITH_ZLIB=ON \
	-D BUILD_ZLIB=ON \
	-D WITH_JPEG=ON \
	-D BUILD_JPEG=ON \
	-D WITH_PNG=ON \
	-D BUILD_PNG=ON \
	-D WITH_TIFF=ON \
	-D BUILD_TIFF=ON \
	-D BUILD_EXAMPLES=OFF ..

RUN cd /provision/opencv/build && \ 
	sudo make install
	
RUN sudo apt-get install libqt5serialport5-dev libqt5serialbus5-dev -y

RUN sudo apt install tesseract-ocr libtesseract-dev -y

RUN cd /provision/ && \
    git clone https://github.com/tesseract-ocr/tessdata.git
RUN sudo cp /provision/tessdata/chi_tra.traineddata /usr/share/tesseract-ocr/4.00/tessdata
RUN export TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00
RUN export TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00/tessdata

ENV PATH="${PATH}:/provision/bin"

# Set Timezone
ENV TZ="Asia/Taipei"

WORKDIR /root
```

## Dockerfile image Build
```bash=
sudo docker build -t [image name] ./
```
:::danger
image名稱必須是小寫
:::


```
1. --gpus all：讓容器能夠使用所有可用的 NVIDIA GPU。
2. -it：讓 Docker 在互動模式下運行容器，以便可以在終端機上輸入指令並查看結果。
3. --rm：當容器停止運行後自動刪除容器。
4. --add-host host.docker.internal:host-gateway：將 host.docker.internal 解析為宿主機的 IP 位址。
5. -v /tmp/.X11-unix:/tmp/.X11-unix：將 X Window System 的 UNIX 套接字掛載到容器內的 /tmp/.X11-unix 目錄，從而允許容器中的 GUI 應用程序顯示在宿主機上。
6. -v /home/a8:/root：將宿主機上的 /home/a8 目錄掛載到容器內的 /root 目錄。
7. -e DISPLAY=$DISPLAY：設定容器內的 DISPLAY 環境變數為宿主機的 DISPLAY 環境變數，從而允許 GUI 應用程序顯示在宿主機上。
8. --privileged：讓容器內的進程可以獲得宿主機的特權。
9. --cap-add=SYS_PTRACE：向容器授予對 ptrace() 系統調用的許可權，從而使容器內的進程能夠進行調試。
10. --net=host：使用宿主機的網路命名空間，從而讓容器能夠訪問宿主機上的網路接口。
11. --security-opt seccomp=unconfined：禁用容器的 Seccomp 設定，從而讓容器內的進程能夠使用所有的系統調用。
```

## 錯誤排除

:::danger
=============
== PyTorch ==
=============

NVIDIA Release 22.05 (build 37432893)
PyTorch Version 1.12.0a0+8a1a93a

Container image Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

Copyright (c) 2014-2022 Facebook Inc.
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies    (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU                      (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006      Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
Copyright (c) 2015      Google Inc.
Copyright (c) 2015      Yangqing Jia
Copyright (c) 2013-2016 The Caffe contributors
All rights reserved.

Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES.  All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license

NOTE: The SHMEM allocation limit is set to the default of 64MB.  This may be
   insufficient for PyTorch.  NVIDIA recommends the use of the following flags:
   docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 ...

No protocol specified
qt.qpa.xcb: could not connect to display :1
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
:::
:::success
如果您在主机上运行了X server，则需要在主机上启用X11转发。您可以在启动容器时使用"-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix"选项，这将将主机上的X11 socket映射到容器内的/tmp/.X11-unix目录中，并将DISPLAY设置为":0"。请确保您的主机上已安装xauth软件包，并已将其添加到X11转发配置中。您可以运行以下命令来检查是否已正确配置X11转发：


```bash=
xhost +
```

**paste to host terminal**

如果您看到"access control disabled, clients can connect from any host"的消息，则表示X11转发已正确配置。
:::

:::danger
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error running hook #0: error running hook: exit status 1, stdout: , stderr: Auto-detected mode as 'legacy' nvidia-container-cli: initialization error: driver rpc error: timed out: unknown.
:::

:::success
解决方案 使用root权限执行如下命令： 
```bash=
nvidia-smi -pm ENABLED
```
:::

:::danger
docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].
:::

## 安装NVIDIA Container Toolkit
:::success
创建包仓库和GPG key:
```bash=
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
       && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
       && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
             sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
             sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
```
更新包列表之后安装 nvidia-docker2 包（以及依赖）
```bash=
sudo apt-get update
sudo apt-get install -y nvidia-docker2
```
重启Docker daemon 来完成设定默认运行时后的安装。
```bash=
sudo systemctl restart docker
```
最后再次创建容器指定 --gpus all ，问题解决，容器创建成功