LoginSignup
3
6

More than 3 years have passed since last update.

【Docker】Jupyter Labの実行環境をDockerで構築

Posted at

この記事がフィットしそうな方

・とりあえずデータ分析環境を手軽に構築したいと考えている。
・dockerを使える環境をお持ちである。
・Dockerfileの記述内容はなんとなくわかる。
・linuxコマンドもだいたいなんとなくわかる。

なぜDocker

・ホストの上にanacondaを入れたりするのでいいのではないか。anacondaなどを入れてしまうとアンインストールが大変になる。
・コンテナごと捨てたりできるので気軽に使える。
・他の人の環境構築もDockerfile一つですぐに環境が立ち上げ可能になる。環境構築がストレスフリーになる。

なぜanaconda

anacondaはPythonやデータ分析に必要なライブラリNumPy SchiPy jupyterなどがまとめられたパッケージ。
今回メインツールのjupyterも入っているので使わない手はない。環境構築に時間はかけたくない。
最近のanaconda3であればおそらくjupyterは入っているはずなので、どのバージョンでも大丈夫です。

実際のDockerfileは下記のようになります。今回のOSはubuntuで記述しています。

Dockerfile
FROM ubuntu:latest

RUN apt-get -y update && apt-get install -y \
         wget

WORKDIR /opt
RUN wget https://repo.continuum.io/archive/Anaconda3-2020.07-Linux-x86_64.sh && \
sh /opt/Anaconda3-2020.07-Linux-x86_64.sh -b -p /opt/anaconda3

ENV PATH /opt/anaconda3/bin:$PATH

RUN pip install --upgrade pip

WORKDIR /

CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]

ほぼ必要最低限のコマンド実行のみを実施しています
実行してみます。下記のコマンドをDockerfileのあるディレクトリで実行してください。
docker build .

実行ログ
Sending build context to Docker daemon  5.632kB
Step 1/6 : FROM ubuntu:latest
 ---> 4e2eef94cd6b
Step 2/6 : RUN apt-get -y update && apt-get install -y          wget
 ---> Running in ab7a4f407583
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [221 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [1078 B]
Get:7 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]

・・・・・中略

Removing intermediate container 5e6f539725f5
 ---> 8f167186b465
Step 5/6 : ENV PATH /opt/anaconda3/bin:$PATH
 ---> Running in da3a2e6521c3
Removing intermediate container da3a2e6521c3
 ---> 7306fc34e088
Step 6/6 : CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]
 ---> Running in 79cd954afcc4
Removing intermediate container 79cd954afcc4
 ---> 7b05db009f7f
Successfully built 7b05db009f7f

Successfully built イメージIDのイメージ IDをコンテナ起動時に使用します。

下記のコマンドを実行することでコンテナが起動します。
docker run -p 8888:8888 上記イメージID

実行ログ
[I 03:32:07.355 LabApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 03:32:08.010 LabApp] All authentication is disabled.  Anyone who can connect to this server will be able to run code.
[I 03:32:08.034 LabApp] JupyterLab extension loaded from /opt/anaconda3/lib/python3.8/site-packages/jupyterlab
[I 03:32:08.034 LabApp] JupyterLab application directory is /opt/anaconda3/share/jupyter/lab
[I 03:32:08.037 LabApp] Serving notebooks from local directory: /opt
[I 03:32:08.037 LabApp] The Jupyter Notebook is running at:
[I 03:32:08.037 LabApp] http://439ce26ee905:8888/
[I 03:32:08.038 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 03:32:08.042 LabApp] No web browser found: could not locate runnable browser.

ブラウザを起動してURLにlocalhost:8888と入力してアクセスできるようになっていると思います。

3
6
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
3
6