Computer Science/Jupyter

[Windows] Docker를 활용하여 로컬에서 JupyterLab 실행하기

2021. 10. 2. 18:06

Jupyter Docker Stacks는 Jupyter applications와 interactive computing tools를 포함한 Docker images 모음이다 [1]. 즉, Docker image가 Jupyter 프로그램 및 유용한 도구들을 모두 포함하고 있으므로, image를 다운받아서 로컬에서 실행하면 Jupyter 프로그램 및 유용한 도구들을 일일이 따로 설치할 필요 없이 한번에 사용할 수 있다. 또한 리눅스 시스템도 사용이 가능하다. 참고로 image를 실행한 상태를 container라고 한다.
이 포스팅에서는 Windows 10에서 Docker Desktop을 설치하고, Jupyter Docker Stacks에서 원하는 image를 골라 다운로드한 후 실행하는 과정을 소개하고자 한다.

Docker Desktop 설치

Unix 환경 뿐 아니라 Windows에서도 Docker를 사용할 수 있다.

 

Docker Desktop for Mac and Windows | Docker

Learn why Docker Desktop is the preferred choice for millions of developers building containerized applications. Download for Mac or Windows.

www.docker.com

위 링크에서 설치파일을 다운로드한 후 프로그램을 설치한다.

프로그램을 처음 실행하면 관련 정책을 표시해주는데, 동의하고 진행한다.
이때 다음과 같은 에러가 날 수 있다.

https://aka.ms/wsl2kernel에 들어가서 'x64 머신용 최신 WSL2 Linux 커널 업데이트 패키지'를 다운받아 설치한다. 컴퓨터를 재부팅하고 나면 Docker Desktop이 제대로 동작하는 것을 확인할 수 있다.

Docker가 돌아가는 상태라면 Docker 명령어를 사용할 수 있다. 명령 프롬프트나 Windows PowerShell, Git bash 등을 열어 아래 명령어를 입력하면 help page가 나오는 것을 확인할 수 있다.

$ docker

WIndows에서 Docker 설치가 완료됐다.

Docker image 다운로드

Docker 명령어를 활용하여 Docker image를 다운 받을 수 있다.
다음 페이지에 Jupyter team이 제공하는 여러 Docker images에 대한 간략한 설명이 나오는데 이중 원하는 것을 선택해서 다운 받으면 된다.

 

Selecting an Image — docker-stacks latest documentation

Selecting an Image Using one of the Jupyter Docker Stacks requires two choices: Which Docker image you wish to use How you wish to start Docker containers from that image This section provides details about the first. Core Stacks The Jupyter team maintains

jupyter-docker-stacks.readthedocs.io

이때 한 image에도 여러 버전이 존재하는데 이는 tag로 구분된다. 다운로드 가능한 버전의 종류는 Docker Hub에서 확인할 수 있다. 예를 들어, "jupyter/datascience-notebook"를 사용하기로 결정했다면 그 중에서 원하는 버전을 아래에서 찾아 다운로드한다.

 

Docker Hub

 

hub.docker.com

나는 jupyter/datascience-notebook:latest을 다운로드 받고자 명령 프롬프트에서 아래 명령어를 입력했다
(image 이름과 tag가 :으로 연결되는데 최신 버전을 받고자 latest tag를 선택했다).

$ docker pull jupyter/datascience-notebook:latest

# 다운받은 image 확인
$ docker images
REPOSITORY                     TAG       IMAGE ID       CREATED      SIZE
jupyter/datascience-notebook   latest    66e13b80b867   5 days ago   4.07GB

 

Docker image 실행 (Container 생성)

$ docker run -p 8888:8888 --name jupyter -e JUPYTER_ENABLE_LAB=yes -e GRANT_SUDO=yes --user root -v [Local 내 작업 Path]:/home/jovyan jupyter/datascience-notebook:latest

# 유용한 명령어
$ docker images # 다운받은 image 확인
$ docker ps # 실행 중인 컨테이너 확인
$ docker ps -a # 종료된 컨테이너 확인
$ docker stop jupyter # 컨테이너 종료
$ docker rm jupyter # 컨테이너 삭제
$ docker rmi jupyter/datasciene-notebook:latest # 이미지 삭제

