Precompiling assets

In development, by default, Rails compiles our assets for each request so that our changes are picked up automatically. However, typically in production, we precompile our assets once and then serve them up as static files for faster load times. Rails provides the following Rake task for this:

bin/rails assets:precompile

Up until now, the changes we have needed to make for our app to run in production have just been config changes or tweaks that would be fine in development too. Here, however, the production version of our app needs additional files: the compiled assets.

How do we achieve this with our Docker setup?

The solution is to create a second, production-flavored image that precompiles the assets at build time, so the compiled assets are baked into the image itself. Generally, it’s a good idea to keep your development environment as similar to production as possible. However, some changes, like this need to precompile assets for production and require that our development and production environments diverge slightly. file

For production, we will need a, which will be similar to the development Dockerfile.

We need to make a change to precompile assets by adding the following line just before the ENTRYPOINT command:

Get hands-on with 1200+ tech skills courses.