Docker 기본 사용법 with NVIDIA CUDA OpenGL

ROS SOTA 비교군 알고리즘 세팅 [1/2]

2023-02-27

서론:

해당 포스팅에서는 다음에 대해 설명하고자 한다.

Docker 설치

$ curl -fsSL https://get.docker.com/ | sudo sh
$ sudo usermod -aG docker $USER

##### NVIDIA-Docker, GPU
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
   && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
   && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

$ sudo apt-get update
$ sudo apt-get install -y nvidia-docker2
$ sudo systemctl restart docker


Image 생성

FROM nvidia/cudagl:11.4.0-devel-ubuntu18.04

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES \
    ${NVIDIA_VISIBLE_DEVICES:-all}
ENV NVIDIA_DRIVER_CAPABILITIES \
    ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics

RUN apt-get update && apt-get -y install sudo

RUN adduser --disabled-password --gecos '' ubuntu
RUN adduser ubuntu sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

RUN mkdir -p /home/ubuntu
RUN chown ubuntu /home/ubuntu
USER ubuntu
WORKDIR /home/ubuntu
CMD /bin/bash
$ docker build -t IMG_NAME .
# IMG_NAME은 원하는 이미지 이름
$ docker images #image 생성 되었는지 확인


Docker container 생성 및 사용

#!/bin/sh
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -

docker run --runtime=nvidia --privileged --gpus all --rm -it \
           --volume=$XSOCK:$XSOCK:rw \
           --volume=$XAUTH:$XAUTH:rw \
           --volume=$HOME:$HOME \
           --shm-size=4gb \
           --env="XAUTHORITY=${XAUTH}" \
           --env="DISPLAY=${DISPLAY}" \
           --env=TERM=xterm-256color \
           --env=QT_X11_NO_MITSHM=1 \
           --env="NVIDIA_DRIVER_CAPABILITIES=all" \
           --net=host \
           <IMAGE_NAME>
           bash
$ chmod +x run.sh
$ ./run.sh

$ docker ps -a # container list 확인


기본 및 편의성 설정 및 error 해결

$ sudo apt install x11-xserver-utils
# 혹은 sudo apt install x11-server-utils
$ xhost +
$ sudo apt install bash-completion

$ sudo gedit /etc/bash.bashrc

## 주석해제
if ! shopt -oq posix; then
    if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
    fi
fi

$ source /etc/bash.bashrc
$ source ~/.bashrc

$ sudo rm /etc/apt/apt.conf.d/docker-clean
$ sudo apt update -y
$ sudo apt-get update && sudo apt-get install -y lsb-release && sudo apt-get clean all
$ sudo rm /etc/apt/sources.list.d/ros-latest.list
$ sudo apt install dbus-x11