Posted Updated AI / JupyterLab17 minutes read (About 2519 words)
GPU 서버에 도커 이미지로 JupyterLab 배포하기
GPU서버에서 딥러닝모델 학습을 위해 JupyterLab을 도커파일로 만들어 실행하는 방법을 정리해보겠다.
JupyterLab 이미지 파일 만들기
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 사용할 기본 이미지 FROM python:3.8-slim # 작업 디렉토리 설정 WORKDIR /app # 필요한 패키지와 라이브러리 설치 RUN pip install --no-cache-dir jupyterlab numpy pandas matplotlib scipy # 포트 8888 열기 EXPOSE8888 # Jupyter Lab 실행 명령 CMD ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--allow-root", "--NotebookApp.token=''", "--NotebookApp.password=''"]
python:3.8-slim 이미지를 기반으로 사용.
/app 디렉토리를 작업 공간으로 설정.
pip를 이용해 jupyterlab을 설치.
컨테이너의 8888번 포트를 열어 외부에서 접근할 수 있게 함.
Jupyter Lab을 루트 권한으로 실행하며, 모든 IP에서 접근을 허용하고, 보안 토큰과 비밀번호를 비활성화.
이 설정은 ‘pip’이 PyPI의 메인 인덱스에서 패키지를 설치하도록 지시하고 SSL 인증서 검증 문제를 방지한다.
2. Dockerfile 작성
Dockerfile에서 ‘pip.conf’ 파일을 적절한 위치에 복사하는 단계를 포함한다. Python 이미지에서는 사용자 레벨의 설정을 적용하기 위해 ‘~/.pip/pip.conf’ 경로를 사용할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 기본 이미지 선택 FROM jupyter/base-notebook
# 작업 디렉토리 설정 WORKDIR /app
# pip.conf 파일 추가 COPY pip.conf /etc/pip.conf
# 필요한 Python 라이브러리 설치 RUN pip install --no-cache-dir numpy pandas matplotlib
# 포트 8888 열기 EXPOSE8888
# Jupyter Lab 실행 명령 CMD ["start-notebook.sh"]
‘jupyer/base-notebook’을 베이스 이미지로 사용한다.
‘pip.conf’ 파일을 Docker 이미지의 ‘/etc/pip.conf’로 복사한다. (시스템 전역 설정을 적용하기 위함. 필요에 따라 위치 변경 가능)
필요한 Python 패키지 설치
8888 포트를 열어 외부에서 접근가능하도록 함.
Jupyter Lab을 시작.
Jupyter notebook 이미지 Docker로 빌드 시 vim 텍스트 편집기 포함하기
Jupyter Notebook 이미지를 빌드할 때 vi와 vim 텍스트 편집기를 포함하려면 Dockerfile에 해당 패키지 설치 명령을 추가해야 한다. 대부분의 Linux 배포판에서 vim은 기본적으로 vi를 포함하고 있으며, 더 많은 기능을 제공한다. 아래의 지침은 vim을 설치하는 방법을 포함하고 있으며, 이를 통해 vi의 기능도 사용할 수 있다.
Dockerfile 작성
다음은 jupyter/base-notebook 이미지를 기반으로 하여 vim을 설치하는 Dockerfile 예시이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 기본 이미지 FROM jupyter/base-notebook
# 작업 디렉토리 설정 WORKDIR /app
# vim 설치 USER root RUN apt-get update && \ apt-get install -y vim
# 필요한 Python 라이브러리 설치 RUN pip install --no-cache-dir numpy pandas matplotlib
# Jupyter Lab 실행 명령 CMD ["start-notebook.sh"]
# 포트 8888 열기 EXPOSE8888
설명
이미지 선택: jupyter/base-notebook은 Jupyter Lab을 실행할 수 있게 미리 구성된 기본 이미지이다.
작업 디렉토리 설정: /app 디렉토리를 작업 디렉토리로 설정한다.
사용자 변경: root 사용자로 변경하여 시스템 레벨의 변경을 수행할 수 있다.
vim 설치: apt-get update를 실행하여 패키지 목록을 최신 상태로 유지하고, apt-get install -y vim을 사용하여 vim을 설치한다.
Python 라이브러리 설치: numpy, pandas, matplotlib와 같은 주요 Python 라이브러리를 설치한다.
포트 열기: 외부에서 Jupyter Lab에 접근할 수 있도록 8888 포트를 연다.
실행 명령: 컨테이너가 시작될 때 start-notebook.sh 스크립트를 실행하여 Jupyter Lab을 시작한다.
추가 팁
라이브러리 버전 지정: 특정 버전의 라이브러리가 필요한 경우, pip install 명령에 버전을 명시할 수 있습니다. 예: pip install numpy==1.18.5
requirements.txt 사용: 많은 수의 라이브러리를 관리해야 하는 경우, requirements.txt 파일을 만들고 그 안에 필요한 라이브러리와 버전을 목록화할 수 있습니다. 그리고 Dockerfile에서 이 파일을 참조하여 모든 라이브러리를 한 번에 설치할 수 있습니다.
1 2
COPY requirements.txt /app/ RUN pip install --no-cache-dir -r requirements.txt
$ sudo docker run --gpus all -it -d -p 59999:8888 -v /home/mydirectory/jupyter-notebook-test/:/home/work [이미지이름]:[태그] /bin/bash $ sudo docker exec -it 5f0cb0da5743 jupyter lab --allow-root --ip='0.0.0.0' [I 2024-05-09 04:56:38.457 ServerApp] jupyter_server_fileid | extension was successfully linked. [I 2024-05-09 04:56:38.461 ServerApp] jupyter_server_ydoc | extension was successfully linked. [I 2024-05-09 04:56:38.466 ServerApp] jupyterlab | extension was successfully linked. [I 2024-05-09 04:56:38.469 ServerApp] nbclassic | extension was successfully linked. [I 2024-05-09 04:56:38.642 ServerApp] notebook_shim | extension was successfully linked. [I 2024-05-09 04:56:38.655 ServerApp] notebook_shim | extension was successfully loaded. [I 2024-05-09 04:56:38.655 FileIdExtension] Configured File ID manager: ArbitraryFileIdManager [I 2024-05-09 04:56:38.655 FileIdExtension] ArbitraryFileIdManager : Configured root dir: / [I 2024-05-09 04:56:38.655 FileIdExtension] ArbitraryFileIdManager : Configured database path: /root/.local/share/jupyter/file_id_manager.db [I 2024-05-09 04:56:38.657 FileIdExtension] ArbitraryFileIdManager : Successfully connected to database file. [I 2024-05-09 04:56:38.657 FileIdExtension] ArbitraryFileIdManager : Creating File ID tables and indices with journal_mode = DELETE [I 2024-05-09 04:56:38.659 ServerApp] jupyter_server_fileid | extension was successfully loaded. [I 2024-05-09 04:56:38.660 ServerApp] jupyter_server_ydoc | extension was successfully loaded. [I 2024-05-09 04:56:38.664 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.7/site-packages/jupyterlab [I 2024-05-09 04:56:38.664 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab [I 2024-05-09 04:56:38.673 ServerApp] jupyterlab | extension was successfully loaded. _ _ _ _ | | | |_ __ __| |__ _| |_ ___ | |_| | '_ \/ _` / _` | _/ -_) \___/| .__/\__,_\__,_|\__\___| |_| Read the migration plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions. https://jupyter-notebook.readthedocs.io/en/latest/migrate_to_notebook7.html Please note that updating to Notebook 7 might break some of your extensions. [I 2024-05-09 04:56:38.682 ServerApp] nbclassic | extension was successfully loaded. [I 2024-05-09 04:56:38.683 ServerApp] Serving notebooks from local directory: / [I 2024-05-09 04:56:38.683 ServerApp] Jupyter Server 1.24.0 is running at: [I 2024-05-09 04:56:38.683 ServerApp] http://5f0cb0da5743:8888/lab?token=ba547f29f926ca0d1c1f03938f07afd1009839e97149705a [I 2024-05-09 04:56:38.684 ServerApp] or http://127.0.0.1:8888/lab?token=ba547f29f926ca0d1c1f03938f07afd1009839e97149705a [I 2024-05-09 04:56:38.684 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 2024-05-09 04:56:38.695 ServerApp] No web browser found: could not locate runnable browser. [C 2024-05-09 04:56:38.695 ServerApp] To access the server, open this file in a browser: file:///root/.local/share/jupyter/runtime/jpserver-15-open.html Or copy and paste one of these URLs: http://5f0cb0da5743:8888/lab?token=ba547f29f926ca0d1c1f03938f07afd1009839e97149705a or http://127.0.0.1:8888/lab?token=ba547f29f926ca0d1c1f03938f07afd1009839e97149705a [I 2024-05-09 04:56:50.087 ServerApp] 302 GET / (221.148.246.64) 1.40ms [I 2024-05-09 04:56:50.116 LabApp] 302 GET /lab? (221.148.246.64) 1.56ms [I 2024-05-09 04:57:04.161 ServerApp] 302 POST /login?next=%2Flab%3F (221.148.246.64) 6.76ms [W 2024-05-09 04:57:05.041 ServerApp] 404 GET /api/me?1715230246656 (221.148.246.64) 9.13ms referer=http://172.31.47.96:59999/lab? [W 2024-05-09 04:57:06.285 LabApp] Could not determine jupyterlab build status without nodejs [I 2024-05-09 04:57:28.961 ServerApp] 302 GET / (221.148.246.64) 1.07ms [W 2024-05-09 04:57:29.202 ServerApp] 404 GET /api/me?1715230270950 (221.148.246.64) 4.24ms referer=http://172.31.47.96:59999/lab? [W 2024-05-09 04:57:30.501 LabApp] Could not determine jupyterlab build status without nodejs [I 2024-05-09 05:02:43.977 ServerApp] 302 GET / (221.148.246.64) 1.02ms [W 2024-05-09 05:02:44.158 ServerApp] 404 GET /api/me?1715230585905 (221.148.246.64) 3.15ms referer=http://172.31.47.96:59999/lab? [W 2024-05-09 05:02:45.460 LabApp] Could not determine jupyterlab build status without nodejs
Jupyter Notebook 접속
[GPU 서버의 VirtualIP]:59999로 접속
기동시 나온 토큰 입력
JupyterLab 재기동하기
실행 중인 컨테이너 조회
1
sudo docker ps
실행 중인 컨테이너 중 59999포트로 실행 중인 JupyterLab 컨테이너를 종료
1
docker stop container_id_or_name
JupyterLab 이미지 실행
1
docker run --gpus all -it -d -p 59999:8888 -v /directory/jupyter-notebook-test/:/home/work [이미지이름]:[태그] /bin/bash
이미지 실행 되면 해당 컨테이너에서 jupyter lab 프로세스 실행
1 2 3 4 5 6 7 8 9 10 11 12 13
docker exec -it 5f0cb0da5743 jupyter lab --allow-root --ip='0.0.0.0' ~~~5. 실행 된 jupyter lab의 컨테이너 터미널 로그에 표시된 토큰을 이용하여 JupyterLab 접속
예시) ~~~bsh [C 2024-05-09 04:56:38.695 ServerApp] To access the server, open this file in a browser: file:///root/.local/share/jupyter/runtime/jpserver-15-open.html Or copy and paste one of these URLs: http://5f0cb0da5743:8888/lab?token=12f6cfeaabec704c03ef65ac230580440f7b32145c592698 or http://127.0.0.1:8888/lab?token=ba547f29f926ca0d1c1f03938f07afd10809839e97149705a
사용할 GPU의 수 설정하기
모든 GPU 사용: 컨테이너에 시스템의 모든 GPU를 할당하려면 all 키워드를 사용한다.
1
docker run --gpus all nvidia/cuda:10.0-base nvidia-smi
특정 수의 GPU 사용: 특정 수의 GPU만 할당하려면 count 키워드를 사용한다. 예를 들어, 사용 가능한 GPU 중 2개만 할당하려면 다음과 같이 한다.
1
docker run --gpus count=2 nvidia/cuda:10.0-base nvidia-smi
특정 GPU 사용: 특정 GPU를 지정하려면 device 키워드와 함께 GPU의 ID를 사용한다. 예를 들어, 첫 번째와 세 번째 GPU만 사용하려면 다음과 같이 명령한다.
1
docker run --gpus '"device=0,2"' nvidia/cuda:10.0-base nvidia-smi
주의 사항
–gpus 옵션을 사용하기 전에, NVIDIA GPU 드라이버와 NVIDIA Docker 플러그인(NVIDIA Container Toolkit)이 설치되어 있어야 한다.
이 옵션은 CUDA와 같은 GPU 가속 라이브러리를 사용하는 애플리케이션에 특히 유용하다.
Docker가 GPU를 사용하도록 설정하기 위해서는 Docker 엔진 설정에서 default-runtime을 nvidia로 설정하는 추가 설정이 필요할 수 있다.