我正在尝试使用docker-compose运行4个容器(Nginx +(Rails + Unicorn)+ Redis + PostgreSQL),但它不起作用。我检查了我的Dockerfile并且nginx.conf无法解决Nginx连接拒绝问题(404在浏览器上找不到。)
[error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.99.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://172.17.0.95:3000/", host: "192.168.99.100:8080"
我的Dockerfile
FROM ruby:2.1.5
RUN apt-get update && apt-get install -y nodejs mysql-client --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY Gemfile /usr/src/app/
COPY Gemfile.lock /usr/src/app/
COPY config/environments/development.rb /usr/src/app/
COPY config/environments/production.rb /usr/src/app/
COPY config/environments/test.rb /usr/src/app/
RUN bundle install
ADD . /usr/src/app
EXPOSE 3000
CMD ["bundle", "exec", "unicorn", "-c", "config/unicorn.rb"]
我的docker-compose.yml
db:
image: postgres
ports:
- '5432:5432'
volumes_from:
- datastore
redis:
image: redis:2.8.19
ports:
- '6379:6379'
volumes_from:
- datastore
nginx:
build: containers/nginx
ports:
- '8080:80'
volumes_from:
- datastore
links:
- rails
datastore:
build: containers/datastore
rails:
build: .
ports:
- '3000:3000'
environment:
DATABASE_URL: postgres://postgres@db:5432
REDIS_URL: redis://redis:6379
#SECRET_KEY_BASE: hogehoge
volumes_from:
- datastore
links:
- db
- redis
containers/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
}
http {
include 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; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream app_server {
# for UNIX domain socket setups:
# server unix:/path/to/.unicorn.sock fail_timeout=0;
# for TCP setups, point these to your backend servers
# server 192.168.0.7:8080 fail_timeout=0;
server rails:3000 fail_timeout=0;
}
server {
listen 80;
server_name localhost;
client_max_body_size 4G;
keepalive_timeout 5;
# path for static files
root /usr/src/app/public;
try_files $uri/index.html $uri.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /usr/src/app/public;
}
}
}
containers/nginx/Dockerfile
FROM nginx:1.9.6
COPY nginx.conf /etc/nginx/nginx.conf
感谢您对此问题的任何帮助。
答案 0 :(得分:1)
我认为问题在于配置中的fail_timeout=0
。当rails容器启动时,启动需要几毫秒(可能更长)。所以nginx需要在一段时间内重试连接。我相信您的配置设置超时为0.如果您将其配置为重试几秒钟,我认为它可能会有效。