Dockernized Nginx + (Rails+Unicorn)+Redis+PostgreSQL:connect() failed (111: Connection refused)

I am trying to run 4containers(Nginx + (Rails+Unicorn)+Redis+PostgreSQL) with docker-compose,but it does not work.I checked my Dockerfile and nginx.conf couldnt solve the Nginx connection refuse problem(404 not found on browser).

[error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client:, server: localhost, request: "GET / HTTP/1.1", upstream: "", host: ""

My Dockerfile

  • Docker development workflow with node.js
  • Google Container Engine: Kubernetes is not exposing external IP after creating container
  • Externalize to docker env
  • Does “docker-compose down” invoke “docker stop” on each container?
  • How to build docker image frome .drone.yml?
  • remote control nginx configuration
  • 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"]

    My docker-compose.yml

      image: postgres
        - '5432:5432'
        - datastore
      image: redis:2.8.19
        - '6379:6379'
        - datastore
      build: containers/nginx
        - '8080:80'
        - datastore
        - rails
      build: containers/datastore
      build: .
        - '3000:3000'
        DATABASE_URL: postgres://postgres@db:5432
        REDIS_URL: redis://redis:6379
        #SECRET_KEY_BASE: hogehoge
        - datastore
        - db
        - redis


    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/;
    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/javascript application/x-javascript
      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 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;


    FROM nginx:1.9.6
    COPY nginx.conf /etc/nginx/nginx.conf

    Thanks for any help in this problem.

  • Gitlab CI: How to run tests in pipeline using docker and shell runner
  • Cannot execute ansible playbook via docker container
  • Communication between two Docker containers on macOS 10.12
  • Environment variables in docker when exec docker run
  • How to install docker specific version
  • Communication between multiple docker-compose projects
  • One Solution collect form web for “Dockernized Nginx + (Rails+Unicorn)+Redis+PostgreSQL:connect() failed (111: Connection refused)”

    I think the problem is the fail_timeout=0 in your configuration. When the rails container starts it takes a few milliseconds to startup (possibly longer). So nginx needs to retry the connection for some period. I believe the configuration you have sets a timeout of 0. If you configure it to retry for a few seconds I think it may work.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.