Skip to content
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

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

Helion55
Copy link
Contributor

@Helion55 Helion55 commented Dec 12, 2024

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

  1. Divided the Dockerfile in 2 stages, Build and Production.
  2. Copied only necessary files to the next stage to reduce the final Image size.
  3. Created a User Group named nodejs and a User nextjs to the group.
  4. Changed the file ownership and assigned them to new user.

Feature added

Solved #537

Summary by CodeRabbit

  • New Features

    • Enhanced security by creating a dedicated user and group for running the application.
    • Updated the application to use a production-ready command for starting the application.
  • Improvements

    • Upgraded the base image for the application to a newer version.
    • Implemented a multi-stage build process for better separation of build and runtime environments.

Created a multistage Dockerfile to reduce the image size upto 50%.
Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
invoify ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 12, 2024 7:23am

Copy link

vercel bot commented Dec 12, 2024

@Helion55 is attempting to deploy a commit to the al1abb-team Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

coderabbitai bot commented Dec 12, 2024

Walkthrough

The changes in this pull request involve significant updates to the Dockerfile for a Node.js application. The base image has been upgraded to node:22-alpine, and a multi-stage build process has been implemented. This includes a dedicated build stage for installing dependencies and building the application, followed by a production stage that enhances security by creating a non-root user for running the application. The start command has been modified to reflect a production environment.

Changes

File Change Summary
Dockerfile - Updated base image from node:lts-alpine to node:22-alpine.
- Added multi-stage build: build and production.
- Changed command from CMD ["npm", "run", "dev"] to CMD npm start.
- Created a new user and group for running the application, enhancing security.

Possibly related PRs

  • added Dockerfile #542: The main PR modifies the existing Dockerfile, which was initially added in this PR, indicating a direct relationship in terms of changes made to the Dockerfile for the Node.js application.

🐰 In a world of code, so bright and new,
A Dockerfile shines, with changes so true.
From dev to start, we leap with delight,
Security's our friend, as we take flight.
With stages defined, our app's set to run,
Hopping to production, oh what fun! 🌟


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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: Combine RUN commands to reduce image layers

Combining 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 image

Copying 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 ./public

This approach installs only production dependencies, reducing the image size further.


10-21: Set NODE_ENV to 'production' in the production stage

Setting 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
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0d9cf06 and 335d938.

📒 Files selected for processing (1)
  • Dockerfile (1 hunks)

Comment on lines +12 to +13
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

@al1abb al1abb merged commit c115e73 into al1abb:master Dec 12, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants