0. 들어가며
- GPU 서버에서 Docker 기반의 개발 환경을 구축하는 방법
1. Docker container 생성
1.1. 서버 접속
- 환경 구축을 진행할 서버에 접속한 뒤 원하는 경로로 이동한다.
ssh -p {PORT} {USER}@{IP}
cd {DIRECTORY}
1.2. Dockerfile 작성
- Dockerfile을 작성한다.
- runtime vs devel: runtime은 가볍지만 nvcc가 설치되어있지 않다. 따라서 devel을 사용해야 한다.
- sending build context to Docker daemon에서 너무 많은 용량을 사용할 경우, 빈 폴더에 Dockerfile을 넣은 뒤 build한다.
vim Dockerfile
# base image
FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-devel
# install packages
RUN apt-get update -y && \
apt-get install -y openssh-server vim git wget unzip sshfs screen && \
apt-get install -y libopenmpi-dev libgl1-mesa-glx
# install python libraries
RUN pip install jupyterlab pytorch_lightning transformers datasets hydra-core wandb --no-cache-dir
# cleaning
RUN apt-get autoremove -y && \
apt-get clean && \
rm -rf /root/.cache
# edit ssh config
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
VOLUME /mnt
VOLUME /workspace
WORKDIR /workspace
EXPOSE 22 80 8000 8512 8888
RUN alias python=python3
RUN alias pip=/opt/conda/bin/pip
RUN alias conda=/opt/conda/bin/conda
CMD ["/bin/bash"]
1.3. Container 생성
shm-size=8G: 공유 메모리 크기 키우기
- windows의 경우에는
$PWD 대신 ${PWD} 사용
docker build -t my-image .
docker run -dit --gpus all --name my-container --privileged --shm-size=8G -p 22:22 -p 80:80 -p 8000:8000 -p 8512:8512 -p 8888:8888 -v $PWD:/workspace my-image bash
1.4. Container 접속
docker exec -it my-container bash
2. 주요 설정
2.1. SSH
- 접속에 사용할 root계정의 비밀번호를 만든다.
passwd root
service ssh start
2.2. Jupyter 실행
jupyter lab --generate-config
jupyter lab password
nohup jupyter lab --allow-root --ip 0.0.0.0 &