質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

0回答

1649閲覧

ECS上でデプロイしたRailsアプリケーションを表示させたい

yuu0000

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

1クリップ

投稿2021/07/28 11:05

編集2021/07/29 14:49

前提・実現したいこと

現在Docker環境で作成したアプリケーションをECS(fargate)を用いてデプロイしております。(本番環境でのデプロイまで実装済み)

デプロイ後にアプリケーションをブラウザで確認したところ,本番環境で立ち上がることは確認できたのですが、RailsアプリケーションではなくWelcome to nginx!が表示されてしまいます。

おそらくローカル環境で、nginxがデフォルトで表示される設定になっているのではないかと疑っております。

パブリックIPアドレスDNS独自ドメインでそれぞれアクセスしてみましたが、すべてWelcome to nginx!が表示されました。

発生している問題・エラーメッセージ

エラーなどは特に起きておらず、Welcome to nginx!がブラウザで表示される。

独自ドメインでのアクセス

ブラウザ画面のスクリーンショット

Railsは正常に立ち上がっており、原因はnginxにあると考えております。

ECS/サービスのタスク上のログ

  • Railsコンテナ
2021-07-28 18:05:43Use Ctrl-C to stop 2021-07-28 18:05:43* PID: 1 2021-07-28 18:05:43* Listening on http://0.0.0.0:3000 2021-07-28 18:05:43Puma starting in single mode... 2021-07-28 18:05:43* Puma version: 5.2.2 (ruby 3.0.1-p64) ("Fettisdagsbulle") 2021-07-28 18:05:43* Min threads: 5 2021-07-28 18:05:43* Max threads: 5 2021-07-28 18:05:43* Environment: production 2021-07-28 18:05:39=> Booting Puma 2021-07-28 18:05:39=> Rails 6.1.3.1 application starting in production 2021-07-28 18:05:39=> Run `bin/rails server --help` for more startup options 2021-07-28 18:05:20Database 'locat' already exists

Listening on http://0.0.0.0:3000にアクセスするとRailsアプリケーションが立ち上がるのですが、こちらはローカルからの立ち上げになりますでしょうか?
(理由は立ち上げのログを見ると、ローカル上のDockerログが確認できるため)

  • nginxコンテナ
2021-07-28 18:05:05/docker-entrypoint.sh: Configuration complete; ready for start up 2021-07-28 18:05:0510-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packaged version 2021-07-28 18:05:05/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh 2021-07-28 18:05:0510-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf 2021-07-28 18:05:05/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 2021-07-28 18:05:05/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration 2021-07-28 18:05:05/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

本番環境でエラーなどは起きていない

本番環境上のエラーから起きているのではなく、ローカルで設定しているnginxファイルを修正する必要があるとわかりました。

ECS周りの設定などを調べてみましたが、設定などは正しく入力されていたのでDocker関係の設定に原因があると考えています。

該当のソースコード

Dockerfile (Rails)

FROM ruby:3.0.1 ENV RAILS_ENV=production RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && 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 build-essential libpq-dev nodejs yarn WORKDIR /locat COPY Gemfile /locat/Gemfile COPY Gemfile.lock /locat/Gemfile.lock RUN gem install bundler RUN bundle install COPY . /locat COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 VOLUME /locat/public VOLUME /locat/tmp CMD ["rails", "server", "-b", "0.0.0.0"]

containers/nginx/Dockerfile

FROM nginx:1.19.1 RUN apt-get update && \ apt-get install -y apt-utils \ locales && \ sed -i -e 's/# ja_JP.UTF-8/ja_JP.UTF-8/g' /etc/locale.gen && \ locale-gen ja_JP.UTF-8 ENV LANG ja_JP.UTF-8 ENV LC_TIME C COPY ./nginx.conf /etc/nginx/nginx.conf COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf

