Glenn Jones

Hello 👋 Welcome to my corner of the internet. I write here about the different challenges I encounter, and the projects I work on. Find out more about me.

Fixing 'No such middleware to insert before: ActionDispatch::Static' when precompiling rails assets during docker build

TL;DR add RAILS_SERVE_STATIC_FILES=true in front of precompile, like so: RAILS_SERVE_STATIC_FILES=true SECRET_KEY_BASE=dummyvalue rails assets:precompile --trace to prevent the error during precompilation when the application environment is not yet available

While converting an existing rails app from buildpack-based deployments to a docker-based approach, I ran into the following error: No such middleware to insert before: ActionDispatch::Static

The error ocurred during the rails assets:precompile step while the container was being built. A google search of the error pointed in various directions with different resolution steps that finally did not directly resolve the error for me.

However, this comment points out that it depends on config.public_file_server.enabled, which I configure through environment variables in the application (RAILS_SERVE_STATIC_FILES).

That explain the issue: the problem lies in the fact that this config is part of the application environment, which is made available when running the app, which occurs only after the build has completed. So during the build step, this config was not yet available.

You can solve this by adding a build environment argument, or simply hard coding the variable into the command. I chose the latter as it is more explicit. So in my Dockerfile I simply added:

RUN RAILS_SERVE_STATIC_FILES=true SECRET_KEY_BASE=dummyvalue rails assets:precompile --trace

Links

Previous: Refer to a local gem within a ruby project
Next: Fixing 'can’t start new thread' and 'thread: resource temporarily unavailable' when running a parallelized containerized selenium setup