首页 > 解决方案 > Rails 6 Docker + NGINX 服务于 webpacker 资产

问题描述

我正在尝试使用 Docker 部署我的 Rails 6.1.4 应用程序,并让 NGINX 处理资产服务。现在一切正常,除了当我尝试访问我的服务器时0.0.0.0:3000找不到这些资产时似乎找不到这些资产。

nginx.conf

upstream app {
    server app:3000;
}

server {
    listen 80;
    server_name localhost;
    keepalive_timeout 5;
    
    root $RAILS_ROOT/public;

    access_log $RAILS_ROOT/log/access.log;
    error_log $RAILS_ROOT/log/error.log;

    location @app {
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://app;
    }

    location / {
      try_files $uri @app;
    }

    location ~ ^/(assets|packs)/ {
      gzip_static on;
      expires max;
      add_header Cache-Control public;
    }

    location = /favicon.ico {
      access_log off;
      log_not_found off;
    }
    
    location = /robots.txt  {
      access_log off;
      log_not_found off;
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /500.html {
    }
}

nginx.Dockerfile

FROM nginx

RUN apt-get update -qq && apt-get -y install apache2-utils

ENV RAILS_ROOT /var/www/dicom_dwh
WORKDIR $RAILS_ROOT

RUN mkdir log
COPY public public/
COPY nginx.conf /tmp/docker.nginx

RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf

EXPOSE 80
CMD [ "nginx", "-g", "daemon off;" ]

应用程序.Dockerfile

FROM ruby:3.0.1

# Install Node 14 and yarn
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get update -qq && apt-get install -qq --no-install-recommends \
    nodejs \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
RUN npm install -g yarn@1

ENV RAILS_ROOT /var/www/dicom_dwh
ENV RAILS_ENV production
ENV RACK_ENV production

WORKDIR $RAILS_ROOT
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
COPY . .

RUN bundle exec rails assets:precompile

EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

码头工人-compose.yml

services:
    app:
        build:
            context: .
            dockerfile: app.Dockerfile
        container_name: application_instance
        command: bash -c "bundle exec puma -C config/puma.rb"
        volumes:             
            - .:/app
            - node-modules:/app/node_modules
        ports:
            - "3000:3000"
        depends_on:
            - database
            - redis
    database:
        image: postgres
        container_name: database_instance
        restart: always
        volumes:      
            - db_data:/var/lib/postgresql/data
        ports:
            - "5432:5432"
        env_file:
            - .env
        environment:
            POSTGRES_USER: ${POSTGRES_USER}
            POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
            POSTGRES_DB: ${POSTGRES_PRODUCTION_DB}
    nginx:
        build:
            context: .
            dockerfile: nginx.Dockerfile
        depends_on:
            - app
        ports:
            - 80:80
        volumes:
            - ./public:/app/public
    redis:
        image: redis
        container_name: redis_instance
        command: redis-server
        ports:
            - "6379"
        volumes:
            - redis:/data
volumes:
    db_data:
    redis:
    node-modules:

服务器启动得很好,但我无法访问我的资产:

没有提供任何资产

有谁知道这里出了什么问题?提前致谢!

标签: ruby-on-railsdockernginxdocker-composewebpacker

解决方案


推荐阅读