Skip to content

Commit

Permalink
build: fix Dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Mogyuchi <[email protected]>
  • Loading branch information
Mogyuchi committed Dec 21, 2023
1 parent c16470a commit 65de17c
Showing 1 changed file with 32 additions and 48 deletions.
80 changes: 32 additions & 48 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,68 +1,52 @@
# syntax=docker/dockerfile:1@sha256:ac85f380a63b13dfcefa89046420e1781752bab202122f8f50032edf31be0021

FROM buildpack-deps:bookworm 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
# ビルド時に基礎として使うイメージを定義
FROM buildpack-deps:bookworm as base-build
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
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,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 \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
pnpm install --frozen-lockfile --offline
# jqのバイナリを取得する => /jq
FROM ghcr.io/jqlang/jq:1.7 as fetch-jq

FROM buildpack-deps:bookworm as builder
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
# 音声モデルを取得する => /app/
FROM --platform=$BUILDPLATFORM base-build AS model-fetch
WORKDIR /app
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,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
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 fetch-deps as prod-deps
FROM base-build as deps
WORKDIR /app
COPY --link --from=fetch-jq /jq ./
COPY --link package.json ./
RUN curl -fsSL --compressed https://get.pnpm.io/install.sh | env PNPM_VERSION=$(cat package.json | ./jq -r .packageManager | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') sh -
COPY --link .npmrc .node-version ./
RUN echo "store-dir=/.pnpm-store" >> .npmrc &&\
echo "use-node-version=`cat .node-version`" >> .npmrc
COPY --link pnpm-lock.yaml ./
ARG NODE_ENV="production"
RUN --mount=type=cache,target=/.pnpm-store \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
pnpm install --frozen-lockfile --offline
RUN pnpm install --frozen-lockfile

# ビルドする => /app/dist/
FROM --platform=$BUILDPLATFORM deps as builder
ARG NODE_ENV=""
RUN pnpm install --frozen-lockfile
COPY --link build.js ./
COPY --link src/ ./src/
RUN pnpm build

FROM gcr.io/distroless/cc-debian12:nonroot as runner
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NODE_ENV="production"
WORKDIR /app
COPY --link --from=model-fetch /app/ ./model/
COPY --link --from=fetch-deps /pnpm/ /pnpm/
COPY --link --from=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 --from=deps /app/node_modules/ ./node_modules/
COPY --link --from=deps /app/.npmrc ./
COPY --link package.json ./
ENTRYPOINT [ "pnpm", "--shell-emulator" ]
CMD [ "start" ]

0 comments on commit 65de17c

Please sign in to comment.