jupyterhubとgitlabをdockerで動かしてSSOさせる

TL; DR

  • jupyterhubのdockerイメージはそのままでは動かない
  • oauthenticator使えば連携は容易

gitlab

起動

$ docker run -p 80:8080 -d -it --name gitlab gitlab/gitlab

rootパスワードの確認

/etc/gitlab/initial_root_passwordにある

アプリケーション設定

ID連携(GitLab)で、JupyterHubのユーザープロビジョニングを簡単に - Qiita に記載の通り。

「Application ID」と「Secret」は後で使う。

jupyterhub

Dockerfile

以下の内容でDockerfileを作成して、

$ docker build ./ -t jupyterhub_custom

を実行。jupyterlabとnotebookを入れないとjupyterhubにログイン後、jupyterlabが起動しない

FROM jupyterhub/jupyterhub

RUN apt update && \
    apt install -y python3 python3-pip npm nodejs && \
    pip3 --no-cache install notebook jupyterlab oauthenticator

ENTRYPOINT ["jupyterhub", "-f", "/srv/jupyterhub/jupyterhub_config.py"]

jupyterhub_config.pyの作成

$ docker run --entrypoint bash -it -d --rm -p 80:80 --name jupyterhub jupyterhub_custom

を実行してとりあえずjupyterhubを起動

$ docker exec -it jupyterhub bash

にてコンテナ内でbashを起動し、

$ jupyterhub --generate-config

を実行。コンテナからexitして、

$ docker cp jupyterhub:/srv/jupyterhub/jupyterhub_config.py ./

にてjupyterhub_config.pyをローカルに取得。コンテナは一旦不要なので、$ docker stop jupyterhubする。

以下をjupyterhub_config.pyに追記

from oauthenticator.gitlab import LocalGitLabOAuthenticator

c.JupyterHub.authenticator_class = LocalGitLabOAuthenticator

c.Spawner.default_url = '/lab'

# gitlabの認証さえ通ればjupyterhubの使用は許可する
c.LocalGitLabOAuthenticator.allow_all = True

c.LocalGitLabOAuthenticator.allow_all = Trueを入れておかないと、認証完了した後に、not allowedというエラーになってjupyterhubに入れない。

env_file

以下の内容を.envなどの名前で保存

GITLAB_URL=<GitLabのURL>
OAUTH_CALLBACK_URL=「Callback URL=jupyterhubの<URL>/hub/oauth_callback」
GITLAB_CLIENT_ID=「Application ID」
GITLAB_CLIENT_SECRET=「Secret」

起動

$ docker run -p 8000:8000 -v ./jupyterhub_config.py:/srv/jupyterhub/ --env-file ./.env -d -it --name jupyterhub jupyterhub_custom