1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker で Marimo を動かす最小ガイド(脱Jupyter)

1
Posted at

Marimo on Docker

marimoをdockerで使うための設定を記載します。
marimoについての詳しい説明は公式や以下の資料を参考にしてください。

最小構成

ディレクトリ構成

project/
├── Dockerfile
├── compose.yaml
├── requirements.txt
└── notebooks/
    └── example.py

Dockerfile

FROM python:3.12-slim

WORKDIR /notebooks
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

EXPOSE 2718
CMD ["marimo", "edit", "--host", "0.0.0.0", "--port", "2718", "--no-token", "/notebooks"]

requirements.txt

marimo
pandas
numpy
matplotlib

compose.yaml

services:
  notebook:
    build: .
    ports:
      - "127.0.0.1:2718:2718"
    volumes:
      - ./notebooks:/notebooks

起動

docker compose build notebook
docker compose up notebook
# → http://localhost:2718

データディレクトリをマウントする

CSV 等のデータファイルをコンテナに読み込ませる場合は読み取り専用でマウントする。

services:
  notebook:
    build: .
    ports:
      - "127.0.0.1:2718:2718"
    volumes:
      - ./notebooks:/notebooks
      - ./data:/data:ro          # :ro で読み取り専用

コンテナ内からは /data/ でアクセスできる。

# ノートブック内での読み込み例
df = pd.read_csv("/data/prices.csv")

認証

ローカル限定(--no-token

127.0.0.1 バインドで外部から到達できない場合はトークンなしで運用できる。

ports:
  - "127.0.0.1:2718:2718"
command: marimo edit --host 0.0.0.0 --port 2718 --no-token /notebooks

トークン認証(外部公開する場合)

services:
  notebook:
    build: .
    ports:
      - "0.0.0.0:2718:2718"
    volumes:
      - ./notebooks:/notebooks
    environment:
      - MARIMO_TOKEN=your-secret-token
    command: >
      marimo edit
      --host 0.0.0.0
      --port 2718
      --token-password ${MARIMO_TOKEN}
      /notebooks

.env ファイルで管理する場合:

# .env
MARIMO_TOKEN=your-secret-token
# compose.yaml
env_file: .env

注意: 外部公開する場合は必ず --token-password を設定すること。


起動モードの切り替え

Marimo には編集モードとアプリモードがある。
compose.yamlcommand を上書きすることで切り替えられる。

モード コマンド 用途
編集モード marimo edit ノートブックの開発・編集
アプリモード marimo run 読み取り専用で公開
# アプリモードで特定ファイルを公開
command: marimo run --host 0.0.0.0 --port 2718 --no-token /notebooks/dashboard.py

.ipynb からの移行

既存の Jupyter Notebook を Marimo 形式に変換することができる。
marimo convert コマンドを使う。
変換はホスト側で実行し、出力ファイルをコンテナのマウント先に置く。

# ホストに marimo をインストール
pip install marimo

# 変換
marimo convert input.ipynb -o notebooks/output.py

変換後の .ipynb は削除してもOK。


よくある操作

# ビルド(Dockerfile や requirements.txt を変更したとき)
docker compose build notebook

# バックグラウンド起動
docker compose up -d notebook

# ログ確認
docker compose logs notebook

# 停止
docker compose down

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?