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

🐋 refactor(Dockerfile.multi): Optimize client build by caching npm install step #2275

Merged
merged 2 commits into from
Apr 4, 2024

Conversation

ochen1
Copy link
Contributor

@ochen1 ochen1 commented Apr 2, 2024

Pull Request Template

Summary

Don't perform an additional npm install unless ./client/package*.json has actually changed. This allows for minor edits in code (that don't necessitate the introduction of new packages) without the need to rerun npm install, which is notoriously slow and painstaking.

Change Type

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes
  • Any changes dependent on mine have been merged and published in downstream modules.
  • New documents have been locally validated with mkdocs

@ochen1 ochen1 changed the title Optimize client build by caching npm install step 🐋 fix(Dockerfile): Optimize client build by caching npm install step Apr 2, 2024
@danny-avila
Copy link
Owner

Thanks for this, I will have to test it later but this makes sense to me

@danny-avila
Copy link
Owner

this creates an unusual issue on a local docker setup where hitting localhost:3080 is in error state, but running vite (npm run frontend:dev) over it is fine. There is an issue with QueryClient, and it could be that data-provider does not resolve correctly during build time since npm install now happens before the package is manually moved.

Is the caching issue because install runs after the client directory is copied? Did you get it running after building? It's strange because there are no errors during building.

image

And I can't debug this properly because the vite dev server runs fine.

It's all good without this update:
image

I built the image with your changes, with no cache, a few times along as well as without to compare.

@danny-avila
Copy link
Owner

I just thought and have the suspicion that the remote package of librechat-data-provider hosted on npm is probably being pulled, and may be out of sync.

@ochen1
Copy link
Contributor Author

ochen1 commented Apr 4, 2024

You may be right. librechat-data-provider is part of the node_modules directory, which could change the behavior of npm install. I had accidentally disregarded this possibility as I usually don't mess with directories within node_modules without using a package manager. Although I did not receive an error in my limited testing, I can see why this optimization might introduce unintended behaviour under certain conditions. The new commit should fix this. Can you try again?

Thank you for your thorough review.

Is the caching issue because install runs after the client directory is copied? Did you get it running after building? It's strange because there are no errors during building.

Despite the title of this issue (perhaps poorly worded on my part), this patch implements a speed optimization, not a "fix" per se. I suggest separating the installation of npm dependencies into a distinct cacheable unit of execution by performing the COPY of package.json files and the RUN npm install step before copying the entire client source code. This allows Docker to cache the installed dependencies layer, avoiding redundant npm install operations when the dependencies remain unchanged across builds. This way, simply modifying the source code (which usually won't change client/package*.json) will not make us wait for an additional npm install operation. Instead, Docker will simply retrieve it from cache, which is much faster.

This is aligned with the 10th tip on the Intro Guide to Dockerfile Best Practices.

@danny-avila
Copy link
Owner

You may be right. librechat-data-provider is part of the node_modules directory, which could change the behavior of npm install. I had accidentally disregarded this possibility as I usually don't mess with directories within node_modules without using a package manager. Although I did not receive an error in my limited testing, I can see why this optimization might introduce unintended behaviour under certain conditions. The new commit should fix this. Can you try again?

Thank you for your thorough review.

Is the caching issue because install runs after the client directory is copied? Did you get it running after building? It's strange because there are no errors during building.

Despite the title of this issue (perhaps poorly worded on my part), this patch implements a speed optimization, not a "fix" per se. I suggest separating the installation of npm dependencies into a distinct cacheable unit of execution by performing the COPY of package.json files and the RUN npm install step before copying the entire client source code. This allows Docker to cache the installed dependencies layer, avoiding redundant npm install operations when the dependencies remain unchanged across builds. This way, simply modifying the source code (which usually won't change client/package*.json) will not make us wait for an additional npm install operation. Instead, Docker will simply retrieve it from cache, which is much faster.

This is aligned with the 10th tip on the Intro Guide to Dockerfile Best Practices.

Thanks for desiring to apply best practices here. I'm testing now and will merge following successful build

@danny-avila danny-avila changed the title 🐋 fix(Dockerfile): Optimize client build by caching npm install step 🐋 refactor(Dockerfile.multi): Optimize client build by caching npm install step Apr 4, 2024
@danny-avila danny-avila merged commit d0d8e47 into danny-avila:main Apr 4, 2024
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.

None yet

2 participants