利用dockfile文件创建一个TensorRT环境的容器

1.创建dockerfile文件内容如下:

# For more detail, check NGC
FROM nvcr.io/nvidia/tensorrt:21.12-py3   

#设置时区环境
ENV TZ=Asia/Shanghai
#设置容器用户名称,最好和主机用户名称一样  
ARG user=jiqiang

# Set timezone in case of interation during installation
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# install packages for opencv
RUN apt-get update
RUN apt install -y --no-install-recommends \
  build-essential cmake git pkg-config libgtk-3-dev \
  libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev \
  libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev \
  python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev \
  build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev \
  libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev \
  libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev \
  python3-numpy libtbb2 libtbb-dev libdc1394-22-dev 
RUN apt-get -y clean && rm -rf /var/lib/apt/lists/*

#Build opencv4.5.5
RUN mkdir /root/opencv_build
WORKDIR /root/opencv_build/
RUN git clone https://github.com/opencv/opencv.git && git clone https://github.com/opencv/opencv_contrib.git && \
  cd /root/opencv_build/opencv && git checkout 4.5.5 && \
  cd /root/opencv_build/opencv_contrib && git checkout 4.5.5 && \
  cd /root/opencv_build/opencv && mkdir build

WORKDIR /root/opencv_build/opencv/build
RUN cmake -D OPENCV_GENERATE_PKGCONFIG=ON \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
  -D WITH_GSTREAMER=ON \
  -D WITH_LIBV4L=ON \
  -D BUILD_opencv_python2=ON \
  -D BUILD_opencv_python3=ON \
  -D BUILD_TESTS=OFF \
  -D BUILD_PERF_TESTS=OFF \
  -D BUILD_EXAMPLES=OFF \
  -D CMAKE_BUILD_TYPE=RELEASE \
  -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
  -D CMAKE_INSTALL_PREFIX=/usr/local ..
RUN make -j16 && make install
WORKDIR /root
RUN rm -rf opencv_build

# install packages that are used for trt inferences
RUN apt-get update && apt install -y --no-install-recommends \
  sudo libgflags-dev libboost-dev libxml2-dev \
  libyaml-cpp-dev sqlite3 libsqlite3-dev libboost-all-dev \
  fish lsb-release peco feh fim openssh-server tmux curl
RUN apt-get -y clean && rm -rf /var/lib/apt/lists/*

# install netron to show model structure
RUN pip install netron

# set up fish shell and exa
RUN curl -Lo exa.zip "https://github.com/ogham/exa/releases/latest/download/exa-linux-x86_64-v0.10.1.zip" && \
  sudo unzip -q exa.zip bin/exa -d /usr/local && rm exa.zip


# set userinfo 如果不设置则默认是root,最后一行是让用户sudo不需要密码
RUN useradd -rm -c ${user} -u 1000 -d /home/${user} -s /bin/bash -G sudo ${user}
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN echo "$user   ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

RUN apt-get update

# copy dotfiles 将宿主机中的文件copy到容器里
COPY my_dot_files/tmux/* /home/${user}/
COPY my_dot_files/fish/ /home/${user}/.config/fish

# set working directory of trt webinar 
#创建用户默认文件夹
RUN mkdir -p /home/${user}/workspace/   
#更改一下文件夹权限
RUN chown -R ${user}:users /home/${user}

# ssh setting 每次启动容器ssh服务端打开
RUN systemctl enable ssh

# set default working directory and user 设置默认工作路径和用户
WORKDIR /home/${user}
USER ${user}

2.在此目录下运行指令创建docker镜像:

sudo docker build -t trt_00:cuda11.5-cudnn8.3-tensorrt8.2_${1} .
  • docker build: 这是构建 Docker 镜像的命令。
  • -t trt_00:cuda11.5-cudnn8.3-tensorrt8.2_${1}: 指定镜像的名称和标签,trt_00是名称,cuda11.5-cudnn8.3-tensorrt8.2_${1}是标签${1} 是一个变量,表示在构建过程中传递给命令的参数。
  • . 表示当前目录,即在当前目录下查找 Dockerfile 文件进行构建。

3.利用编译好的镜像创建容器并运行

docker run  -it \
    --gpus all \
    --name <创建的容器名称>\
    -v /tmp/.X11-unix:/tmp/.X11-unix:rw \
    -p 8090:22 \
    -e DISPLAY=$DISPLAY \
    -v ${PWD}:${PWD} \
    -v ${HOME}/Dataset:${HOME}/Dataset \
    -w ${PWD} \
    <镜像> \
    bash

-p 8090:22 : -p表示开放端口 主机端端口和容器端端口进行一个绑定。8090是主机端端口,器内的端口号为 22,通常用于 SSH(Secure Shell)连接。

-v /tmp/.X11-unix:/tmp/.X11-unix:rw \ :能从主机终端中打开容器中的GUI操作。

注:可能会出现docker gpu报错Error response from daemon: could not select device driver ““ with capabilities: [[gpu]]

解决方案:docker gpu报错Error response from daemon: could not select device driver ““ with capabilities: [[gpu]]_Adenialzz的博客-CSDN博客

4.对创建好的容器换源

打开/etc/apt/sources.list

vim /etc/apt/sources.list

更换内容(阿里源)如下:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

注:TensorRT目录被放在了/opt/tensorrt/目录下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jiqiang_z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值