Home > Enterprise >  What's wrong with Rails assets:precompile and production environment?
What's wrong with Rails assets:precompile and production environment?

Time:02-14

Trying to finally deploy a rails 6 app - works well in development, but not in production. Here's the error from production.log:

F, [2022-02-09T18:03:53.317145 #1] FATAL -- : [6d02bb29-feb6-4fac-a7df-af9ff0d0ed74]   
[6d02bb29-feb6-4fac-a7df-af9ff0d0ed74] ActionView::Template::Error (Webpacker can't find application.css in /app/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
   unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.

Dockerfile:

FROM ruby:3.0.0

ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq
RUN apt-get install -y build-essential nodejs yarn imagemagick

RUN gem install bundler:2.2.3
ADD Gemfile* $APP_HOME/
RUN bundle install
ADD . $APP_HOME

RUN bundle exec rake assets:precompile

RUN yarn install --check-files

EXPOSE 3000
CMD ["rails","server","-b","0.0.0.0"]

I can run everything properly via docker-compose with RAILS_ENV set to 'development'. But as soon as I change RAILS_ENV to 'production', the above error happens.

I also checked config/environments/production.rb. I have this line in but whether or not it's in doesn't change the error (I tried to run the whole thing with both this line active and commented out - error stays the same).

config.assets.compile = true

I don't know where to look for answers...

CodePudding user response:

I found out what needs to be done. I spent a whole evening googling even more, and as my question comes up quite high now, here's the answer for others that run into the same problem. There were two things I needed to do:

  1. In the Dockerfile, I needed to run rake asset:precompile with RAILS_ENV set to production - to the corresponding line in the Dockerfile now looks like this:

    RUN RAILS_ENV=production bundle exec rake assets:precompile

And then I ALSO realized that I need to set the environment variable RAILS_SERVE_STATIC_FILES to.. well something (config/environments/production.rb checks whether it exists). So now my moviedb services of the docker-compose file looks like this:

  moviedb:
    image: tkhobbes/moviedb
    restart: unless-stopped
    depends_on:
      - mariamovie
    environment:
      MYSQL_ROOT_PASSWORD: redacted
      RAILS_ENV: production
      NEWMOVIEDB_DATABASE_PASSWORD: redacted
      RAILS_SERVE_STATIC_FILES: "yes"
    ports:
      - 3001:3000

With these two things fixed - rails PUMA now serves production!

  • Related