実現したいこと
ホスト上のRailsとコンテナ上のNginxをUNIX DOMAIN SOCKETで連携させたい
現状
Docker(docker-compose)でRailsコンテナとNginxコンテナを作成し、コンテナ間のsocket通信することは出来たのですが
Railsをコンテナではなく、ホスト(Mac)側に環境を作った際に、Nginxでconnect() to unix:///var/www/myapp/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://unix:///var/www/myapp/tmp/sockets/puma.sock:/", host: "localhost"
のエラーが発生し通信できません。
そもそもホストとコンテナ間でsocket通信は可能なのでしょうか?
追記
- localhost:3000で通信できましたが、localhostだと上記のエラーになります。
- Macにbrewでnginxをインストールし、同じconf設定にするとlocalhostでWebページが表示されることを確認しました。
Nginxの設定は以下のとおりです。
# docker-compose.yml version: '2' services: postgres: image: postgres ports: - 5432:5432 environment: POSTGRES_USER: devuser POSTGRES_PASSWORD: password POSTGRES_DB: dev volumes: - ./containers/postgresql/init.sh:/docker-entrypoint-initdb.d/init.sh - ./data/pg-data:/var/lib/postgresql/data nginx: build: ./containers/nginx ports: - 80:80 volumes: - ./public:/var/www/myapp/public - ./tmp:/var/www/myapp/tmp - ./log:/var/www/myapp/log environment: TZ: Asia/Tokyo depends_on: - postgres
Dockerfile
1FROM nginx 2 3RUN apt-get update -qq && apt-get -y install apache2-utils 4ENV RAILS_ROOT /var/www/myapp 5WORKDIR $RAILS_ROOT 6 7ADD default.conf /tmp/default.nginx 8ADD nginx.conf /tmp/nginx.nginx 9 10RUN envsubst '$RAILS_ROOT' < /tmp/default.nginx > /etc/nginx/conf.d/default.conf 11RUN envsubst '$RAILS_ROOT' < /tmp/nginx.nginx > /etc/nginx/nginx.conf 12 13EXPOSE 80 14CMD ["nginx", "-g", "daemon off;"] 15
# 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; tcp_nopush on; keepalive_timeout 75; gzip on; include /etc/nginx/conf.d/*.conf; }
# /etc/nginx/conf.d/default.conf upstream puma { server unix://$RAILS_ROOT/tmp/sockets/puma.sock; } server { listen 80; server_name localhost; root $RAILS_ROOT/public; index index.html; access_log $RAILS_ROOT/log/nginx.access.log; error_log $RAILS_ROOT/log/nginx.error.log; # deny requests for files that should never be accessed location ~ /. { deny all; } location ~* ^.+.(rb|log)$ { deny all; } # serve static (compiled) assets directly if they exist (for rails production) location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ { try_files $uri @rails; access_log off; gzip_static on; # to serve pre-gzipped version expires max; add_header Cache-Control public; # Some browsers still send conditional-GET requests if there's a # Last-Modified header or an ETag header even if they haven't # reached the expiry date sent in the Expires header. add_header Last-Modified ""; add_header ETag ""; break; } location / { try_files $uri @rails; } location @rails { proxy_pass http://puma; 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 $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } error_page 404 /404.html; error_page 500 502 503 504 /500.html; location = /500.html { root $RAILS_ROOT/public; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/11 09:29
2019/06/11 11:39
2019/06/11 15:31 編集
2019/06/12 01:46
2019/06/12 09:32 編集
2019/06/12 10:17