前提
ALB→nginx→puma→RailsAPIのような疎通で、Fargateにデプロイしたいのですが
connect() to unix:///back/tmp/sockets/puma.sock failed (2: No such file or directory)
が出ており、ヘルスチェックが通らずタスクが停止してしまう。
発生している問題・エラーメッセージ
connect() to unix:///back/tmp/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.0.33, server: localhost, request: "GET /api/v1/health_check HTTP/1.1", upstream: "http://unix:///back/tmp/sockets/puma.sock:/api/v1/health_check", host: "10.0.2.117" "GET /api/v1/health_check HTTP/1.1" 502 157 "-" "ELB-HealthChecker/2.0"
該当のソースコード
puma.rb
rails
1threads_count = ENV.fetch('RAILS_MAX_THREADS', 5).to_i 2threads threads_count, threads_count 3port ENV.fetch('PORT', 5000) 4environment ENV.fetch('RAILS_ENV', 'development') 5plugin :tmp_restart 6 7app_root = File.expand_path('..', __dir__) 8bind "unix://#{app_root}/tmp/sockets/puma.sock"
default.conf
upstream app { server unix:///back/tmp/sockets/puma.sock; } server { listen 80 default; server_name localhost; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /back/app/public; location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header Host $http_host; proxy_pass http://app; } client_max_body_size 100m; # error_page 404 505 502 503 504 /layouts/error.vue; try_files $uri/index.html $uri @app; keepalive_timeout 5; }
nginx.conf
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 600; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain text/xml text/css text/javascript image/svg+xml application/javascript application/x-javascript application/json application/xml application/xml+rss application/font-tff application/octet-stream; include /etc/nginx/conf.d/*.conf; }
nginx Dockerfile
FROM nginx:1.16 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 ./nginx/nginx.conf /etc/nginx/nginx.conf ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
rails Dockerfile
FROM ruby:2.6.6 RUN curl https://deb.nodesource.com/setup_12.x | bash RUN curl 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 apt-utils \ build-essential \ libpq-dev \ nodejs \ yarn \ vim \ default-mysql-client RUN gem install bundler:2.0.2 ENV TZ Asia/Tokyo ENV LANG C.UTF-8 WORKDIR /back ADD /back/Gemfile . ADD /back/Gemfile.lock . RUN bundle install RUN mkdir -p /back/tmp/pids RUN mkdir -p /back/tmp/sockets RUN yarn install --check-files # RUN WEBPACKER_PRECOMPILE=false bundle exec rails assets:precompile VOLUME /back/tmp/sockets COPY /back .
{ "ipcMode": null, "executionRoleArn": ":role/career-finder-ecsTaskExecutionRole", "containerDefinitions": [ { "dnsSearchDomains": null, "environmentFiles": null, "logConfiguration": { "logDriver": "awslogs", "secretOptions": null, "options": { "awslogs-group": "/ecs/staging-agt-careerfinder-api", "awslogs-region": "ap-northeast-1", "awslogs-stream-prefix": "ecs" } }, "entryPoint": null, "portMappings": [ { "hostPort": 5000, "protocol": "tcp", "containerPort": 5000 } ], "command": [ "bundle", "exec", "puma", "-C", "config/puma.rb" ], "linuxParameters": null, "cpu": 0, "environment": [], "resourceRequirements": null, "ulimits": null, "dnsServers": null, "mountPoints": [], "workingDirectory": null, "secrets": null, "dockerSecurityOptions": null, "memory": null, "memoryReservation": null, "volumesFrom": [], "stopTimeout": null, "image": "967906006845.dkr.ecr.ap-northeast-1.amazonaws.com/staging-agt-careerfinde-rails", "startTimeout": null, "firelensConfiguration": null, "dependsOn": null, "disableNetworking": null, "interactive": null, "healthCheck": { "retries": 3, "command": [ "CMD-SHELL", "curl --unix-socket /back/tmp/sockets/puma.sock ./", "|| exit 1" ], "timeout": 5, "interval": 30, "startPeriod": null }, "essential": true, "links": null, "hostname": null, "extraHosts": null, "pseudoTerminal": null, "user": null, "readonlyRootFilesystem": null, "dockerLabels": null, "systemControls": null, "privileged": null, "name": "rails" }, { "dnsSearchDomains": null, "environmentFiles": null, "logConfiguration": { "logDriver": "awslogs", "secretOptions": null, "options": { "awslogs-group": "/ecs/staging-agt-careerfinder-api", "awslogs-region": "ap-northeast-1", "awslogs-stream-prefix": "ecs" } }, "entryPoint": null, "portMappings": [ { "hostPort": 80, "protocol": "tcp", "containerPort": 80 } ], "command": null, "linuxParameters": null, "cpu": 0, "environment": [], "resourceRequirements": null, "ulimits": null, "dnsServers": null, "mountPoints": [ { "readOnly": null, "containerPath": "/back/tmp", "sourceVolume": "sockets" } ], "workingDirectory": null, "secrets": null, "dockerSecurityOptions": null, "memory": null, "memoryReservation": null, "volumesFrom": [], "stopTimeout": null, "image": "967906006845.dkr.ecr.ap-northeast-1.amazonaws.com/staging-agt-careerfinde-nginx", "startTimeout": null, "firelensConfiguration": null, "dependsOn": [ { "containerName": "rails", "condition": "HEALTHY" } ], "disableNetworking": null, "interactive": null, "healthCheck": null, "essential": true, "links": null, "hostname": null, "extraHosts": null, "pseudoTerminal": null, "user": null, "readonlyRootFilesystem": false, "dockerLabels": null, "systemControls": null, "privileged": null, "name": "nginx" } ], "placementConstraints": [], "memory": "1024", "taskRoleArn": "arn:aws:iam::967906006845:role/career-finder-ecsTaskExecutionRole", "compatibilities": [ "EC2", "FARGATE" ], "taskDefinitionArn": "arn:aws:ecs:ap-northeast-1:967906006845:task-definition/staging-agt-careerfinder-api:56", "family": "staging-agt-careerfinder-api", "requiresAttributes": [ { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" }, { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.docker-remote-api.1.24" }, { "targetId": null, "targetType": null, "value": null, "name": "ecs.capability.execution-role-awslogs" }, { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.ecr-auth" }, { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" }, { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.task-iam-role" }, { "targetId": null, "targetType": null, "value": null, "name": "ecs.capability.container-health-check" }, { "targetId": null, "targetType": null, "value": null, "name": "ecs.capability.container-ordering" }, { "targetId": null, "targetType": null, "value": null, "name": "ecs.capability.execution-role-ecr-pull" }, { "targetId": null, "targetType": null, "value": null, "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "targetId": null, "targetType": null, "value": null, "name": "ecs.capability.task-eni" } ], "pidMode": null, "requiresCompatibilities": [ "FARGATE" ], "networkMode": "awsvpc", "runtimePlatform": { "operatingSystemFamily": "LINUX", "cpuArchitecture": null }, "cpu": "512", "revision": 56, "status": "ACTIVE", "inferenceAccelerators": null, "proxyConfiguration": null, "volumes": [ { "fsxWindowsFileServerVolumeConfiguration": null, "efsVolumeConfiguration": null, "name": "sockets", "host": { "sourcePath": null }, "dockerVolumeConfiguration": null } ] }
試したこと
- railsDockerdileにVOLUME /back/tmpを追加して、タスク定義でnginxのコンテナのソースコンテナにrailsを指定→500エラーに変わりヘルスチェック通らず
- VOLUME [“/back/tmp”]も試したが同様でした。
- default.confのupstreamをserver localhost:5000;に変更→“GET /api/v1/health_check HTTP/1.1" 500 71587 “-” “ELB-HealthChecker/2.0"
パブリックIPアドレスやロードバランサーDNS名でブラウザにアクセスしようとしましたがアクセスできませんでした。
こちらの記事を参考にしましたが、
-ここから追加で試したこと-
- タスク定義でボリュームの追加→名前:sockets ボリュームタイプ:Bind Mount
- nginxコンテナでマウントポイントの追加→ソースボリューム:sockets コンテナパス:/back/tmp/sockets
- RailsDockerfileにVOLUME追加→VOLUME /back/tmp/sockets
- docker-compose.ymlにvolumesを追加→backのvolumesに - sockets:/back/tmp/sockets volumesにsockets
nginxコンテナからRailsコンテナのpublic配下は確認できるが、tmp配下はファイル群が存在していない。
Railsコンテナ
Starting session with SessionId: ecs-execute-command-060aa63f924fee35b # cd tmp # cd sockets # ls -a . .. puma.sock # pwd /back/tmp/sockets
nginxコンテナ
Starting session with SessionId: ecs-execute-command-05c9c71aac4d64b8c # cd back # ls -a . .. public tmp # cd public # ls -a . .. favicon.ico robots.txt uploads # cd .. # cd tmp # ls -a . ..
補足情報(FW/ツールのバージョンなど)
ruby 2.6.6
rails 6.0.3
AWS (VPC/ECS Fargate/ECR/RDS/S3/ALB/Route53)
