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

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

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

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

docker-compose

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

nginx

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

Ruby on Rails

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

935閲覧

.unicorn.sock failed (111: Connection refused)

ryouya

総合スコア14

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

docker-compose

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

nginx

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

Ruby on Rails

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

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2020/06/03 09:21

お世話になっております。
下記の問題に知見のある方がいらっしゃいましたらご教示お願いします。

#起きている問題
AWS環境でnginxrailsmysqldockerコンテナで構築し、
docker-compose up実行時、railsコンテナだけが起動しませんでした。

$ docker-compose up Starting coffee_app_db_1 ... done Starting coffee_app_nginx_1 ... done Starting coffee_app_app_1 ... done Attaching to coffee_app_db_1, coffee_app_nginx_1, coffee_app_app_1 db_1 | 2020-06-03T00:56:22.195265Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161) db_1 | 2020-06-03T00:56:22.195272Z 0 [Warning] [MY-010142] : : db_1 | 2020-06-03T00:56:23.769691Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060 coffee_app_app_1 exited with code 0

ALBのDNS名でアクセス時のnginx.logを確認したところ、.unicorn.sock failed (111: Connection refused) のエラーが発生していました。

$ tail nginx.error.log 2020/06/03 00:46:06 [error] 4077#0: *606 open() "/usr/share/nginx/html/latest/dynamic/instance-identity/document" failed (2: No such file or directory), client: 10.0.0.201, server: _, request: "GET /latest/dynamic/instance-identity/document HTTP/1.1", host: "169.254.169.254" : : 2020/06/03 01:01:06 [error] 4077#0: *789 connect() to unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 10.0.0.201, server: coffeeapp-elb-16348812.ap-northeast-1.elb.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock:/", host: "coffeeapp-elb-16348812.ap-northeast-1.elb.amazonaws.com" 2020/06/03 01:24:26 [error] 4077#0: *1071 connect() to unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 10.0.0.201, server: coffeeapp-elb-16348812.ap-northeast-1.elb.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock:/", host: "coffeeapp-elb-16348812.ap-northeast-1.elb.amazonaws.com"

#確認したこと
パーミッションの確認をしましたが、問題ございませんでした。

$ ls -la drwxrwxrwx 2 root root 27 6月 3 00:56 sockets $ ls -la srwxrwxrwx 1 root root 0 6月 3 00:56 .unicorn.sock

#関連ファイル
coffee_app(アプリ名)/docker/nginx/Dockerfile(nginx)

FROM nginx:1.12.2 RUN apt-get update && \ apt-get install -y apt-utils \ locales && \ echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \ locale-gen ja_JP.UTF-8 ENV LC_ALL ja_JP.UTF-8 ADD ./docker/nginx/nginx.conf /etc/nginx/nginx.conf ADD ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf

coffee_app/docker/nginx/Dockerfile(rails)

FROM ruby:2.5.1 RUN apt-get update -qq && \ apt-get install -y apt-utils \ build-essential \ libpq-dev \ nodejs \ default-mysql-client \ yarn RUN mkdir /coffee_app WORKDIR /coffee_app ADD Gemfile /coffee_app/Gemfile ADD Gemfile.lock /coffee_app/Gemfile.lock RUN bundle install -j4 ADD . /coffee_app EXPOSE 3000

coffee_app/docker/nginx/default.conf(nginx設定ファイル)

upstream unicorn { server unix:/var/tmp/unicorn.sock; } server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://unicorn; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }

coffee_app/docker/nginx/nginx.conf(nginx設定ファイル)

user nginx; worker_processes 1; 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; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }

etc/nginx/conf.d/coffee_app.conf(nginx設定ファイル)

error_log /var/www/rails/coffee_app/log/nginx.error.log; access_log /var/www/rails/coffee_app/log/nginx.access.log; upstream unicorn_server { server unix:/var/www/rails/coffee_app/tmp/sockets/.unicorn.sock fail_timeout=0; } server { listen 80; client_max_body_size 4G; server_name 【ELBのDNS名】; # server_name 【アプリのElastic IP】; keepalive_timeout 5; root /var/www/rails/coffee_app/public; location ~ ^/assets/ { root /var/www/rails/coffee_app/public; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://unicorn_server; break; } } error_page 500 502 503 504 /500.html; location = /500.html { root /var/www/rails/coffee_app/public; } location = /favicon.ico { log_not_found off; } }

etc/nginx/nginx.conf(nginx設定ファイル)

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; server_names_hash_bucket_size 128; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

coffee_app/docker/nginx/Dockerfile(mysql)

FROM mysql:8.0.17 RUN apt-get update && \ apt-get install -y apt-utils \ locales && \ rm -rf /var/lib/apt/lists/* && \ echo "ja_JP.UTF-8 UTF-8" > /etc/locale.gen && \ locale-gen ja_JP.UTF-8 ENV LC_ALL ja_JP.UTF-8 ADD ./docker/mysql/charset.cnf /etc/mysql/conf.d/charset.cnf

coffee_app/docker-compose.yml

version: '2' services: app: build: context: . dockerfile: ./docker/rails/Dockerfile command: bundle exec unicorn_rails -c /coffee_app/config/unicorn.conf.rb -D -E production # command: bundle exec rails s -p 3000 -b '0.0.0.0' ports: - '3000:3000' volumes: - /var/tmp - .:/coffee_app depends_on: - db extends: file: ./docker/mysql/password.yml service: password db: build: context: . dockerfile: ./docker/mysql/Dockerfile ports: - '3300:3306' volumes: - db_data:/var/lib/mysql extends: file: ./docker/mysql/password.yml service: password nginx: build: context: . dockerfile: ./docker/nginx/Dockerfile ports: - '88:80' volumes: - app volumes: db_data:

coffee_app/config/unicorn.conf.rb

$worker = 2 $timeout = 30 $app_dir = "/coffee_app" $listen = File.expand_path 'tmp/sockets/.unicorn.sock', $app_dir $pid = File.expand_path 'tmp/pids/unicorn.pid', $app_dir $std_log = File.expand_path 'log/unicorn.log', $app_dir # set config worker_processes $worker working_directory $app_dir stderr_path $std_log stdout_path $std_log timeout $timeout listen $listen pid $pid # loading booster preload_app true # before starting processes before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin Process.kill "QUIT", File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end # after finishing processes after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end

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

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

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

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

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

guest

回答1

0

自己解決

解決しました。

docker_compose.ymlでappコンテナとnginxコンテナの両方からunicorn.sockをみれるようにしたら正常に動くことができました。
確認してくださった皆様、ありがとうございましたm(_ _)m

coffee_app/docker-compose.yml

version: '3' services: app: build: context: . dockerfile: ./docker/rails/Dockerfile command: bundle exec unicorn_rails -c /coffee_app/config/unicorn.conf.rb # command: bundle exec rails s -p 3000 -b '0.0.0.0' ports: - '3000:3000' volumes: - tmp_data:/coffee_app/tmp/sockets - .:/coffee_app depends_on: - db db: build: context: . dockerfile: ./docker/mysql/Dockerfile command: --default-authentication-plugin=mysql_native_password ports: - '3306:3306' volumes: - db_data:/var/lib/mysql nginx: build: context: . dockerfile: ./docker/nginx/Dockerfile ports: - '80:80' volumes: - tmp_data:/var/tmp/ volumes: db_data: tmp_data:

投稿2020/06/05 01:33

ryouya

総合スコア14

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問