LoginSignup
0
0

wsl上にdockerでjupyterlab環境構築

Last updated at Posted at 2024-05-18

忘れないように忘備録として

実行環境

  • windows11
  • wsl2 Ubuntu 22.04.4 LTS
  • docker 26.1.0

ディレクトリ構成

docker-jupyterlab
├── compose.yml
├── python
│ ├── Dockerfile
│ ├── requirements.txt
│ └── root_jupyter
└── workspace

ファイル作成

Dockerfile、compose.yml、requirements.txt を作成

# パッケージをインストールするためのイメージ
FROM python:3.11.9-slim-bullseye as builder

COPY python/requirements.txt .

# パッケージの追加とタイムゾーンの設定
# libgomp1を入れないと、lightgbmをimportしたときエラーが出る
RUN apt-get update \
    && apt-get install -y \
    tzdata \
    libgomp1 \
    &&  ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
    &&  apt-get clean \
    &&  rm -rf /var/lib/apt/lists/*

ENV TZ=Asia/Tokyo

# pythonパッケージ
RUN python3 -m pip install --upgrade pip \
    &&  python3 -m pip install --no-cache-dir -r requirements.txt

compose.yml
services:
  python:
    container_name: docker-jupyterlab_python
    build:
      context: .
      dockerfile: python/Dockerfile
    restart: always
    entrypoint: >
      jupyter-lab --allow-root --ip=0.0.0.0 --port=8888 --no-browser --NotebookApp.token='' --notebook-dir=/workspace 
    expose:
      - "8888"
    ports:
      - "127.0.0.1:8888:8888"
    volumes:
      - ./python/root_jupyter:/root/.jupyter
      - ./workspace:/workspace

requirements.txt
jupyterlab==4.2.0
jupyterlab_code_formatter==2.2.1
black==24.4.2
isort==5.13.2
icecream==2.1.3
polars==0.20.26
plotly==5.22.0
lightgbm==4.3.0

実行方法

compose.ymlファイルのあるdocker-jupyterlabディレクトリで以下コマンドを実行

  • dockerイメージをビルド
    docker compose build
  • dockerコンテナを起動
    docker compose up -d
  • ブラウザで http://localhost:8888 にアクセス

以下の画面が表示されれば、jupyterlabの起動成功

Pasted image 20240519000618.png

おまけ

pythonコンテナに接続したいとき

docker exec -it docker-jupyterlab_python bashを実行

dockerコンテナの停止

docker compose downを実行

コードフォーマッタの使い方

Pasted image 20240518222506.png

赤い線で囲ったところをクリックするだけ

参考

DockerでJupyterLabの環境を作ろう | インフォメーション・ディベロプメント

3部: Docker Compose|実践 Docker - ソフトウェアエンジニアの「Docker よくわからない」を終わりにする本

0
0
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
0