快速的新手問題。我有一個想要投入生產的簡單 Rails 應用程式。我想將它部署在 AWS EC2 服務器中。該應用程式在本地埠 3000 上運行良好。
我嘗試運行容器,雖然它說正在偵聽埠 3000,但當我嘗試在瀏覽器上轉到:13.241.213.464:3000 時,它會繼續加載而沒有顯示任何內容。
我在互聯網上讀到您需要更改 Dockerfile 上的 CMD 以使其運行,因此我將其更改為:
CMD ["rails", "server", "-b", "0.0.0.0"]
對此:
CMD ["bundle", "exec", "rails", "server", "-e", "production"]
所以現在我的Dockerfile樣子是這樣的:
# Start from the official ruby image, then update and install JS & DB
FROM ruby:2.6.6
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# Create a directory for the application and use it
RUN mkdir /myapp
WORKDIR /myapp
# Gemfile and lock file need to be present, they'll be overwritten immediately
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
# Install gem dependencies
RUN gem install bundler:2.2.32
RUN bundle install
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 && apt-get install -y yarn && apt-get install -y npm
RUN yarn install
RUN yarn add bootstrap jquery popper.js
COPY . /myapp
# This script runs every time the container is created, necessary for rails
COPY entrypoint.sh /usr/bin/
RUN chmod x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start rails
CMD ["bundle", "exec", "rails", "server", "-e", "production"]
我docker-compose.yml的是:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_HOST: db
而且entrypoint.sh是:
#!/bin/bash
# Rails-specific issue, deletes a pre-existing server, if it exists
set -e
rm -f /myapp/tmp/pids/server.pid
exec "$@"
要運行容器進行生產,我運行docker-compose.override.yml:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
image: "dockerhub_user/repo:${WEB_TAG:-latest}"
ports:
- "3000:3000"
depends_on:
- db
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_HOST: db
我這樣運行它:docker-compose -f docker-compose.override.yml up
它仍然繼續加載而不顯示任何內容。
I also read about some projects that have nginx but I am not sure if it is needed or not to make it work. I kind of understood the nginx help to proxy the requests between users and the Rails app, is it correct?
Then, do I need nginx to make it work in production?
Any kind of enlightment is welcome. Thank you!
uj5u.com熱心網友回復:
所以我最終建立了,nginx但即便如此它也不起作用。然后我意識到這可能是Inbound and outbound rulesAWS EC2 實體的安全性。
所以,現在,我的應用程式正在偵聽埠 80(因為 nginx)。我所做的是創建了幾個Inbound rules:
80 TCP 0.0.0.0/0
80 TCP ::/0
有了這個,一旦我進入public ip of the instance:80.
希望這對將來有需要的人有所幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/436595.html
標籤:ruby-on-rails docker nginx amazon-ec2 dev-to-production
