使用Docker Rails 5 Postgres应用程序的Nginx路由

当我尝试将Dockerize Rails应用程序放入容器并在主机上运行Nginx时,出现了外部路由问题。我无法在rails应用程序容器中访问/ public。 相反,我可以在主机上看到/var/www/app/public

我如何从Nginx路由到Docker Rails容器?

nginx.conf:

 upstream puma_app { server 127.0.0.1:3000; } server { listen 80; client_max_body_size 4G; keepalive_timeout 10; error_page 500 502 504 /500.html; error_page 503 @503; server_name localhost app; root /var/www/app/public; try_files $uri/index.html $uri @puma_app; location @puma_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://puma_app; # limit_req zone=one; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; } location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } location = /50x.html { root html; } location = /404.html { root html; } location @503 { error_page 405 = /system/maintenance.html; if (-f $document_root/system/maintenance.html) { rewrite ^(.*)$ /system/maintenance.html break; } rewrite ^(.*)$ /503.html break; } if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){ return 405; } if (-f $document_root/system/maintenance.html) { return 503; } location ~ \.(php|html)$ { return 405; } } 

泊坞窗,compose.yml:

 version: '2' services: app: build: . command: bundle exec puma -C config/puma.rb volumes: - 'app:/var/www/app' - 'public:/var/www/app/public' ports: - '3000:3000' depends_on: - postgres env_file: - '.env' postgres: image: postgres:latest environment: POSTGRES_USER: 'postgres_user' ports: - '5432:5432' volumes: - 'postgres:/var/lib/postgresql/data' volumes: postgres: app: public: 

Dockerfile

 # Base image: FROM ruby:2.4 # Install dependencies RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs # Set an environment variable where the Rails app is installed to inside of Docker image: ENV RAILS_ROOT /var/www/app RUN mkdir -p $RAILS_ROOT ENV RAILS_ENV production ENV RACK_ENV production # Set working directory, where the commands will be ran: WORKDIR $RAILS_ROOT # Gems: COPY Gemfile Gemfile COPY Gemfile.lock Gemfile.lock RUN gem install bundler RUN bundle install COPY config/puma.rb config/puma.rb # Copy the main application. COPY . . RUN bundle exec rake RAILS_ENV=production assets:precompile VOLUME ["$RAILS_ROOT/public"] EXPOSE 3000 # The default command that gets ran will be to start the Puma server. CMD bundle exec puma -C config/puma.rb 

    我想你正尝试从/var/www/app/public的容器内的主机访问/var/www/app/public

    您需要在容器中装载主机目录。 运行容器时,可以使用-v "/public:/var/www/app/public"