Skip to content

Instantly share code, notes, and snippets.

@dattp
Last active June 10, 2024 03:49
Show Gist options
  • Save dattp/7c6babfbd04b6fbcfd9691dd0899c66c to your computer and use it in GitHub Desktop.
Save dattp/7c6babfbd04b6fbcfd9691dd0899c66c to your computer and use it in GitHub Desktop.
DOCKER_BUILDKIT=1 docker build -t webapp:v1 --build-arg NEXT_ENVIRONMENT=development --build-arg BUILDKIT_INLINE_CACHE=1 .
# Install dependencies only when needed
FROM node:18-alpine AS builder
WORKDIR /app
RUN apk add --no-cache libc6-compat && rm -rf /var/cache/apk/*
ARG NEXT_ENVIRONMENT
ENV NODE_ENV $NEXT_ENVIRONMENT
ENV NEXT_PUBLIC_ENV $NEXT_ENVIRONMENT
# RUN mkdir -p .next/cache
COPY package.json yarn.lock ./
# Use Docker BuildKit to mount the cache
RUN --mount=type=cache,target=/root/.yarn/cache \
--mount=type=cache,target=/app/.next/cache \
yarn install --pure-lockfile
COPY . .
COPY .env.${NEXT_ENVIRONMENT} .env.production
# Build the Next.js application
RUN --mount=type=cache,target=/app/.next/cache \
yarn build
# Remove dev dependencies
RUN --mount=type=cache,target=/app/.next/cache \
yarn install --pure-lockfile --prod
FROM node:18-alpine
WORKDIR /app
EXPOSE 3000
ENV PORT 3000
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
USER nextjs
CMD ["npm", "run", "start"]
@dattp
Copy link
Author

dattp commented Jun 5, 2024

log build in local

[+] Building 271.7s (24/24) FINISHED                                                                                    
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 37B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 35B                                                                                   0.0s
 => [internal] load metadata for docker.io/library/node:18-alpine                                                  1.0s
 => importing cache manifest from webapp:cache                                                                     0.0s
 => [internal] load build context                                                                                  0.3s
 => => transferring context: 268.28kB                                                                              0.3s
 => [stage-1 1/9] FROM docker.io/library/node:18-alpine@sha256:cf350f8bb497d82471f1f735df5d6d3321138be3b9f7f84ad1  0.0s
 => => resolve docker.io/library/node:18-alpine@sha256:cf350f8bb497d82471f1f735df5d6d3321138be3b9f7f84ad10a4b86a4  0.0s
 => CACHED [stage-1 2/9] WORKDIR /app                                                                              0.0s
 => CACHED [builder  3/10] RUN apk add --no-cache libc6-compat && rm -rf /var/cache/apk/*                          0.0s
 => CACHED [builder  4/10] COPY package.json yarn.lock ./                                                          0.0s
 => CACHED [builder  5/10] RUN --mount=type=cache,target=/root/.yarn/cache     --mount=type=cache,target=/app/.ne  0.0s
 => CACHED [builder  6/10] RUN yarn install --pure-lockfile                                                        0.0s
 => [builder  7/10] COPY . .                                                                                       7.9s
 => [builder  8/10] COPY .env.development .env.production                                                          0.0s
 => [builder  9/10] RUN yarn build                                                                               231.4s
 => [builder 10/10] RUN yarn install --pure-lockfile --prod                                                        6.0s
 => CACHED [stage-1 3/9] RUN addgroup --system --gid 1001 nodejs                                                   0.0s
 => CACHED [stage-1 4/9] RUN adduser --system --uid 1001 nextjs                                                    0.0s
 => CACHED [stage-1 5/9] COPY --from=builder /app/public ./public                                                  0.0s
 => CACHED [stage-1 6/9] COPY --from=builder /app/package.json ./package.json                                      0.0s
 => CACHED [stage-1 7/9] COPY --from=builder /app/node_modules ./node_modules                                      0.0s
 => [stage-1 8/9] COPY --from=builder /app/.next/standalone ./                                                     1.9s
 => [stage-1 9/9] COPY --from=builder /app/.next/static ./.next/static                                             0.2s
 => exporting to image                                                                                             0.1s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:b19f4d33f93a7a3d8dad387b208f1ee36a8c9c5eb0138e99b8f00bf10d79dff9                       0.0s
 => => naming to docker.io/library/webapp:v1                                                                       0.0s
 => exporting cache                                                                                                0.0s
 => => preparing build cache for export   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment