|
| 1 | +# Adapted from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile |
| 2 | +# For more information, see https://nextjs.org/docs/pages/building-your-application/deploying#docker-image |
| 3 | + |
| 4 | +# Use a base image for building |
| 5 | +FROM node:18-slim AS base |
| 6 | + |
| 7 | +# Install git |
| 8 | +RUN apt-get update && apt-get install -y git |
| 9 | + |
| 10 | +# Clone the repository and navigate to the next-server folder |
| 11 | +WORKDIR /app |
| 12 | +RUN git clone https://github.com/huggingface/transformers.js-examples . |
| 13 | + |
| 14 | +# Set the working directory to the next-server folder |
| 15 | +WORKDIR /app/next-server |
| 16 | + |
| 17 | +# Install dependencies only when needed |
| 18 | +FROM base AS deps |
| 19 | + |
| 20 | +# Install dependencies based on the preferred package manager |
| 21 | +RUN \ |
| 22 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 23 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 24 | + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ |
| 25 | + else echo "Lockfile not found." && exit 1; \ |
| 26 | + fi |
| 27 | + |
| 28 | +# Rebuild the source code only when needed |
| 29 | +FROM base AS builder |
| 30 | +WORKDIR /app/next-server |
| 31 | +COPY --from=deps /app/next-server/node_modules ./node_modules |
| 32 | +COPY . . |
| 33 | + |
| 34 | +RUN \ |
| 35 | + if [ -f yarn.lock ]; then yarn run build; \ |
| 36 | + elif [ -f package-lock.json ]; then npm run build; \ |
| 37 | + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ |
| 38 | + else echo "Lockfile not found." && exit 1; \ |
| 39 | + fi |
| 40 | + |
| 41 | +# Production image, copy all the files and run next |
| 42 | +FROM base AS runner |
| 43 | +WORKDIR /app/next-server |
| 44 | + |
| 45 | +ENV NODE_ENV=production |
| 46 | + |
| 47 | +RUN addgroup --system --gid 1001 nodejs |
| 48 | +RUN adduser --system --uid 1001 nextjs |
| 49 | + |
| 50 | +COPY --from=builder /app/next-server/public ./public |
| 51 | + |
| 52 | +# Set the correct permission for prerender cache |
| 53 | +RUN mkdir .next |
| 54 | +RUN chown nextjs:nodejs .next |
| 55 | + |
| 56 | +# Automatically leverage output traces to reduce image size |
| 57 | +COPY --from=builder --chown=nextjs:nodejs /app/next-server/.next/standalone ./ |
| 58 | +COPY --from=builder --chown=nextjs:nodejs /app/next-server/.next/static ./.next/static |
| 59 | + |
| 60 | +USER nextjs |
| 61 | + |
| 62 | +# Allow the running process to write model files to the cache folder. |
| 63 | +RUN mkdir -p /app/next-server/node_modules/@huggingface/transformers/.cache |
| 64 | +RUN chmod 777 -R /app/next-server/node_modules/@huggingface/transformers/.cache |
| 65 | + |
| 66 | +EXPOSE 3000 |
| 67 | + |
| 68 | +ENV PORT=3000 |
| 69 | +ENV HOSTNAME="0.0.0.0" |
| 70 | +CMD ["node", "server.js"] |
0 commit comments