LoginSignup
3
5

More than 1 year has passed since last update.

DockerでJupyterLab環境を作る - Streamlit編

Posted at

Streamlitを表示させながらJupyterLabでコードを書きたいと思い、
Dockerで環境を作成しました。

前提の話はこちらの記事で書いています。

基本的には上記の内容とほぼ変わらないですが、
docker-compose.ymlでimageを2つ作成している点がことなります。

フォルダ構成

Dockerfile
docker-compose.yml
requirements.txt
src
 └ app.py

Dockerfile

FROM python:3

COPY requirements.txt .

RUN pip3 install --upgrade pip && \
    pip3 install --no-cache-dir -r requirements.txt && \
    pip3 install jupyterlab

WORKDIR /src
COPY /src /src

requirements.txt

streamlit
pandas

app.py

最初は空でも問題ないです。

import streamlit as st

st.write('Hello Streamlit')

docker-compose.yml

version: '3'
services:
  jupyterlab:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    container_name: jupyterlab
    working_dir: '/src'
    tty: true
    volumes:
      - ./src:/src
    ports: 
      - '8080:8080'
    command: jupyter-lab --ip 0.0.0.0 --port=8080 --allow-root --no-browser --NotebookApp.token=''

  stremlit:
    restart: always
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: stremlit
    working_dir: '/src'
    tty: true
    volumes: 
      - ./src:/src
    ports: 
      - '8501:8501'
    command: streamlit run app.py

jupyterlabの方は--NotebookApp.token=''オプションを追加してトークンの入力なしで開けるようにしました。
streamlitではworkind_dirで指定したフォルダにapp.pyがあることが重要です。

これらを用意してdockerコマンドを実行

docker compose up

localhost:8080でJupyterLab
localhost:8501でStreamlitが表示されます。

JupyterLabの拡張機能を入れるとimageの作成に時間がかかるので
省略していますが、実際には入れて環境構築します。

その話はこちらの記事でまとめています。

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