docker中使用cuda、opengl、ros,支持rviz可视化

参考:
docker ubuntu1804 opengl 支持 pytorch
Using Hardware Acceleration with Docker

准备cuda和opengl环境

安装依赖工具

sudo apt-get install x11-xserver-utils

准备文件

mkdir cuda-opengl
cd cuda-opengl
touch Dockerfile
touch 10_nvidia.json
touch run.sh

Dockerfile如下:

ARG from
 
FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu18.04  as glvnd
FROM nvidia/cuda:11.2.2-cudnn8-devel-ubuntu18.04
 
ARG DEBIAN_FRONTEND=noninteractive

RUN apt update && apt install -y \
		curl \
		ca-certificates \
		pkg-config \
		vim

RUN apt autoclean && apt clean

COPY --from=glvnd /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu
COPY --from=glvnd /usr/lib/i386-linux-gnu /usr/lib/i386-linux-gnu
 
COPY 10_nvidia.json /usr/share/glvnd/egl_vendor.d/10_nvidia.json
 
RUN echo '/usr/lib/x86_64-linux-gnu' >> /etc/ld.so.conf.d/glvnd.conf && \
    echo '/usr/lib/i386-linux-gnu' >> /etc/ld.so.conf.d/glvnd.conf && \
    ldconfig
 
ENV LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu:/usr/lib/i386-linux-gnu${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
 
ARG DEBIAN_FRONTEND=noninteractive
ENV NVIDIA_VISIBLE_DEVICES \
    ${NVIDIA_VISIBLE_DEVICES:-all}
ENV NVIDIA_DRIVER_CAPABILITIES \
    ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics

10_nvidia.json如下:

{
    "file_format_version" : "1.0.0",
    "ICD" : {
        "library_path" : "libEGL_nvidia.so.0"
    }
}

run.sh如下:

XAUTH=/tmp/.docker.xauth
if [ ! -f $XAUTH ]
then
    xauth_list=$(xauth nlist :0 | sed -e 's/^..../ffff/')
    if [ ! -z "$xauth_list" ]
    then
        echo $xauth_list | xauth -f $XAUTH nmerge -
    else
        touch $XAUTH
    fi
    chmod a+r $XAUTH
fi

docker run -it --rm \
    --env="DISPLAY=$DISPLAY" \
    --env="QT_X11_NO_MITSHM=1" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --env="XAUTHORITY=$XAUTH" \
    --volume="$XAUTH:$XAUTH" \
    --runtime=nvidia \
    cuda-opengl:latest \
    bash

或者下面也可以用

#!/bin/bash
# set -e

IMAGE_NAME="cuda-opengl"
TAG_NAME="latest"
uid="$(id -u)"
gid="$(id -g)"
ENTRYPOINT="/bin/bash"

docker run -it --rm \
    -e USER=$USER \
    -e USER_ID=$uid \
    -e UID=$uid \
    -e GID=$gid \
    -v /etc/localtime:/etc/localtime:ro \
    --env="DISPLAY=$DISPLAY" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --net=host \
    --shm-size "2G" \
    --privileged \
    --runtime=nvidia \
    $IMAGE_NAME:$TAG_NAME \
    $ENTRYPOINT

具体版本根据需求定,我采用的ubuntu 18.04、 cuda11.2.2、 cudnn8、 libglvnd1.0.0
其他参考:
opengl dockerfile: gitlab
cuda docker: docker
opengl docker: docker

编译docker

# in cuda-opengl
docker build -t cuda-opengl .
# after compiled
chmod a+x run.sh
# config x11
xhost +
# run docker 
./run.sh

安装ros

在容器中安装ros,然后采用docker commit保存镜像
安装参考ROS Installation Options,选择要安装的版本,18.04对应melodic,安装ros-melodic-desktop-full后,可以测试支持rviz。

 roscore > /dev/null & rosrun rviz rviz

你可能感兴趣的:(linux,工具,ros)