Files
yotta 233319876b
Validate and publish container image / build (pull_request) Successful in 6m2s
コンテナCIのビルドを最適化
2026-07-18 08:53:49 +09:00

41 lines
1.2 KiB
Docker

FROM python:3.11-slim AS base
# 作業ディレクトリを設定
WORKDIR /app
# システムパッケージを更新
RUN apt-get update && apt-get install -y \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Pythonの依存関係をコピーしてインストール
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# アプリケーションファイルをコピー
COPY app.py .
# テスト用ステージ(CIではこのステージまでビルドして単体テストを実行)
FROM base AS test
COPY docker-compose.yml docker-compose.local.yml ./
COPY tests ./tests
RUN python -m unittest discover -s tests -v \
&& touch /tmp/tests-passed
# 実行用ステージ
FROM base AS runtime
# 非rootユーザーを作成
COPY --from=test /tmp/tests-passed /tmp/tests-passed
RUN rm /tmp/tests-passed \
&& useradd --create-home --shell /bin/bash appuser \
&& chown -R appuser:appuser /app
USER appuser
# ヘルスチェックを追加
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "from pathlib import Path; import sys; sys.exit(0 if b'app.py' in Path('/proc/1/cmdline').read_bytes() else 1)"
# アプリケーションを実行
CMD ["python", "app.py"]