LoginSignup
0
1

More than 3 years have passed since last update.

Dockerでjupyter_notebook_config.pyが読み込まれないのですが?

Posted at

はじめに

Kaggleに本腰を入れようかと思い、Kaggle用の環境をDockerを利用して作成していました。
こちらの記事の通りに環境を作成しました。

普段はURLのトークンパスワードを入力せずに、localhost:8888にアクセスするだけでJupyter notebookが利用できる環境を使っているため、今回もjupyter_notebook_config.pyを導入したのですが、それがうまく読み込まれず、困ったので記事にしました。

jupyter_notebook_config.pyを追加することで、パスワードの設定などができるのでおすすめです。

問題

Dockerfileでjupyter_notebook_config.pyをローカルからコンテナにコピーしているが読み込まれていませんでした。
読み込まれて入れば、jupyter_notebook_config.pyに設定をした以下3点が反映されるはずでした。

  1. wording_dirが設定される
  2. トークンの設定無効化
  3. パスワードの設定

各ファイルについて

ディレクトリ構成は以下のようになっています。

┣ libraries
  ┣ setting
    ┣ jupyter_notebook_config.py
┣ notebook
┣ docker-compose.yml
┣ Dockerfile

各ファイルは以下のようになっています。

docker-compose.yml

version: "3"
services:
    jupyter:
        build: .
        volumes:
            - ./notebook/:/tmp/working
        ports:
            - 8888:8888
        command: jupyter notebook --ip=0.0.0.0 --allow-root --no-browser

Dockerfile

FROM gcr.io/kaggle-images/python:v56

RUN apt-get update
RUN apt-get install vim -y

RUN pip install -U pip && \
        pip install fastprogress japanize-matplotlib

COPY ./libraries/setting/jupyter_notebook_config.py .jupyter/

jupyter_notebook_config.py

c.NotebookApp.notebook_dir = "/tmp/working"
c.NotebookApp.password = ""
c.NotebookApp.token = ''

なぜか動く現象

docker-compose.ymlのvolumeの位置にjupyter_notebook_config.pyをコピーしてworking_dirを設定するとなぜかjupyter_notebook_config.pyが読み込まれた。

version: "3"
services:
    jupyter:
        build: .
        volumes:
            - ./libraries/setting:/tmp/working
        working_dir: /tmp/working
        ports:
            - 8888:8888
        command: jupyter notebook --ip=0.0.0.0 --allow-root --no-browser

解決方法

rootでjupyterを起動しているため、root/.jupyter/にコピーする必要があった。

以下のコードに修正することで動きました。

Dockerfile

FROM gcr.io/kaggle-images/python:v56

RUN apt-get update
RUN apt-get install vim -y

RUN pip install -U pip && \
        pip install fastprogress japanize-matplotlib

COPY ./libraries/setting/jupyter_notebook_config.py root/.jupyter/

docker-compose.yml

version: "3"
services:
    jupyter:
        build: .
        volumes:
            - ./notebook/:/tmp/working
        ports:
            - 8888:8888
        command: jupyter notebook --ip=0.0.0.0 --allow-root --no-browser

なぜか動く現象はよくわからないが、volumeするディレクトリ直下にjupyter_notebook_config.pyがあればちゃんと動くようだった。ここはさらに調査が必要そう

おわりに

この記事はteratailで実際に質問した内容をまとめたものになります。この質問は具体的すぎるから返ってこないだろうなと思いましたが、回答があり驚きました。

とりあえず質問を投げるのはアリだと思いました。

私も詳しくなってきたら質問を答える側に回りたいです。

参考記事

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1