概要
ビルドを高速化しましょう
環境
- macOS 26.3.1
- docker 29.3.1
前
FROM python:3.12.11-alpine3.21
# 必要なパッケージをインストール
RUN apk add --no-cache nodejs npm
COPY . /home
WORKDIR /home
RUN pip install pipenv
RUN pipenv install
ENV REDIS_HOST=redis
# ミニファイ用ツールをインストール
RUN npm install -g clean-css-cli uglify-js
# CSS / JS をミニファイ
RUN uglifyjs static/js/fontawesome-all.min.js -o static/js/fontawesome-all.min.js
RUN cleancss -o static/css/footer.css static/css/footer.css
CMD ["pipenv", "run", "gunicorn", "-w", "1", "-b", "0.0.0.0:8080", "app:app"]
後
# syntax=docker/dockerfile:1.7
FROM python:3.12.11-alpine3.21
# 必要なパッケージ
RUN apk add --no-cache nodejs npm
WORKDIR /home
# -------------------------
# ① 依存関係だけ先にコピー(キャッシュ効かせる)
# -------------------------
COPY Pipfile Pipfile.lock ./
# pipenv + cache mount
RUN --mount=type=cache,target=/root/.cache/pip \
pip install pipenv && \
pipenv install --deploy --system
# -------------------------
# ② npmツール(キャッシュ効かせる)
# -------------------------
RUN --mount=type=cache,target=/root/.npm \
npm install -g clean-css-cli uglify-js
# -------------------------
# ③ アプリ本体(最後にコピー)
# -------------------------
COPY . .
ENV REDIS_HOST=redis
# -------------------------
# ④ minify(tmpfsで高速化&不要データ残さない)
# -------------------------
RUN --mount=type=tmpfs,target=/tmp \
uglifyjs static/js/fontawesome-all.min.js -o static/js/fontawesome-all.min.js && \
cleancss -o static/css/footer.css static/css/footer.css
CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:8080", "app:app"]
最後に
今回はライブラリのインストールなどに使いましたが一時的な認証情報を渡すときにも使えます