본문 바로가기

docker

[docker] jupyter-notebook 이미지를 이용한 jupyterlab 실행

파이썬 개발 환경을 고민하던 중 notebook의 필요성과 함께 jupyterlab을 사용해보고 싶어졌다.

검색을 하다 보니 아래 사이트의 링크를 활용하면 쉽게 사용할 수 있음을 알게 되었다.

 

https://jupyter-docker-stacks.readthedocs.io/en/latest/using/selecting.html#selecting-an-image

 

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

 

해당 사이트에서는 다양한 형태의 jupyter image를 제공하고 있다.

기본 base-notebook 부터 all-spark-notebook 까지 대략 8개의 이미지를 제공하고 있다.

최근 빅데이터/AI 분야에서 python, R, Spark 등 다양한 형태에 대해 각 특성있게 구성되어 있다고 생각이들며,

도커 이미지의 특성상 경량화한 이미지를 선호한다.

내부 작업에 비해 Docker Image 사이즈는 많은 사용하므로 요건에 맞는 이미지 선택이 필요하다

 

나의 경우에는 Python 수집/저장/분석에 관련된 테스트를 진행할 예정으로 datascience-notebook을 선택하여 작업을 진행한다.

 

1. datascience-notebook 이미지 다운

https://hub.docker.com/r/jupyter/datascience-notebook

 

# docker pull jupyter/base-notebook
Using default tag: latest
Trying to pull repository docker.io/jupyter/base-notebook ...
latest: Pulling from docker.io/jupyter/base-notebook
345e3491a907: Already exists
57671312ef6f: Already exists
5e9250ddb7d0: Already exists
6ce2d9f93aa7: Pull complete
61cbf5540866: Pull complete
4f4fb700ef54: Pull complete
28e27cd5a2e1: Pull complete
7653365676df: Pull complete
c1cba7fbe642: Pull complete
225953c5c717: Pull complete
4e326e4e67be: Pull complete
20e09afa3f7f: Pull complete
326366c8a5e0: Pull complete
630dab2ecb70: Pull complete
Digest: sha256:06e71f04f418a46853ba1296354b3cb82f42cee29c365ee164153664b08af71a
Status: Downloaded newer image for docker.io/jupyter/base-notebook:latest

# docker images
REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
docker.io/jupyter/datascience-notebook   latest              5edce2cf3535        2 hours ago         3.94 GB

외부망과 연결된 상태로 도커 서비스가 정상적으로 수행되고 있다면, dockerhub를 통해 해당 이미지를 다운 받을 수 있다

 

다운받은 이미지는 docker images 명령어를 통해 확인할 수 있다.

 

2. jupyter docker 실행 및 jupyterlab 실행

 

# docker run -it --rm -p 49999:8888 -d jupyter/datascience-notebook start.sh jupyter lab
485670dd1edbc076907ab26b4bac12219187b2444fa7e84a78f168829efe0833

# docker ps -a
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS                      PORTS                     NAMES
485670dd1edb        jupyter/datascience-notebook             "tini -g -- start...."   4 seconds ago       Up 3 seconds                0.0.0.0:49999->8888/tcp   upbeat_lamport
3ea12f9f1966        docker.io/jupyter/datascience-notebook   "tini -g -- start-..."   6 minutes ago       Exited (0) 36 seconds ago                             boring_goldberg

