이번에 연구실 서버를 세팅 하면서 삽질한 기록을 남겨봅니다.
먼저, 연구에 필요한 기능들을 정리했습니다.
필요한 기능
1.
PyTorch Latest Stable
•
공식 문서등을 참조하기 쉽도록 높은 버전의 PyTorch를 사용하고자 합니다.
2.
Python 3.10 or Higher
•
딥러닝 연구자들은 3.6이나 3.8을 선호하는 듯 하지만 저는 Python Type Hint를 좋아하기 때문에 높은 버전의 Python을 사용하길 원했습니다.
•
하지만 PyTorch가 Python 3.10을 Stable Support 하지 않아서 포기했습니다.
•
그래서 그냥 안정적인 Python 3.8.12을 사용하기로 했습니다.
•
Python 3.10 지원하는 PyTorch가 곧 Nightly가 나온다고 하던데 Stable이 나오면 판올림할 계획입니다.
3.
CUDA 11, cuDNN 8
4.
Jupyter Lab
•
Python 사용에 있어 Jupyter Ecosystems을 선호하지는 않지만 그래도 딥러닝 연구라면 있어야 할 것 같아서 Access 가능하게 하려고 합니다.
5.
code-server
•
vscode 를 광적으로 좋아하기 때문에, Web Based Access 라면 Jupyter Lab 보다는 code-server 를 사용하고자 했습니다.
6.
Remote Containers or Remote SSH
•
개인 PC 환경에서 접근하기 쉽도록 VSCODE Remote Containers나 Remote SSH로 접근 가능하게 하고싶었습니다.
•
Remote Containers를 사용한다면 Host 서버에 Docker API를 열고, Client 에서 Docker API로 연결 후 사용해야 할 것이고, Remote SSH를 사용한다면 Container 내에서 sshd를 열고 Host 로 전달하여 사용해야 할 것 입니다.
7.
Ubuntu 미러 서버 변경
•
apt 저장소를 한국 미러 서버 Mirror로 바꿔주어야 합니다. 아니면 속터집(...)
Why Docker?
파이썬 가상 환경으로 흔히 venv나 conda등을 사용합니다.
하지만 개인적으로 venv나 conda를 활용해서 가상 환경을 구성하는 것은 시간도 오래 걸리기도 하고 뭐만 하면 Dependency가 꼬여버리거나해서 선호하지 않습니다.
또한, 서버에서는 여러명이 사용하므로 Conda 보다는 Docker Container로 사용하는 것이 좋다고 생각했습니다.
결정적으로, 딥러닝 환경 설정은 버전과 드라이버등을 꼼꼼하게 맞춰주어야 하기 때문에 그냥 속 편하게 남이 해놓은 Docker 이미지를 쓰는게 좋을 것 같았습니다.
베이스 이미지
Deepo Repository를 활용하였습니다.
Deepo 프로젝트는 기본적인 딥러닝 환경 설정을 갖춘 상태에서 원하는 CUDA, cuDNN, Python Version 등을 커스터마이즈 할 수 있는 Docker Image 빌드 옵션을 제공합니다.
따라서 위의 요구 사항에 맞게 아래와 같은 명령어를 사용하였습니다.
git clone https://github.com/ufoym/deepo.git
cd deepo/generator
python generate.py Dockerfile pytorch jupyterlab python=3.9 --cuda-ver 11.1 --cudnn-ver 8
docker build -t idclab/deepo .
Shell
복사
명령어를 실행하면 Local에 idclab/deepo 라는 이미지가 생성되었을 것입니다.
추가 설정
위의 추가적인 요구사항을 만족하기 위해 Dockerfile 을 수정해줍니다.
sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list && \ #카카오 미러 서버로 수정
apt-get update && \ ##apt update
apt-get upgrade -y&& \ ##apt upgrade
apt-get install -y net-tools && \ ##net-tools 설치
curl -fsSL https://code-server.dev/install.sh | sh ##code-server 설치
Shell
복사
Dockerfile의 RUN 부분에 위의 코드를 삽입해줍니다.
최종적으로 완성된 Docker File 코드는 아래와 같습니다.
# ==================================================================
# module list
# ------------------------------------------------------------------
# python 3.8 (apt)
# pytorch latest (pip)
# ==================================================================
FROM nvidia/cuda:11.1-cudnn8-devel-ubuntu18.04
ENV LANG C.UTF-8
RUN APT_INSTALL="apt-get install -y --no-install-recommends" && \
PIP_INSTALL="python -m pip --no-cache-dir install --upgrade" && \
GIT_CLONE="git clone --depth 10" && \
rm -rf /var/lib/apt/lists/* \
/etc/apt/sources.list.d/cuda.list \
/etc/apt/sources.list.d/nvidia-ml.list && \
sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list && \
apt-get update && \
apt-get upgrade -y&& \
curl -fsSL https://code-server.dev/install.sh | sh \
# ==================================================================
# tools
# ------------------------------------------------------------------
DEBIAN_FRONTEND=noninteractive $APT_INSTALL \
build-essential \
apt-utils \
ca-certificates \
wget \
git \
vim \
libssl-dev \
curl \
unzip \
unrar \
cmake \
net-tools \
&& \
# ==================================================================
# python
# ------------------------------------------------------------------
DEBIAN_FRONTEND=noninteractive $APT_INSTALL \
software-properties-common \
&& \
add-apt-repository ppa:deadsnakes/ppa && \
apt-get update && \
DEBIAN_FRONTEND=noninteractive $APT_INSTALL \
python3.8 \
python3.8-dev \
python3-distutils-extra \
&& \
wget -O ~/get-pip.py \
https://bootstrap.pypa.io/get-pip.py && \
python3.8 ~/get-pip.py && \
ln -s /usr/bin/python3.8 /usr/local/bin/python3 && \
ln -s /usr/bin/python3.8 /usr/local/bin/python && \
$PIP_INSTALL \
setuptools \
&& \
$PIP_INSTALL \
numpy \
scipy \
pandas \
jupyterlab \
cloudpickle \
scikit-image>=0.14.2 \
scikit-learn \
matplotlib \
Cython \
tqdm \
&& \
# ==================================================================
# pytorch
# ------------------------------------------------------------------
$PIP_INSTALL \
future \
numpy \
protobuf \
enum34 \
pyyaml \
typing \
&& \
$PIP_INSTALL \
--pre torch torchvision torchaudio -f \
https://download.pytorch.org/whl/nightly/cu111/torch_nightly.html \
&& \
# ==================================================================
# config & cleanup
# ------------------------------------------------------------------
ldconfig && \
apt-get clean && \
apt-get autoremove && \
rm -rf /var/lib/apt/lists/* /tmp/* ~/*
Shell
복사
컨테이너 실행
docker run --gpus all -it -p 8888:8888 -v /home/u:/root --ipc=host ufoym/deepo jupyter lab --no-browser --ip=0.0.0.0 --allow-root --LabApp.allow_origin='*' --LabApp.root_dir='/root'
Shell
복사
주피터랩과 동시에 실행하는 코드입니다.
서버 접속
서버는 세가지 경로로 사용 예정입니다.
1.
SSH 접속 후 docker exec
2.
docker API 설정
a.
docker API를 설정해 클라이언트에서 TCP를 타고 서버의 Docker 명령을 실행할 수 있습니다.
b.
docker remote api로 검색하면 관련 아티클들이 나옵니다.
3.
Remote Containers
a.
Docker API와 비슷하게 사용하는 방법입니다.
b.
Client에 Docker를 설치하고 (저의 경우 맥이라 Docker Desktop을 설치하였습니다.)
c.
vscode 설정에서 docer.host를 아래와 같이 설정해줍니다.
d.
Remote-Containers : Attach to Running Containers 를 이용해 컨테이너에 연결해줍니다.