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

Remove node_modules when no node runtime is installed in the image ? #111

Open
Intrepidd opened this issue Jul 22, 2024 · 5 comments
Open

Comments

@Intrepidd
Copy link

In some cases (when grover is present for exemple) we need a node runtime to be able to launch some JS deps from ruby.

But, when no node runtime is installed, node_modules still lingers in the final image, which can consume a lot space.

I was wondering if this was on purpose, or if we could indeed remove the node_modules folder in this specific case ?

If so, I'm happy to write a PR for it if you give me some pointers on the preferred way to do it (just rm -r ? tweak the COPY statement ? )

@louim
Copy link
Collaborator

louim commented Jul 22, 2024

Hey! The node_modules folder is added in the dockerignore file, which should remove it from the final image. Is that not the case for you?

@Intrepidd
Copy link
Author

No this is not the case for me, I think the docker ignored files only apply when copying from the host to the container but not when copying from one container to another.

Don't you have the folder in your containers ? I can try to make a reproductible case if needed.

@Intrepidd
Copy link
Author

Here's an easy reproduction :

cd /tmp
rails new poc --js esbuild
cd poc
npm add esbuild @hotwired/stimulus @hotwired/turbo-rails
bundle add dockerfile-rails
rm Dockerfile
rails g dockerfile
docker build . -t poc
docker run --rm -it poc ls node_modules

@Intrepidd
Copy link
Author

Intrepidd commented Jul 23, 2024

So I digged a bit more and indeed, the .dockerignore file is only used when copying from the build context, not when copying from a stage to another.

My naive solution to fix this would be to RUN rm -rf node_modules in the final stage , unless using_execjs?

@rubys
Copy link
Collaborator

rubys commented Jul 23, 2024

I'm surprised this hasn't been noticed before.

Perhaps run the rm -rf in the later portion of the build stage? This will reduce the size of intermediate images as well as perhaps speed up the later COPY statement.

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

No branches or pull requests

3 participants