# docker logs 485670dd1edb
Executing the command: jupyter lab
[I 2021-05-19 14:03:34.019 ServerApp] jupyterlab | extension was successfully linked.
[W 2021-05-19 14:03:34.026 NotebookApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-05-19 14:03:34.026 NotebookApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-05-19 14:03:34.026 NotebookApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[I 2021-05-19 14:03:34.041 ServerApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/jupyter_cookie_secret
[I 2021-05-19 14:03:34.351 ServerApp] nbclassic | extension was successfully linked.
[I 2021-05-19 14:03:34.389 LabApp] JupyterLab extension loaded from /opt/conda/lib/python3.9/site-packages/jupyterlab
[I 2021-05-19 14:03:34.389 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 2021-05-19 14:03:34.395 ServerApp] jupyterlab | extension was successfully loaded.
[I 2021-05-19 14:03:34.402 ServerApp] nbclassic | extension was successfully loaded.
[I 2021-05-19 14:03:34.405 ServerApp] Serving notebooks from local directory: /home/jovyan
[I 2021-05-19 14:03:34.405 ServerApp] Jupyter Server 1.7.0 is running at:
[I 2021-05-19 14:03:34.405 ServerApp] http://485670dd1edb:8888/lab?token=b5a9551e723de0ef5449cf0f924e43424f569695a5cb64b7
[I 2021-05-19 14:03:34.405 ServerApp]     http://127.0.0.1:8888/lab?token=b5a9551e723de0ef5449cf0f924e43424f569695a5cb64b7
[I 2021-05-19 14:03:34.405 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2021-05-19 14:03:34.411 ServerApp]

    To access the server, open this file in a browser:
        file:///home/jovyan/.local/share/jupyter/runtime/jpserver-8-open.html
    Or copy and paste one of these URLs:
        http://485670dd1edb:8888/lab?token=b5a9551e723de0ef5449cf0f924e43424f569695a5cb64b7
        http://127.0.0.1:8888/lab?token=b5a9551e723de0ef5449cf0f924e43424f569695a5cb64b7
[W 2021-05-19 14:03:40.545 ServerApp] Clearing invalid/expired login cookie username-192-168-100-173-49999
[W 2021-05-19 14:03:40.546 ServerApp] Clearing invalid/expired login cookie username-192-168-100-173-49999
[W 2021-05-19 14:03:40.547 ServerApp] Forbidden
[W 2021-05-19 14:03:40.548 ServerApp] 403 GET /api/sessions?_=1621432701644 (192.168.100.254) 3.97ms referer=http://192.168.100.173:49999/tree?
[W 2021-05-19 14:03:40.548 ServerApp] Forbidden
[W 2021-05-19 14:03:40.549 ServerApp] 403 GET /api/terminals?_=1621432701645 (192.168.100.254) 2.50ms referer=http://192.168.100.173:49999/tree?
[I 2021-05-19 14:03:42.229 NotebookApp] 302 GET /tree? (192.168.100.254) 1.10ms

 

jupyter-docker-stacks에서 제공하는 도커이미지는 기본적으로 jupyter-notebook으로 수행되어 도커 내 ubuntu 환경은 설정하기 어려운 단점이 있다.

 

jupyterlab을 사용하기 위해서는 docker image를 구동할 때 start.sh jupyter lab 을 추가적으로 입력하면, 

컨테이너가 실행되면서 jupyterlab으로 변경되어 실행된다.

 

https://jupyter-docker-stacks.readthedocs.io/en/latest/using/recipes.html#run-jupyterlab

 

Contributed Recipes — docker-stacks latest documentation

Users sometimes share interesting ways of using the Jupyter Docker Stacks. We encourage users to contribute these recipes to the documentation in case they prove useful to other members of the community by submitting a pull request to docs/using/recipes.md

jupyter-docker-stacks.readthedocs.io

Run JupyterLab

JupyterLab is preinstalled as a notebook extension starting in tag c33a7dc0eece.

Run jupyterlab using a command such as docker run -it --rm -p 8888:8888 jupyter/datascience-notebook start.sh jupyter lab

 

-p 옵션의 경우 포트 포워딩을 하는 옵션으로 서버PORT:컨테이너PORT를 입력하면 된다.

ex) localhost의 49999포트를 접속하게 되면, 현재 구동된 컨테이너 내부의 8888(jupyterlab)포트를 접속하게 됨

 

docker logs는 docker container의 디렉토리 log를 확인하는 명령어로 jupyter container에서는 jupyter log를 보여준다

해당 로그를 통해 token 키 값을 확인할 수 있다.

 

3. jupyterlab 접속

위에 설정된대로  http://서버IP:49999를 입력하면 아래와 같은 웹에 접속할 수 있으며 token은 logs에 나온 값을 입력하면 된다.

ex) b5a9551e723de0ef5449cf0f924e43424f569695a5cb64b7

정상적으로 접속하게 된다면 아래 오른쪽과 같은 페이지를 확인할 수 있다.