Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

feat: Added support for Multi-Stage Docker build #827

Merged
merged 6 commits into from
Nov 19, 2023

Conversation

SaptarshiSarkar12
Copy link
Contributor

@SaptarshiSarkar12 SaptarshiSarkar12 commented Nov 18, 2023

Fixes Issue

Fixes #814

Changes proposed

  • I have added three stages - deps, builder and runner to install dependencies, build the source code and run (serve) the build directory respectively.
  • I have used pm2 to serve the build directory as serve did not work for all the other webpages and Nginx failed for the same reason.

Check List (Check all the boxes which are applicable)

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • All new and existing tests passed.
  • This PR does not contain plagiarized content.
  • The title of my pull request is a short description of the requested changes.

Screenshots

  • Successful Docker build
    image
  • Website is Live!
    image

Summary by CodeRabbit

  • Chores

    • Updated .dockerignore to enhance build context by excluding unnecessary files and directories such as configuration files, documentation, and dependencies directory.
  • Refactor

    • Restructured the Dockerfile to implement multi-stage builds, improving caching and reducing the final image size.
  • New Features

    • The application now uses Node.js 17 for better performance and newer features.
    • Integrated pm2 for efficient process management when serving the application.
  • Style

    • Changed the Dockerfile CMD instruction to ENTRYPOINT to better handle the serving of the application using pm2.

Copy link

coderabbitai bot commented Nov 18, 2023

Walkthrough

The Dockerfile has been restructured to implement a multi-stage build process, optimizing the final image size and improving caching. The .dockerignore file has been updated to exclude unnecessary files from the build context, further reducing the image size. These changes streamline the container management and distribution process.

Changes

File Change Summary
.dockerignore Excluded config, documentation, and certain folders
Dockerfile Implemented multi-stage build and added pm2

Assessment against linked issues

Objective Addressed Explanation
#814: Implement Multi-Stage Build in Dockerfile
Insert COPY package*.json ./ before COPY . . in Dockerfile

Poem

As winter whispers, the code does dance,
In Docker's embrace, it finds its chance.
Through layers built, and sizes shrank,
A rabbit hops, with joy, to the bank. 🐰❄️💾


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

@SaptarshiSarkar12 SaptarshiSarkar12 marked this pull request as ready for review November 18, 2023 08:11
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7cb01f1 and 5794a8a.
Files selected for processing (2)
  • .dockerignore (1 hunks)
  • Dockerfile (1 hunks)
Files skipped from review due to trivial changes (1)
  • .dockerignore
Additional comments: 1
Dockerfile (1)
  • 1-13: The Multi-Stage Docker build is correctly implemented. Each stage is clearly defined and serves a specific purpose, which is good for maintainability and readability. The use of node:17-alpine is appropriate for a lightweight image, and separating the dependency installation (deps stage) from the build process (builder stage) can indeed leverage Docker's caching mechanism to speed up builds when dependencies don't change.

However, there are a few points to consider:

  • Ensure that Node.js 17 is compatible with all project dependencies, as some might not yet support newer Node.js versions.
  • Verify that pm2 is the best choice for the project's needs. While it's a powerful process manager, it might introduce unnecessary complexity if the application doesn't require its advanced features.
  • The --no-daemon flag in the ENTRYPOINT is important for Docker containers as it keeps the process in the foreground, which is a Docker best practice.
  • The --spa flag is used to serve single-page applications, which is suitable if the project is indeed an SPA.
  • The COPY --from=builder build ./build command assumes that the build process outputs to a build directory. Ensure this matches the actual output directory of the build process.
  • The EXPOSE 3000 instruction is good for documentation purposes, but remember that it doesn't actually publish the port. The port needs to be published when running the container using the -p flag with docker run.

Overall, the Dockerfile looks well-structured and optimized for the project's requirements. Just ensure that all the assumptions made (like Node.js version compatibility and the output directory) are indeed correct and that pm2 is the right choice for the project.

@SaptarshiSarkar12
Copy link
Contributor Author

@AnuragThePathak Should I also add docker-compose.yml file to automatically set the port required?

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5794a8a and 451f13d.
Files selected for processing (1)
  • Dockerfile (1 hunks)
Additional comments: 1
Dockerfile (1)
  • 1-13: The Multi-Stage Docker build is correctly implemented with separate stages for dependencies, building, and running the application. This should indeed improve caching and build times, as well as ensure a smaller final image size because only the necessary artifacts are included in the runner stage.
  • Using node:17-alpine is a good choice for keeping the image size small, but ensure that Node.js 17 is compatible with all project dependencies since it's not an LTS version and might introduce breaking changes or deprecations.
  • The COPY --from=builder build ./build command correctly copies only the build artifacts from the builder stage to the runner stage.
  • Installing pm2 globally in the runner stage is appropriate for process management.
  • The EXPOSE 3000 instruction indicates that the application will be available on port 3000, which is standard for web applications.
  • Using ENTRYPOINT with pm2 is a good practice to ensure that the process manager starts with the container and the specified parameters are used. However, ensure that the --no-daemon flag works as intended with the Docker container lifecycle.
  • The --spa flag is correctly used for serving single-page applications, which require all requests to be redirected to the index.html file.

Overall, the Dockerfile changes seem well-structured and thought out. Just ensure compatibility with Node.js 17 and that the pm2 flags are suitable for your deployment environment.

Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 451f13d and c3a9add.
Files selected for processing (1)
  • Dockerfile (1 hunks)
Files skipped from review due to trivial changes (1)
  • Dockerfile

@AnuragThePathak AnuragThePathak merged commit 7aaedf3 into WeMakeDevs:main Nov 19, 2023
4 checks passed
@SaptarshiSarkar12 SaptarshiSarkar12 deleted the issue-814 branch November 19, 2023 18:38
@SaptarshiSarkar12
Copy link
Contributor Author

Thank you @AnuragThePathak for reviewing and merging this PR 😄!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] Implement Multi-Stage Build in Dockerfile
2 participants