Docker run parameters

  • --name: container의 이름을 정해주는 것으로 생략 시 random한 이름이 부여된다.
  • -e JUPYTER_ENABLE_LAB=yes: Jupyter Notebook이 아닌 JupyterLab을 실행해준다.
  • -e GRANT_SUDO=yes --user root: 기본으로 사용하는 user 이름인 jovyan에 sudo 권한을 부여한다. 프로그램 등을 설치할 때 유용하다 [4].
  • -v [Local 내 작업 Path]:/home/jovyan/work: 기본적으로 container를 실행한 후 파일을 저장하면 container 내에만 파일이 저장된다. 즉, 로컬에서 container의 파일에 접근할 수 없고, 반대로 container에서 로컬 파일에 접근할 수 없다. 이를 방지하기 위해 폴더를 mount할 수 있는데 위 명령어를 입력하면 container의 /home/jovyan/work와 [Local 내 작업 Path]를 연결할 수 있다.
  • -i: (참고) interactive 입출력
  • -t: (참고) 가상 터미널 생성
  • --rm: (참고) docker run 실행 후 컨테이너 삭제


Container를 생성할 때 나오는 로그 마지막의 URL을 Chrome 등의 브라우저에 복사하면 JupyterLab이 실행된다. 이 URL에는 token이 포함된 값으로 이를 통해 인증을 할 수 있다. 한번 인증을 하고 나면 token 없이 000.0.0.0:8888/lab까지만 입력해도 접속이 가능한 것 같았다.
이상한 점은 CTRL+C를 입력해도 JupyterLab이 종료되지 않았다. 그래서 명령 프롬프트 창을 아예 닫은 후 확인해봤는데 여전히 background에서 JupyterLab이 돌아가고 있었다 (docker ps로 확인 가능). 그래서 docker stop jupyter로 container를 종료하였다 (docker ps -a로 확인 가능). 혹은 컴퓨터 전원을 내리면 container가 자동으로 종료된다.
Container를 다시 실행하고 싶다면 docker start jupyter를 입력한다. 그러면 따로 로그가 출력되지 않기 때문에 token을 확인할 수 없지만, 미리 인증을 했으므로 000.0.0.0:8888/lab까지만 입력해도 접속이 가능하다. 만약 token을 다시 확인하고 싶다면 docker start -i jupyter를 입력하여 로그를 통해 token을 확인할 수 있다.

# Container 시작
$ docker start jupyter

# Token을 확인하고자 할 때,
$ docker start -i jupyter

 

혹은 Docker Desktop을 통해서 빠르게 JupyterLab을 띄우고 접속할 수 있다.

만들어둔 종료된 container를 실행시켜 준다.

OPEN IN BROWSER를 누르면 바로 접속창이 뜨는데, Token을 모르는 상황이다. 접속창에서 어떻게 해야하는지 친절히 설명해주고 있다.

먼저 Token을 확인하는 방법은 CLI를 누르고 $ jupyter lab list 를 입력하면 확인할 수 있다 [2].

혹은 Token을 이용하여 password를 설정하면 이후에 password를 통해서도 로그인이 가능하다.

 

만약 password를 설정하였는데 잊었다면 쉽게 다시 설정 가능하다 [3].

 

참고

OS 확인

$ awk -F= '/^NAME/{print $2}' /etc/os-release
"Ubuntu"
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

Java 설치

Java를 설치하려는데 "E: Unable to locate package default-jre" 등의 에러가 나서 검색을 해봤더니 apt 관련 파일을 수정하고 해야 했었다 [5].

$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
$ sudo vi /etc/apt/sources.list
# 끝에 아래 문장 추가
deb http://ca.archive.ubuntu.com/ubuntu/ bionic main restricted universe multiverse
deb http://ca.archive.ubuntu.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://ca.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu bionic-security main restricted universe multiverse

deb-src http://ca.archive.ubuntu.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted universe multiverse
deb-src http://ca.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://ca.archive.ubuntu.com/ubuntu/ bionic-updates main restricted universe multiverse

$ sudo apt update
$ sudo apt install default-jre

$ java --version
openjdk 11.0.17 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu220.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu220.04, mixed mode, sharing)

 

 

Reference

  1. https://jupyter-docker-stacks.readthedocs.io/en/latest/
  2. https://stackoverflow.com/questions/46551551/list-running-jupyter-notebooks-and-tokens
  3. https://stackoverflow.com/questions/36312372/change-jupyter-notebook-server-password
  4. https://github.com/kubeflow/kubeflow/issues/425
  5. https://unix.stackexchange.com/questions/535633/cannot-install-default-jre-package-default-jre-has-no-installation-candidate

 

 

728x90
반응형