0

I have a Rails app that is containerized and running correctly in development on my local machine. When I pull it from Docker Hub to the production server, though, I experience some weird behavior with webpacker, wherein webpacker seems to be looking in a wrong directory (/app/app/public/packs) for my manifest/files, instead of the place they actually exist (/app/public/packs).

My guess

My guess is that there is a conflict caused by the Rails app living in /app, but also having the standard app directory that all Rails projects have (/app/app in my container). I think there is a name/path conflict/misreading somewhere. I could be wrong.

Here's what happens:

After pulling the image and bringing it up with docker-compose, I issue the command to compile webpacker assets:

docker-compose exec app bundle exec rake webpacker:compile

Which outputs:

Compiling...
Compiled all packs in /app/public/packs

Okay, fine. That path appears correct, because in the Rails app container, the Rails project lives in /app, and there indeed is a /app/public/paths directory in the container.

The error

But when I attempt to access the Rails app via my browser, I get an error. Inside of log/production.log, I find this:

F, [2020-08-06T17:20:48.069394 #6] FATAL -- : [fc2bd325-78f8-48f6-b45a-920fd3105bfd]
[fc2bd325-78f8-48f6-b45a-920fd3105bfd] ActionView::Template::Error (Webpacker can't find media/images/login-logo.png 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.
Your manifest contains:
{
}
):

There are two major points to this log:

  1. Webpacker is looking for my manifest /app/public/packs/manifest.json
  2. Webpacker says my manifest is empty.

But the manifest at that location isn't empty:

/app # cat /app/public/packs/manifest.json
{
  "application.css": "/packs/css/application-93da03a6.css",
  "application.js": "/packs/js/application-3d0a7e3a09e279deed6f.js",
  "application.js.map": "/packs/js/application-3d0a7e3a09e279deed6f.js.map",
  "entrypoints": {
    "application": {
      "css": [
        "/packs/css/application-93da03a6.css"
      ],
      "js": [
        "/packs/js/application-3d0a7e3a09e279deed6f.js"
      ],
      "js.map": [
        "/packs/js/application-3d0a7e3a09e279deed6f.js.map"
      ]
    }
  },
  "media/images/header-logo.png": "/packs/media/images/header-logo-af4d5c3bf874bd9aff042c8946a2ea86.png",
  "media/images/login-logo.png": "/packs/media/images/login-logo-7ecc5f8267bf89503ee87f8deb6fac66.png"
}/app #

A "fix"

This seemed weird to me, until I got an idea: what if the container's /app directory is getting confused with the rails app directory, which is located at /app/app in the container. What if webpacker is actually looking at /app/app/public/..., which doesn't exist?

To test this theory, I ran:

ln -s /app/public/ /app/app/public
bin/rails restart

And bam. The Rails app appeared in my browser correctly. Everything works.

Why is this happening, and what should I do?

So it seems like my theory could be correct. My question is: why is this happening, and what should I do?

I don't think I've changed any webpacker config settings to anything weird. I've listed my webpacker config below, in full. What should I do to sort this behavior out? What is causing webpacker to look in the wrong directory?

My webpacker config:

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: true

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .mjs
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'


test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true
David Gay
  • 1,094
  • 2
  • 16
  • 32
  • I think you are right, what we do in our docker containers is put the rails app in myapp. `RUN mkdir -p /home/app/myapp` `WORKDIR /home/app/myapp` – dbugger Aug 06 '20 at 19:30
  • See [this](https://stackoverflow.com/questions/58520418/rails-webpacker-compile-error-on-production-enviorment) SO post it may solve the issue. – Giuseppe Schembri Aug 07 '20 at 18:10

0 Answers0