From c737cef68e53da92b84bba03eb0ce635679e87d1 Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Wed, 20 Dec 2023 21:39:16 +0900 Subject: [PATCH] wip --- Dockerfile | 70 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6b1cdc24..5bbe8834 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,56 +1,73 @@ # syntax=docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021 -FROM buildpack-deps:bookworm AS model-fetch +# ビルド時に基礎として使うイメージを定義 +FROM buildpack-deps:bookworm as base-build +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +# jqのバイナリを取得する => /jq +FROM ghcr.io/jqlang/jq:1.7 as fetch-jq + +# 音声モデルを取得する => /app/ +FROM --platform=$BUILDPLATFORM base-build AS model-fetch WORKDIR /app RUN wget https://github.com/jpreprocess/jpreprocess/releases/download/v0.6.1/naist-jdic-jpreprocess.tar.gz \ && tar xzf naist-jdic-jpreprocess.tar.gz \ && rm naist-jdic-jpreprocess.tar.gz RUN git clone --depth 1 https://github.com/icn-lab/htsvoice-tohoku-f01.git -FROM ghcr.io/jqlang/jq:1.7 as fetch-jq - -FROM quay.io/curl/curl-base:8.4.0 as fetch-pnpm +# pnpmを取得する => /pnpm/ +FROM base-build as fetch-pnpm ENV SHELL="sh" ENV ENV="/tmp/env" -ENV PNPM_HOME="/pnpm" WORKDIR /dist RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,from=fetch-jq,source=/jq,target=/mounted-bin/jq \ curl -fsSL --compressed https://get.pnpm.io/install.sh | env PNPM_VERSION=$(cat package.json | /mounted-bin/jq -r .packageManager | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') sh - -FROM buildpack-deps:bookworm as fetch-deps -ENV PNPM_HOME="/pnpm" -ENV PATH="$PNPM_HOME:$PATH" -WORKDIR /package +# .npmrcに設定を追記する => /.npmrc +FROM base-build as change-npmrc +COPY --link .npmrc ./ +RUN --mount=type=bind,source=.node-version,target=.node-version \ + echo \ + "store-dir=/.pnpm-store\n \ + use-node-version=`cat .node-version`"\ + >> .npmrc + +# Node.jsと依存パッケージを取得する => /pnpm/,/.pnpm-store +FROM base-build as fetch-deps COPY --link --from=fetch-pnpm /pnpm/ /pnpm/ -RUN pnpm config set store-dir /.pnpm-store -COPY --link .npmrc .node-version ./ -RUN echo "use-node-version=`cat .node-version`" >> .npmrc RUN --mount=type=cache,target=/.pnpm-store \ + --mount=type=bind,from=change-npmrc,source=/.npmrc,target=.npmrc \ + --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ pnpm fetch -COPY --link package.json ./ -FROM fetch-deps as dev-deps -RUN --mount=type=cache,target=/.pnpm-store \ +# dev用の依存パッケージをインストールする => /node_modules/ +FROM --platform=$BUILDPLATFORM base-build as dev-deps +RUN --mount=type=bind,from=fetch-deps,source=/pnpm/,target=/pnpm/ \ + --mount=type=bind,from=fetch-deps,source=/.pnpm-store,target=.pnpm-store \ + --mount=type=bind,from=change-npmrc,source=/.npmrc,target=.npmrc \ + --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ pnpm install --frozen-lockfile --offline -FROM buildpack-deps:bookworm as builder -ENV PNPM_HOME="/pnpm" -ENV PATH="$PNPM_HOME:$PATH" -WORKDIR /app +# ビルドする => /dist/ +FROM --platform=$BUILDPLATFORM base-build as builder RUN --mount=type=bind,from=fetch-deps,source=/pnpm/,target=/pnpm/ \ - --mount=type=bind,from=dev-deps,source=/package/node_modules/,target=node_modules/ \ + --mount=type=bind,from=dev-deps,source=/app/node_modules/,target=node_modules/ \ + --mount=type=bind,from=change-npmrc,source=/.npmrc,target=.npmrc \ --mount=type=bind,source=package.json,target=package.json \ - --mount=type=bind,source=.npmrc,target=.npmrc \ --mount=type=bind,source=build.js,target=build.js \ --mount=type=bind,source=src/,target=src/ \ pnpm build -FROM fetch-deps as prod-deps +# prod用の依存パッケージをインストールする => /node_modules/ +FROM base-build as prod-deps ARG NODE_ENV="production" -RUN --mount=type=cache,target=/.pnpm-store \ +RUN --mount=type=bind,from=fetch-deps,source=/pnpm/,target=/pnpm/ \ + --mount=type=bind,from=fetch-deps,source=/.pnpm-store,target=.pnpm-store \ + --mount=type=bind,from=change-npmrc,source=/.npmrc,target=.npmrc \ + --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ pnpm install --frozen-lockfile --offline @@ -61,8 +78,9 @@ ENV NODE_ENV="production" WORKDIR /app COPY --link --from=model-fetch /app/ ./model/ COPY --link --from=fetch-deps /pnpm/ /pnpm/ -COPY --link --from=builder /app/dist/ ./dist/ -COPY --from=prod-deps /package/node_modules/ ./node_modules/ -COPY --link .npmrc package.json ./ +COPY --link --from=builder /dist/ ./dist/ +COPY --from=prod-deps /node_modules/ ./node_modules/ +COPY --link --from=prod-deps /app/.npmrc ./ +COPY --link package.json ./ ENTRYPOINT [ "pnpm", "--shell-emulator" ] CMD [ "start" ]