-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created a Multistage Dockerfile and Reduced the Image size. #554
Conversation
Created a multistage Dockerfile to reduce the image size upto 50%.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@Helion55 is attempting to deploy a commit to the al1abb-team Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe changes in this pull request involve significant updates to the Dockerfile for a Node.js application. The base image has been upgraded to Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
Dockerfile (3)
12-14
: CombineRUN
commands to reduce image layersCombining multiple
RUN
directives into a single command reduces the number of layers in your image, which can further decrease the image size.Apply this diff to optimize the Dockerfile:
FROM node:22-alpine AS production -RUN addgroup --system --gid 1001 nodejs -RUN adduser --system --uid 1001 nextjs +RUN addgroup --system --gid 1001 nodejs \ + && adduser --system --uid 1001 -G nodejs nextjs USER nextjs
15-18
: Ensure only production dependencies are included in the final imageCopying
node_modules
from the build stage may include development dependencies, increasing the image size unnecessarily. To include only production dependencies, consider installing them in the production stage.Here's how you can modify the production stage:
COPY --from=build --chown=nextjs:nodejs /app/package.json ./package.json +COPY --from=build --chown=nextjs:nodejs /app/package-lock.json ./package-lock.json +RUN npm ci --only=production --ignore-scripts COPY --from=build --chown=nextjs:nodejs /app/.next ./.next COPY --from=build --chown=nextjs:nodejs /app/public ./publicThis approach installs only production dependencies, reducing the image size further.
10-21
: SetNODE_ENV
to'production'
in the production stageSetting the
NODE_ENV
environment variable ensures that Node.js runs in production mode, which can improve performance and disable debug features.Add this line to the production stage:
FROM node:22-alpine AS production +ENV NODE_ENV=production RUN addgroup --system --gid 1001 nodejs
RUN addgroup --system --gid 1001 nodejs | ||
RUN adduser --system --uid 1001 nextjs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specify the USER
to run the application as non-root
While you've created the nodejs
group and nextjs
user, the application will still run as root unless you specify the USER
directive. Running as a non-root user enhances security by limiting permissions.
Apply this diff to set the user in the production stage:
FROM node:22-alpine AS production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
+USER nextjs
COPY --from=build --chown=nextjs:nodejs /app/.next ./.next
Committable suggestion skipped: line range outside the PR's diff.
About this PR
Created a multistage Dockerfile to reduce the image size upto 50%. Which is now 1.08Gb from 2.5Gb. Also added container root user security .
Steps followed
Feature added
Solved #537
Summary by CodeRabbit
New Features
Improvements