diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..b83f8f8 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,89 @@ +FROM mcr.microsoft.com/devcontainers/base:ubuntu-24.04 + +# System packages +RUN apt-get update && apt-get install -y --no-install-recommends \ + git \ + curl \ + ca-certificates \ + build-essential \ + zsh \ + tmux \ + gh \ + && rm -rf /var/lib/apt/lists/* + +# Node.js LTS (for Claude Code) +RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \ + && apt-get install -y --no-install-recommends nodejs \ + && rm -rf /var/lib/apt/lists/* + +# Miniforge (mamba included) +ENV MAMBA_ROOT_PREFIX=/opt/conda +RUN ARCH="$(uname -m)" \ + && curl -fsSL "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-${ARCH}.sh" \ + -o /tmp/miniforge.sh \ + && bash /tmp/miniforge.sh -b -p "${MAMBA_ROOT_PREFIX}" \ + && rm /tmp/miniforge.sh \ + && "${MAMBA_ROOT_PREFIX}/bin/mamba" clean --all --yes \ + && chown -R vscode:vscode "${MAMBA_ROOT_PREFIX}" + +ENV PATH="${MAMBA_ROOT_PREFIX}/bin:${PATH}" + +# Verify mamba is available +RUN mamba --version + +# Claude Code (global npm install) +RUN npm install -g @anthropic-ai/claude-code + +# Switch to non-root user +USER vscode + +# Keep Claude Code's config file inside the volume-backed directory. By default it +# lives at ~/.claude.json, outside ~/.claude, and would be lost on every rebuild. +ENV CLAUDE_CONFIG_DIR=/home/vscode/.claude + +# Project conda environment (keep in sync with environment.yml) +ENV CONDA_ENV_NAME=work + +# Register the conda shell hook so `conda activate` works in bash/zsh, and +# activate the project environment in every interactive shell. +# +# Both lines are required; keep or drop them together. +# +# - `conda init` writes the "conda initialize" block that defines the shell +# function. Without it `conda activate` is not a command at all, so the +# activation below fails and, more importantly, so does the one VS Code's +# Python Environments extension types into its terminals: because this +# block sets CONDA_SHLVL, that extension takes its "already sourced" path +# and emits a bare `conda activate ` with no `source` in front of it. +# - The explicit activation is what VS Code-independent shells rely on +# (`docker exec`, CI, ssh); conda's auto_activate would land them in base. +# +# The interpreter VS Code itself uses is a separate matter: the Python +# Environments extension ignores python.defaultInterpreterPath and stores its +# per-project environment in VS Code's internal DB, so it has to be picked once +# via "Python: Select Interpreter". See .devcontainer/devcontainer.json. +RUN conda init bash zsh \ + && printf '\n# Auto-activate the project conda environment\nconda activate %s 2>/dev/null || true\n' "${CONDA_ENV_NAME}" \ + | tee -a /home/vscode/.bashrc /home/vscode/.zshrc > /dev/null + +# uv (fast pip alternative) +RUN curl -LsSf https://astral.sh/uv/install.sh | sh +ENV PATH="/home/vscode/.local/bin:${PATH}" + +# プロジェクトの conda 環境をここで作り込む。数分かかる処理なので、コンテナ +# 再作成のたびに走る postCreateCommand ではなくイメージ側に置いている。 +# +# COPY するのは environment.yml だけにすること。ソースごと COPY すると、 +# 無関係な .py を 1 行直しただけで COPY 以降のレイヤーキャッシュが無効になり、 +# 直後の env create がまるごとやり直しになる。 +# +# 自分自身の editable install (-e .) をここに含めないのも同じ理由。あれは +# setuptools_scm のために .git を必要とし、.git を COPY するとコミットする +# たびにキャッシュが飛ぶ。editable install は postCreateCommand 側に残してある。 +# 環境名は environment.yml の name と CONDA_ENV_NAME で揃えてある。 +COPY --chown=vscode:vscode environment.yml /tmp/environment.yml +RUN mamba env create -y -f /tmp/environment.yml \ + && mamba clean -yaf \ + && rm /tmp/environment.yml + +WORKDIR /workspaces/python-template diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index f1ee725..ea30a93 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,11 +1,11 @@ // https: //code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials { "name": "python-template", - "dockerComposeFile": "../docker/docker-compose.yml", + "dockerComposeFile": "docker-compose.yml", "service": "app", "workspaceFolder": "/workspaces/python-template", "remoteUser": "vscode", - // conda 環境そのものは docker/Dockerfile でイメージに焼いてあるので、ここに + // conda 環境そのものは .devcontainer/Dockerfile でイメージに焼いてあるので、ここに // 残すのは自分自身の editable install だけ。これをビルド時に回せないのは、 // setuptools_scm がバージョン解決に .git を要求する一方、.git はイメージに // 入れず bind mount で渡しているため。詳細は environment.yml のコメント参照。 @@ -16,7 +16,7 @@ // conda activate は対話シェル (.bashrc / .zshrc) でしか走らない。VS Code // サーバは非対話で起動するため、これが無いと拡張から起動されるコマンド // (.vscode/settings.json の RunOnSave など) が base の PATH で実行される。 - // 環境名は environment.yml / docker/Dockerfile の CONDA_ENV_NAME と揃える。 + // 環境名は environment.yml / .devcontainer/Dockerfile の CONDA_ENV_NAME と揃える。 "remoteEnv": { "PATH": "/opt/conda/envs/work/bin:${containerEnv:PATH}" }, diff --git a/docker/docker-compose.yml b/.devcontainer/docker-compose.yml similarity index 98% rename from docker/docker-compose.yml rename to .devcontainer/docker-compose.yml index dcdef7a..2fa0005 100644 --- a/docker/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -17,7 +17,7 @@ services: app: build: context: .. - dockerfile: docker/Dockerfile + dockerfile: .devcontainer/Dockerfile # gpus: all # If you do not want to use, comment it out depends_on: init: diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 0494839..7f2a7b1 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,10 +13,17 @@ updates: commit-message: prefix: "chore: " open-pull-requests-limit: 5 + # ライブラリなので、既存の範囲 (>= 下限) で新版を許容できるなら下限は上げない。 + # 未指定 (auto) だと PyPI 上の同名パッケージとの照合で app 判定され、 + # increase (下限の引き上げ) になる。 + versioning-strategy: increase-if-necessary # Docker dependencies - package-ecosystem: "docker" - directory: "/docker" + # 開発用 (.devcontainer) と利用者向け (docker) の Dockerfile + directories: + - "/.devcontainer" + - "/docker" schedule: interval: "weekly" commit-message: diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d6f3c26..d924e21 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -19,22 +19,42 @@ on: branches: - main paths: + - '.devcontainer/**' - 'docker/**' + # 開発用イメージはビルド時に environment.yml から conda 環境を作る + - 'environment.yml' - '.github/workflows/docker.yml' pull_request: types: [opened, synchronize, reopened, ready_for_review] paths: + - '.devcontainer/**' - 'docker/**' + - 'environment.yml' - '.github/workflows/docker.yml' jobs: - build: + devcontainer: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - name: Validate docker compose config run: | - docker compose -f docker/docker-compose.yml config -q + docker compose -f .devcontainer/docker-compose.yml config -q - name: Build devcontainer image run: | - docker compose -f docker/docker-compose.yml build app + docker compose -f .devcontainer/docker-compose.yml build app + + user-image: + runs-on: ubuntu-latest + steps: + # setuptools_scm がタグからバージョンを決めるので、履歴とタグごと取る。 + # 浅いクローンでもビルドは通るが、バージョンが 0.1.devN になる。 + - uses: actions/checkout@v7 + with: + fetch-depth: 0 + - name: Build user image + run: | + docker build -f docker/Dockerfile -t python-template:ci . + - name: Smoke test + run: | + docker run --rm python-template:ci python -m python_template --version diff --git a/README.md b/README.md index ea7cc0c..ebe4abe 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,17 @@ pip install git+https://github.com/yu9824/python-template.git#subdirectory=packa ``` +### Docker + +```bash +git clone https://github.com/yu9824/python-template.git +cd python-template +docker build -f docker/Dockerfile -t python-template . +docker run --rm -it python-template +``` + +The image contains the package with its optional dependencies and starts a Python shell. The development container lives in `.devcontainer/` instead. + ## Directory layout | Directory | Purpose | @@ -47,3 +58,5 @@ pip install git+https://github.com/yu9824/python-template.git#subdirectory=packa | `docs_src/` | Sphinx source | | `packages/` | Package templates for different build configurations | | `examples/` | Example scripts | +| `docker/` | Docker image for users of the package | +| `.devcontainer/` | Development container (VS Code Dev Containers) | diff --git a/docker/Dockerfile b/docker/Dockerfile index b83f8f8..e8da906 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,89 +1,38 @@ -FROM mcr.microsoft.com/devcontainers/base:ubuntu-24.04 +# syntax=docker/dockerfile:1 -# System packages -RUN apt-get update && apt-get install -y --no-install-recommends \ - git \ - curl \ - ca-certificates \ - build-essential \ - zsh \ - tmux \ - gh \ - && rm -rf /var/lib/apt/lists/* - -# Node.js LTS (for Claude Code) -RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \ - && apt-get install -y --no-install-recommends nodejs \ - && rm -rf /var/lib/apt/lists/* - -# Miniforge (mamba included) -ENV MAMBA_ROOT_PREFIX=/opt/conda -RUN ARCH="$(uname -m)" \ - && curl -fsSL "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-${ARCH}.sh" \ - -o /tmp/miniforge.sh \ - && bash /tmp/miniforge.sh -b -p "${MAMBA_ROOT_PREFIX}" \ - && rm /tmp/miniforge.sh \ - && "${MAMBA_ROOT_PREFIX}/bin/mamba" clean --all --yes \ - && chown -R vscode:vscode "${MAMBA_ROOT_PREFIX}" - -ENV PATH="${MAMBA_ROOT_PREFIX}/bin:${PATH}" - -# Verify mamba is available -RUN mamba --version - -# Claude Code (global npm install) -RUN npm install -g @anthropic-ai/claude-code +# このライブラリの利用者向けイメージ。開発用のコンテナは .devcontainer/ にある。 +# +# docker build -f docker/Dockerfile -t python-template . +# docker run --rm -it python-template +# +# ビルドコンテキストはリポジトリのルート。送るファイルは隣の +# Dockerfile.dockerignore (この Dockerfile 専用の除外設定) で絞っている。 -# Switch to non-root user -USER vscode +# ---- builder: wheel を作るだけのステージ ---- +FROM python:3.13-slim AS builder -# Keep Claude Code's config file inside the volume-backed directory. By default it -# lives at ~/.claude.json, outside ~/.claude, and would be lost on every rebuild. -ENV CLAUDE_CONFIG_DIR=/home/vscode/.claude +# setuptools_scm は git コマンドと .git からバージョンを決める。どちらも +# このステージにしか入れず、最終イメージには wheel だけを持ち込む。 +RUN apt-get update && apt-get install -y --no-install-recommends git \ + && rm -rf /var/lib/apt/lists/* -# Project conda environment (keep in sync with environment.yml) -ENV CONDA_ENV_NAME=work +WORKDIR /src +COPY . . +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /dist . -# Register the conda shell hook so `conda activate` works in bash/zsh, and -# activate the project environment in every interactive shell. -# -# Both lines are required; keep or drop them together. -# -# - `conda init` writes the "conda initialize" block that defines the shell -# function. Without it `conda activate` is not a command at all, so the -# activation below fails and, more importantly, so does the one VS Code's -# Python Environments extension types into its terminals: because this -# block sets CONDA_SHLVL, that extension takes its "already sourced" path -# and emits a bare `conda activate ` with no `source` in front of it. -# - The explicit activation is what VS Code-independent shells rely on -# (`docker exec`, CI, ssh); conda's auto_activate would land them in base. -# -# The interpreter VS Code itself uses is a separate matter: the Python -# Environments extension ignores python.defaultInterpreterPath and stores its -# per-project environment in VS Code's internal DB, so it has to be picked once -# via "Python: Select Interpreter". See .devcontainer/devcontainer.json. -RUN conda init bash zsh \ - && printf '\n# Auto-activate the project conda environment\nconda activate %s 2>/dev/null || true\n' "${CONDA_ENV_NAME}" \ - | tee -a /home/vscode/.bashrc /home/vscode/.zshrc > /dev/null +# ---- runtime ---- +FROM python:3.13-slim -# uv (fast pip alternative) -RUN curl -LsSf https://astral.sh/uv/install.sh | sh -ENV PATH="/home/vscode/.local/bin:${PATH}" +RUN useradd --create-home app -# プロジェクトの conda 環境をここで作り込む。数分かかる処理なので、コンテナ -# 再作成のたびに走る postCreateCommand ではなくイメージ側に置いている。 -# -# COPY するのは environment.yml だけにすること。ソースごと COPY すると、 -# 無関係な .py を 1 行直しただけで COPY 以降のレイヤーキャッシュが無効になり、 -# 直後の env create がまるごとやり直しになる。 +# wheel はパッケージ名ではなくファイルのパスで指定する。名前で指定すると +# (--find-links)、手元の wheel が開発版 (.devN) のとき、index 上に同名の +# 正式版があれば pip はそちらを優先して入れてしまう。 # -# 自分自身の editable install (-e .) をここに含めないのも同じ理由。あれは -# setuptools_scm のために .git を必要とし、.git を COPY するとコミットする -# たびにキャッシュが飛ぶ。editable install は postCreateCommand 側に残してある。 -# 環境名は environment.yml の name と CONDA_ENV_NAME で揃えてある。 -COPY --chown=vscode:vscode environment.yml /tmp/environment.yml -RUN mamba env create -y -f /tmp/environment.yml \ - && mamba clean -yaf \ - && rm /tmp/environment.yml +# bind mount なので、wheel 自体はイメージのレイヤーに残らない。 +RUN --mount=type=bind,from=builder,source=/dist,target=/tmp/dist \ + pip install --no-cache-dir "$(ls /tmp/dist/*.whl)[optional]" -WORKDIR /workspaces/python-template +USER app +WORKDIR /home/app +CMD ["python"] diff --git a/docker/Dockerfile.dockerignore b/docker/Dockerfile.dockerignore new file mode 100644 index 0000000..3c0f639 --- /dev/null +++ b/docker/Dockerfile.dockerignore @@ -0,0 +1,23 @@ +# docker/Dockerfile 専用の除外設定。ビルドコンテキストはリポジトリのルート。 +# +# .git と、git が追跡しているファイルは除外しないこと。builder ステージでは +# setuptools_scm が git に作業ツリーの状態を問い合わせてバージョンを決める。 +# 追跡ファイルが欠けていると「変更あり」と判定され、バージョンに日付 +# (+...dYYYYMMDD) が付いてしまう。 +# +# ここで落とすのは、git が追跡していない重いもの・ホスト固有のものだけ。 +data/** +!data/**/.gitkeep +models/** +!models/**/.gitkeep +**/__pycache__ +**/*.egg-info +**/.DS_Store +.mypy_cache +.pytest_cache +.ruff_cache +.venv +build +dist +docs +jupyter_execute diff --git a/environment.yml b/environment.yml index dad0ddc..7facf2f 100644 --- a/environment.yml +++ b/environment.yml @@ -19,7 +19,7 @@ dependencies: # - scikit-learn # - networkx - # このファイルは docker/Dockerfile でイメージビルド時に読まれる (env create)。 + # このファイルは .devcontainer/Dockerfile でイメージビルド時に読まれる (env create)。 # そのため「ソースが無いと解決できない依存」をここに書いてはいけない。 # # 具体的には自分自身の editable install (-e .[dev,test,optional]) が該当する。