Rails app - problem with node_modules when deploying

I am trying to deploy a Rails app and in the final stages of the build of the image it fails with

------
 > [stage-4 5/8] COPY --from=node_modules /app/node_modules /app/node_modules:
------
Error failed to fetch an image or build from source: error building: failed to compute cache key: "/app/node_modules" not found: not found

What can I do? Thanks

1 Like

Hi @vitobotta

Have you tried building the docker image locally to test it?

It looks like the docker image builder can’t find the folder specified.

In a preceding build step, yarn build failed to create a node-modules directory, causing a later step to fail when it tries to copy the results of the previous step. Were there errors earlier in this build?

I am facing the same issue.
One thing I noticed tho is that node_modules is put in .dockerignore file.
So probably a stupid question, but maybe that is affecting the deploy to fail?

Odd, as the Dockerfiles we now produce don’t have that line. We are now basing our Rails dockerfiles of of the ones that will be provided by Rails 7.1. We are also contributing to that effort, and bringing it to applications of all Rails releases. By teaming up, we are producing Dockerfiles with better documentation, testing, and support. Come join us!

To get started, if you haven’t already done so, make sure your source is saved.

git add .
git commit -a -m snapshot

Next, regenerate your Dockerfile:

bundle add dockerfile-rails --optimistic --group development
bin/rails generate dockerfile

It will prompt you when it needs to overwrite a file. Accept those changes.

Review this FAQ: Dockerfiles and fly.toml · Fly Docs ; in particular look at the scaling section, and either run the command again with --no-prepare or remove/comment out the deploy section in your fly.tom.

Now you should be ready to deploy.

If you are still having problems, it probably means that something you have in your package.json won’t build. Providing your package.json might help me spot the problem. In rare cases the problem is in your yarn.lock file (or package-lock.json). Deleting that and rebuilding can often fix such problems.

If you don’t like what you see, you can undo these changes by running git checkout .

Reasonable guess, but no you don’t want to upload your node_modules to the build machine. For starters, this directory is often huge. And depending on your configuration it was built for Microsoft Windows, an M1 Mac, or other configuration when we need is one that works on i686 Debian Linux, so we rebuild your node_modules directory on such a machine.