お世話になっております。
下記の問題に知見のある方がいらっしゃいましたらご教示お願いします。
#起きている問題
AWS
環境でnginx
、rails
、mysql
をdocker
コンテナで構築し、
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。