title: Connect nginx on host with wsgi unicorn inside docker container
body: <p>Starting to dockerize my Rails application I am facing following problem:</p>
  
  <p>My idea was to have every web application with their Wsgi and dependencies running in an extra docker container and the database also ruiing in seperate containers while using docker-compose to set it up.</p>
  
  <p>Outside the containers Nginx is routing traffic then depending on the domain to the specific container via unix sockets.(Didn't want nginx in a container to reduce the complexity and avoid having multiple nginx running in multiple containers to maintain multiple webapps). </p>
  
  <p>Before starting with docker my wsgi and nginx got connected via unix sockets. But after dockerizing this is not working anymore. Only connecting them with ports works now which I would like to avoid.</p>
  
  <p>Is there anyway way to connect Nginx on the host via unix sockets with the WSGI inside a container? If not what is best practice here?</p>
  
  <p>My approach was to use shared volumes as location for the socket file but nginx cant access the socket created by the wsgi unicorn:
  Socket created by unicorn:</p>
  
  <pre><code>srwxrwxrwx 1 root root    0 Nov 14 14:53 unicorn.sock=
  </code></pre>
  
  <p>Nginx error:</p>
  
  <pre><code>*2 connect() to unix:/ruby-webapps/myapp/shared/sockets/unicorn.sock failed (13: Permission denied) while connecting to upstream
  </code></pre>
  
  <p>Nginx sites-avaible/myapp:</p>
  
  <pre><code>upstream myapp {
      # Path to Unicorn SOCK file, as defined previously
      server unix:/ruby-webapps/myapp/shared/sockets/unicorn.sock fail_timeout=0;
  }
  server {
      listen 80 default_server;
  ...
  }
  server {
      listen 443 ssl http2;
      listen [::]:443 ssl http2;
      server_name myapp.de www.myapp.de;
  
      root /ruby-webapps/myapp;
      try_files $uri/index.html $uri @MyApp;
  
      location @MyApp {
          proxy_pass http://myapp;
          #proxy_pass  http://127.0.0.1:3000;
          proxy_set_header X-Forwarded-For https;
          proxy_redirect off;
      }   
  }
  </code></pre>
  
  <p>docker-compose.yml:</p>
  
  <pre><code>version:'2'
   services:
    postgresmyapp: 
      image: postgres
      env_file: .env    
    myapp:
      build: .
      env_file: .env
      command: supervisord -c /myapp/unicorn_supervisord.conf 
      volumes:
        - .:/myapp
      ports:
        - "3000:3000" 
      links:
        - postgreslberg 
  </code></pre>
  
  <p>config/unicorn.rb:                                                    </p>
  
  <pre><code>app_dir = File.expand_path("../..", __FILE__)
  shared_dir = "#{app_dir}/shared"
  working_directory app_dir
  
  rails_env = ENV['RAILS_ENV'] || 'production'
  
  # Set unicorn options
  worker_processes 2
  preload_app true
  timeout 30
  
  # Set up socket location
  listen "#{shared_dir}/sockets/unicorn.sock", :backlog =&gt; 64
  #listen(3000, backlog: 64) 
  
  stderr_path "#{shared_dir}/log/unicorn.stderr.log"
  stdout_path "#{shared_dir}/log/unicorn.stdout.log"
  pid "#{shared_dir}/pids/unicorn.pid"
  </code></pre>