evert revised this gist 2 years ago. Go to revision
2 files changed, 130 insertions
nestjs.Dockerfile(file created)
| @@ -0,0 +1,62 @@ | |||
| 1 | + | ############################### | |
| 2 | + | # BUILD FOR LOCAL DEVELOPMENT # | |
| 3 | + | ############################### | |
| 4 | + | ||
| 5 | + | FROM node:18-alpine As development | |
| 6 | + | ||
| 7 | + | # Create app directory | |
| 8 | + | WORKDIR /usr/src/app | |
| 9 | + | ||
| 10 | + | # Copy application dependency manifests to the container image. | |
| 11 | + | # A wildcard is used to ensure copying both package.json AND package-lock.json (when available). | |
| 12 | + | # Copying this first prevents re-running npm install on every code change. | |
| 13 | + | COPY --chown=node:node package*.json ./ | |
| 14 | + | ||
| 15 | + | # Install app dependencies using the `npm ci` command instead of `npm install` | |
| 16 | + | RUN npm ci | |
| 17 | + | ||
| 18 | + | # Bundle app source | |
| 19 | + | COPY --chown=node:node . . | |
| 20 | + | ||
| 21 | + | # Use the node user from the image (instead of the root user) | |
| 22 | + | USER node | |
| 23 | + | ||
| 24 | + | ######################## | |
| 25 | + | # BUILD FOR PRODUCTION # | |
| 26 | + | ######################## | |
| 27 | + | ||
| 28 | + | FROM node:18-alpine As build | |
| 29 | + | ||
| 30 | + | WORKDIR /usr/src/app | |
| 31 | + | ||
| 32 | + | COPY --chown=node:node package*.json ./ | |
| 33 | + | ||
| 34 | + | # In the previous development stage we ran `npm ci` which installed all dependencies | |
| 35 | + | # so we can copy over the node_modules directory from the development image | |
| 36 | + | COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules | |
| 37 | + | ||
| 38 | + | COPY --chown=node:node . . | |
| 39 | + | ||
| 40 | + | # Run the build command which creates the production bundle | |
| 41 | + | RUN npm run build | |
| 42 | + | ||
| 43 | + | # Set NODE_ENV environment variable | |
| 44 | + | ENV NODE_ENV production | |
| 45 | + | ||
| 46 | + | # Passing in --omit=dev ensures that only the production dependencies are installed. | |
| 47 | + | RUN npm ci --omit=dev && npm cache clean --force | |
| 48 | + | ||
| 49 | + | USER node | |
| 50 | + | ||
| 51 | + | ############## | |
| 52 | + | # PRODUCTION # | |
| 53 | + | ############## | |
| 54 | + | ||
| 55 | + | FROM node:18-alpine As production | |
| 56 | + | ||
| 57 | + | # Copy the bundled code from the build stage to the production image | |
| 58 | + | COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules | |
| 59 | + | COPY --chown=node:node --from=build /usr/src/app/dist ./dist | |
| 60 | + | ||
| 61 | + | # Start the server using the production build | |
| 62 | + | CMD [ "node", "dist/main.js" ] | |
nextjs.Dockerfile(file created)
| @@ -0,0 +1,68 @@ | |||
| 1 | + | ################ | |
| 2 | + | # DEPENDENCIES # | |
| 3 | + | ################ | |
| 4 | + | FROM node:18-alpine AS deps | |
| 5 | + | RUN apk add --no-cache libc6-compat | |
| 6 | + | USER node | |
| 7 | + | WORKDIR /app | |
| 8 | + | COPY --chown=node:node package*.json ./ | |
| 9 | + | RUN npm ci | |
| 10 | + | ||
| 11 | + | ########### | |
| 12 | + | # BUILDER # | |
| 13 | + | ########### | |
| 14 | + | FROM node:18-alpine AS builder | |
| 15 | + | ||
| 16 | + | USER node | |
| 17 | + | WORKDIR /app | |
| 18 | + | ||
| 19 | + | COPY --chown=node:node --from=deps /app/node_modules ./node_modules | |
| 20 | + | COPY --chown=node:node . . | |
| 21 | + | ||
| 22 | + | ENV NODE_ENV production | |
| 23 | + | ENV NEXT_PUBLIC_URL APP_NEXT_PUBLIC_URL | |
| 24 | + | ENV NEXT_PUBLIC_UPLOADS_URL APP_NEXT_PUBLIC_UPLOADS_URL | |
| 25 | + | RUN npm run build | |
| 26 | + | ||
| 27 | + | RUN npm ci --omit=dev && npm cache clean --force | |
| 28 | + | ||
| 29 | + | ############## | |
| 30 | + | # PRODUCTION # | |
| 31 | + | ############## | |
| 32 | + | FROM node:18-alpine AS runner | |
| 33 | + | ||
| 34 | + | USER node | |
| 35 | + | WORKDIR /app | |
| 36 | + | ||
| 37 | + | ENV NODE_ENV production | |
| 38 | + | ||
| 39 | + | COPY --chown=node:node --from=builder /app/node_modules ./node_modules | |
| 40 | + | COPY --chown=node:node --from=builder /app/package*.json ./ | |
| 41 | + | COPY --chown=node:node --from=builder /app/entrypoint.sh ./ | |
| 42 | + | ||
| 43 | + | COPY --chown=node:node --from=builder /app/next.config.js ./ | |
| 44 | + | COPY --chown=node:node --from=builder /app/public ./public | |
| 45 | + | COPY --chown=node:node --from=builder /app/.next ./.next | |
| 46 | + | ||
| 47 | + | EXPOSE 3000 | |
| 48 | + | ||
| 49 | + | ENTRYPOINT ["/app/entrypoint.sh"] | |
| 50 | + | CMD ["node_modules/.bin/next", "start"] | |
| 51 | + | ||
| 52 | + | ###################### | |
| 53 | + | # /app/entrypoint.sh # | |
| 54 | + | ###################### | |
| 55 | + | ||
| 56 | + | #!/bin/sh | |
| 57 | + | set -Ex | |
| 58 | + | ||
| 59 | + | function apply_path { | |
| 60 | + | test -n "$NEXT_PUBLIC_URL" | |
| 61 | + | test -n "$NEXT_PUBLIC_UPLOADS_URL" | |
| 62 | + | ||
| 63 | + | find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_NEXT_PUBLIC_URL#$NEXT_PUBLIC_URL#g" | |
| 64 | + | find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_NEXT_PUBLIC_UPLOADS_URL#$NEXT_PUBLIC_UPLOADS_URL#g" | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | apply_path | |
| 68 | + | exec "$@" | |
Newer
Older