AWS EC2 实例未显示 Rails 应用生产

AWS EC2 instance not showing Rails app production

快速的新手问题。我有一个简单的 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

它仍然在加载,没有显示任何内容。

我也读到一些项目有 nginx 但我不确定是否需要它来让它工作。我有点理解 nginx 有助于代理用户和 Rails 应用程序之间的请求,是否正确? 那么,我是否需要 nginx 才能使其在生产环境中运行?

欢迎各种启发。谢谢!

所以我最终构建了 nginx,但即便如此它也无法正常工作。然后我意识到它可能是AWS EC2实例的安全性中的Inbound and outbound rules

所以,现在,我的应用程序正在侦听端口 80(因为 nginx)。我所做的是创建几个 Inbound rules:

80  TCP 0.0.0.0/0
80  TCP ::/0

有了这个,一旦我进入 public ip of the instance:80,它就会显示该应用程序。

希望这对以后有需要的人有所帮助