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

feat: optimize image size with multi-staged dockerfile #3313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Varun-Kolanu
Copy link

@Varun-Kolanu Varun-Kolanu commented Oct 21, 2024

Description

  • Updated Dockerfile to reduce the size of image formed for developer's convenience.
  • For this, I have updated the previously defined single-staged dockerfile into multi-staged one.
  • The image size originally was 1.53 GB and with the help of new dockerfile, it reduced to 1.28 GB (350 MB reduction!)

image

  • The reason for the working of this implementation is:
    1. Stage 1 (Dependency stage) installs the required dependencies and store it in that layer
    2. In Stage 2 (Development environment), we copy just the node modules from the previous stage and copy the source code. This reduces much of image size because we are excluding the files not needed like .npm, caches etc., that were created during the dependency installation stage.
  • I have also run the new docker image and everything is working well.

Related issue(s)
Fixes #3223
This PR successfully fixes this issue doing the intended changes. I tried so much to further reduce the size of image but concluded (in my opinion) that this may be the optimal size for the development docker image.

Summary by CodeRabbit

  • New Features

    • Introduced a multi-stage build process for improved Docker image optimization.
    • Enhanced dependency management by separating installation from the application build.
  • Bug Fixes

    • None reported.
  • Documentation

    • None reported.

Copy link

coderabbitai bot commented Oct 21, 2024

Walkthrough

The changes involve modifying the Dockerfile to implement a multi-stage build process. The first stage, named deps, installs dependencies using node:18-alpine, while the second stage, dev, copies the installed dependencies and application files to set up the development environment. This restructuring aims to optimize the Docker image size and build speed by separating dependency installation from the application build.

Changes

File Change Summary
Dockerfile Introduced a multi-stage build with deps for dependencies and dev for application setup.

Assessment against linked issues

Objective Addressed Explanation
Implement multi-stage Dockerfile to reduce image size and build time (3223)

🐇 In a world of code so bright,
A multi-stage build takes flight.
Dependencies fetched, oh what a sight,
Docker images now feel just right!
With each new change, we hop with glee,
Optimized builds, as happy as can be! 🌟


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

netlify bot commented Oct 21, 2024

Deploy Preview for asyncapi-website ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2bff358
🔍 Latest deploy log https://app.netlify.com/sites/asyncapi-website/deploys/67166e6babfd410008758f17
😎 Deploy Preview https://deploy-preview-3313--asyncapi-website.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@asyncapi-bot
Copy link
Contributor

⚡️ Lighthouse report for the changes in this PR:

Category Score
🔴 Performance 46
🟢 Accessibility 98
🟢 Best practices 92
🟢 SEO 100
🔴 PWA 33

Lighthouse ran on https://deploy-preview-3313--asyncapi-website.netlify.app/

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: 0

🧹 Outside diff range and nitpick comments (1)
Dockerfile (1)

12-27: Well-structured development stage with room for minor improvement.

The second stage, "dev", is implemented effectively:

  1. Consistent use of the node:18-alpine base image.
  2. Efficient copying of node_modules from the deps stage, avoiding temporary caches.
  3. Proper setup of the development environment with correct port exposure and NODE_ENV setting.

These changes successfully address the objectives outlined in issue #3223.

Consider adding a comment before the COPY command on line 20 to clarify its purpose:

COPY --from=deps /async/node_modules ./node_modules

+ # Copy application files
COPY . .

This addition would improve readability and make the purpose of each COPY command more explicit.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3a8f69a and 2bff358.

📒 Files selected for processing (1)
  • Dockerfile (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
Dockerfile (2)

3-11: Excellent implementation of the dependency installation stage!

The first stage, named "deps", is well-structured and follows best practices:

  1. Using a lightweight alpine base image.
  2. Copying only package files before running npm install, which leverages Docker's layer caching.
  3. Separating dependency installation from the main build, which optimizes build time and image size.

These changes align well with the PR objectives of optimizing the Docker image size and build speed.


1-27: Excellent implementation of a multi-stage Dockerfile for optimized development.

This PR successfully transforms the Dockerfile into a multi-stage build, addressing the objectives outlined in issue #3223. The key improvements are:

  1. Separation of dependency installation and application build stages.
  2. Efficient use of Docker layer caching.
  3. Reduction of the final image size by excluding unnecessary files and caches.

These changes should result in faster builds and a significantly smaller Docker image (reported 350 MB decrease), enhancing the development workflow.

To confirm the image size reduction, please run the following script:

This script will build the image and output its size. Please compare this with the previous image size to confirm the reported 350 MB reduction.

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.

[FEATURE] For making current docker file a multistaged one.
2 participants