🎏

DockerでStreamlitを動かしてみる

に公開

はじめに

Streamlitをローカル上のDockerで動かす際の手順を備忘録としてまとめました。

動作環境

  • Windows 11
  • Docker Desktop
  • WSL2 (Ubuntu-24.04)
  • Python 3.13.5

フォルダ構成

今回はDockerでStreamlitを動かすことに焦点を置いているため、フォルダ構成は最小限としています。

- .venv
- .python-version
- Dockerfile
- requirements.txt
- streamlit_app.py

以下、各ファイル、フォルダの説明です。

  • .venvフォルダ:ローカル環境で動作確認をする際に作成するPythonの仮想環境フォルダです。Dockerで完結させる場合は必要ないです。
  • .python-version:Pythonのバージョン管理としてpyenvを使用しており、ローカルのPythonのバージョンを指定した際に作成されたバージョン管理ファイルです。Dockerで完結させる場合は必要ないです。
  • Dockerfile:コンテナイメージを作成するために必要なファイルです。
  • requirements.txt:Pythonのパッケージを管理するファイルです。コンテナイメージ作成時に用います。
  • streamlit_app.py:Streamlitアプリを動かすためのpyファイルです。

各ファイルの説明

Streamlitを動かすためのDockerfile

Dockerfile公式ドキュメントに書かれているサンプルをベースに記載しました。

FROM python:3.13.5-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    software-properties-common \
    git \
    && rm -rf /var/lib/apt/lists/*

# streamlit_app.pyとrequirements.txtをコピー
COPY streamlit_app.py .
COPY requirements.txt .

RUN pip3 install -r requirements.txt

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]

streamlit_app.py

Streamlitで表示させるページは、公式のGitHubのリポジトリのstreamlit_app.pyを利用しました。

import altair as alt
import numpy as np
import pandas as pd
import streamlit as st

"""
# Welcome to Streamlit!

Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
forums](https://discuss.streamlit.io).

In the meantime, below is an example of what you can do with just a few lines of code:
"""

num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)

indices = np.linspace(0, 1, num_points)
theta = 2 * np.pi * num_turns * indices
radius = indices

x = radius * np.cos(theta)
y = radius * np.sin(theta)

df = pd.DataFrame({
    "x": x,
    "y": y,
    "idx": indices,
    "rand": np.random.randn(num_points),
})

st.altair_chart(alt.Chart(df, height=700, width=700)
    .mark_point(filled=True)
    .encode(
        x=alt.X("x", axis=None),
        y=alt.Y("y", axis=None),
        color=alt.Color("idx", legend=None, scale=alt.Scale()),
        size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
    ))

requirements.txt

ローカル環境のPythonでStreamlitアプリを作成する際にインストールしたPythonのパッケージを、pip freeze > requirements.txtで出力したものです。

altair==5.5.0
attrs==25.3.0
blinker==1.9.0
cachetools==6.1.0
certifi==2025.8.3
charset-normalizer==3.4.2
click==8.2.1
gitdb==4.0.12
GitPython==3.1.45
idna==3.10
Jinja2==3.1.6
jsonschema==4.25.0
jsonschema-specifications==2025.4.1
MarkupSafe==3.0.2
narwhals==2.0.1
numpy==2.3.2
packaging==25.0
pandas==2.3.1
pillow==11.3.0
protobuf==6.31.1
pyarrow==21.0.0
pydeck==0.9.1
python-dateutil==2.9.0.post0
pytz==2025.2
referencing==0.36.2
requests==2.32.4
rpds-py==0.27.0
six==1.17.0
smmap==5.0.2
streamlit==1.48.0
tenacity==9.1.2
toml==0.10.2
tornado==6.5.1
typing_extensions==4.14.1
tzdata==2025.2
urllib3==2.5.0
watchdog==6.0.0

コンテナでアプリを起動~停止

  1. Dockerイメージのビルド

    docker build -t streamlit-app .
    
  2. コンテナを起動

    初回起動(コンテナ作成)の場合

    docker run -p 8501:8501 -d --name streamlit-app streamlit-app
    

    コンテナ作成済みの場合

    docker start streamlit-app
    

    コンテナが起動すると以下のように表示されます。

    Docker Desktopのコンテナ一覧

  3. ブラウザで http://localhost:8501 にアクセス


    コンテナで起動したStreamlit Webアプリのトップページ

  4. コンテナを停止

     docker stop streamlit-app
    

最後に

streamlitをDockerで動かす際の手順を記載しました。
今度はDBやAzure Storageなどを含めたdocker-composeでのアプリ起動についてまとめようと思います。

ヘッドウォータース

Discussion