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

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

新規登録して質問してみよう
ただいま回答率
85.50%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby on Rails 5

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

Ruby

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

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Docker

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

Q&A

解決済

1回答

4530閲覧

dockerで作成したnginxコンテナとrailsコンテナの接続がうまくいかない

Daimian

総合スコア53

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby on Rails 5

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

Ruby

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

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Docker

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

0グッド

1クリップ

投稿2019/03/04 06:23

編集2019/03/04 12:40

前提・実現したいこと

docker-compose + nginx + Rails + mysqlという環境でアプリを作成し、起動させたいです。

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

下記の「該当のソースコード」に記載の状態でdocker-compose upをし、localhostにアクセスしますが、nginxがrailsコンテナを見に行っている気配がありません。

localhost:3000だと問題なくrailsの「Yay! You’re on Rails!」というオープニング画面が表示されますが、localhostに接続するとWelcome to nginx!と、nginxのオープニング画面が出て来ます。

また、設定しているroutingで/api/v1/lightning/2018というのがあり、これを
__http://localhost:3000/api/v1/lightning/2018__ で実施すると問題なくdbからデータを取って来ますが、
__http://localhost/api/v1/lightning/2018__ で実施すると「404 Not Found nginx/1.15.9」というメッセージが返って来てしまいます。
その時は

2019/03/04 12:37:28 [error] 6#6: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.28.0.1, server: localhost, request: "GET /api/v1/lightning/2018 HTTP/1.1", upstream: "http://127.0.0.1:3000/api/v1/lightning/2018", host: "localhost" 2019/03/04 12:37:28 [error] 6#6: *2 open() "/app/kaminari_API/kaminari_API_rails/public/500.html" failed (2: No such file or directory), client: 172.28.0.1, server: localhost, request: "GET /api/v1/lightning/2018 HTTP/1.1", upstream: "http://127.0.0.1:3000/api/v1/lightning/2018", host: "localhost"

と表示されます。

該当のソースコード

docker_compose.yml

version: '3' services: nginx: image: nginx:latest container_name: kaminari_nginx ports: - "80:80" links: - web volumes: - ./kaminari_nginx/nginx.conf:/etc/nginx/nginx.conf web: build: context: ./ dockerfile: Dockerfile_API tty: true stdin_open: true container_name: kaminari_API environment: RAILS_ENV: development volumes: - ./kaminari_API/kaminari_API_rails:/app/kaminari_API/kaminari_API_rails:rw - ./start_api.sh:/usr/local/bin/start_api.sh:ro ports: - 3000:3000 working_dir: /app/kaminari_API/kaminari_API_rails command: bash /usr/local/bin/start_api.sh db: image: mariadb:latest container_name: kaminari_db ports: - 3307:3306 environment: MYSQL_DATABASE: kaminari_API_rails_development

./kaminari_nginx/nginx.conf

user nginx; worker_processes auto; error_log /var/log/nginx/error_test.log debug; 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_test.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; root /app/kaminari_API/kaminari_API_rails/public; location / { proxy_pass http://app_server; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } error_page 404 /404.html; error_page 500 502 503 504 /500.html; location = /500.html { root /app/kaminari_API/kaminari_API_rails/public; } } upstream app_server { server 127.0.0.1:3000; } }

./kaminari_API/kaminari_API_rails/config/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. # threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } threads threads_count, threads_count # 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 number of `workers` to boot in clustered mode. # Workers are forked webserver 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

試していること

nginxがrailsのpumaとうまく接続できていないのではないかと思い、調べてはいるものの、迷走しています。
どんなことでもいいので、何か回答をいただきたく。。。。

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

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

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

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

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

t_obara

2019/03/04 07:29

リバースプロキシの設定(nginx.conf)は何を参考にされましたか?その点をご提示ください。
Daimian

2019/03/04 08:59

さらに申し上げると、ログを確認しようと思ってnginxのコンテナに入っても、ログが出力されて来ません。。accessログも、errorログも・・
guest

回答1

0

ベストアンサー

以下の点をご確認ください。参照したページとも違っています。

nginx.conf


proxy_pass http://web;


proxy_pass http://app_server;

投稿2019/03/04 09:01

t_obara

総合スコア5488

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

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

Daimian

2019/03/04 09:07

ご回答ありがとうございます!修正しましたが、状況変わらずでした。。
t_obara

2019/03/04 09:26

今回の件を踏まえ、再度参考にした内容を見直してみてはいかがですか? また、修正後に再起動して確認しているのですよね?
Daimian

2019/03/04 09:33

ありがとうございます、修正後に再起動して確認しています!内容ももう一度確認しています!
Daimian

2019/03/04 14:28 編集

色々とご対応ありがとうございました! proxy_pass http://app_server; upstream app_server { server web:3000; } この2つの記述の追加・変更でうまく行きました。 dockerコンテナ内で127.0.0.1のURIで探しても、コンテナ内しか探してもらえないみたいです。 dockerネットワーク内であればhost名で名前解決ができるため、web:3000の記述が有効みたいです。 【参考】 https://serverfault.com/questions/895789/111-connection-refused-nginx-proxy-for-docker-containers 解決に丸1日かかりましたがなんとか、お陰様で出来ました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問