containers/nginx/nginx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 600; include /etc/nginx/conf.d/*.conf; }

containers/nginx/conf.d/default.conf

upstream unicorn { server 127.0.0.1:3000; } server { listen 80 default_server; server_name localhost; root /usr/share/nginx/html; try_files $uri/index.html $uri @unicorn; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; client_max_body_size 100m; error_page 404 /404.html; error_page 505 502 503 504 /500.html; location @unicorn { 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://unicorn; } }

docker-compose.yml

version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: root ports: - "4306:3306" volumes: - ./mysql-confd:/etc/mysql/conf.d web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/locat ports: - 3000:3000 depends_on: - db tty: true stdin_open: true volumes: mysql-data:

試したこと

  • 原因がdefault.confにあると考えており、今回unicornではなくpumaで動いていることに加えて、server_nameをローカルホストから本番環境のドメインに変更する必要があるのではと仮説を立てました。
upstream puma { server unix:////tmp/sockets/puma.sock;; } pumaに変更する必要があるのでは? server { listen 80 default_server; server_name localhost;        localhostの部分をタスク内のパブリックIPアドレス、独自ドメインに指定する必要があるのでは? root /usr/share/nginx/html;    root /locat/public;    こちらの記述にルートを変更する必要があるのでは? try_files $uri/index.html $uri @unicorn; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; client_max_body_size 100m; error_page 404 /404.html; error_page 505 502 503 504 /500.html; location @unicorn { 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://unicorn; }         unicornの部分をpumaに修正する必要があるのでは? }
修正したファイル

nginx/Dockerfile

FROM nginx:1.19.1 RUN apt-get update && \ apt-get install -y apt-utils \ locales && \ sed -i -e 's/# ja_JP.UTF-8/ja_JP.UTF-8/g' /etc/locale.gen && \ locale-gen ja_JP.UTF-8 ENV LANG ja_JP.UTF-8 ENV LC_TIME C ADD ./nginx/nginx.conf /etc/nginx/nginx.conf ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf

nginx/default.conf

upstream locat { server unix:///locat/tmp/sockets/puma.sock; } server { listen 80 default_server; server_name locat-app.com; root /locat/public; try_files $uri/index.html $uri @locat; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; client_max_body_size 100m; error_page 404 /404.html; error_page 505 502 503 504 /500.html; location / { 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://locat; } }

puma.rb

# Puma can serve each request in a thread from an internal thread pool. # The `threads` method setting takes two numbers: a minimum and maximum. # Any libraries that use thread pools should be configured to match # the maximum value specified for Puma. Default is set to 5 threads for minimum # and maximum; this matches the default thread size of Active Record. # max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } threads min_threads_count, max_threads_count # Specifies the `worker_timeout` threshold that Puma will use to wait before # terminating a worker in development environments. # worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" # Specifies the `port` that Puma will listen on to receive requests; default is 3000. # port ENV.fetch("PORT") { 3000 } # Specifies the `environment` that Puma will run in. # environment ENV.fetch("RAILS_ENV") { "development" } # Specifies the `pidfile` that Puma will use. pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } # Specifies the number of `workers` to boot in clustered mode. # Workers are forked web server processes. If using threads and workers together # the concurrency of the application would be max `threads` * `workers`. # Workers do not work on JRuby or Windows (both of which do not support # processes). # # workers ENV.fetch("WEB_CONCURRENCY") { 2 } # Use the `preload_app!` method when specifying a `workers` number. # This directive tells Puma to first boot the application and load code # before forking the application. This takes advantage of Copy On Write # process behavior so workers use less memory. # # preload_app! # Allow puma to be restarted by `rails restart` command. plugin :tmp_restart app_root = File.expand_path('..', __dir__) bind "unix://#{app_root}/tmp/sockets/puma.sock" stdout_redirect "#{app_root}/log/puma.stdout.log", "#{app_root}/log/puma.stderr.log", true
  • nginxファイル、pumaの設定などに変更を加えてみましたが、変わらずWelcome to nginx!が表示されました。
  • タスクは Railsコンテナ(ローカルでRails用に設定しているDockerfile)とnginxコンテナ(ローカルでnginx用に作成したDockerfile)を独立して作成しております。

補足情報(FW/ツールのバージョンなど)

開発環境

  • rubymine
  • ruby(3.0.1)
  • Ruby on rails (6.1.3.1)
  • Docker (20.10.7)
  • AWS(ECS Fargate/ECR/VPC/ACM/ALB/S3/RDS/Route53)
  • Github

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問