title: Nginx reverse proxy to an app in host
body: <p>I have an app that is running outside Docker on port 5000. I am trying to run a reverse proxy in nginx via Docker compose but am unable to communicate with the host's port 5000. In my docker-compose.yml file I have:</p>
  
  <pre><code>ports:
    - 80:80
    - 443:443
    - 5000:5000
  </code></pre>
  
  <p>When I try to run this I get:</p>
  
  <pre><code>ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (374026a0d34c8b6b789dcd82d6aee6c4684b3201258cfbd3fb18623c4101): Error starting userland proxy: listen tcp 0.0.0.0:5000: bind: address already in use
  </code></pre>
  
  <p>If I comment out <code>- 5000:5000</code> I get:</p>
  
  <pre><code>[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
  </code></pre>
  
  <p>How do I connect to an already running app in the Host from a Docker nginx container?</p>
  
  <p>EDIT:</p>
  
  <p>My nginx.conf file</p>
  
  <pre><code>user www-data;
  worker_processes auto;
  pid /run/nginx.pid;
  
  events {
      worker_connections 768;
  }
  
  http {
      upstream mysite {
          server 0.0.0.0:5000;
      }
  
      server {
          listen 80;
          server_name localhost;
  
          location / {
          proxy_pass http://mysite;
          }
      }
  }
  </code></pre>
  
  <p>The response when I try to curl localhost is <code>502 Bad Gateway</code>. The app itself and curl 127.0.0.1:5000 responds fine from the host.</p>
  
  <p>EDIT 2:
  I have also tried the <a href="https://stackoverflow.com/questions/41363529/curl-7-failed-to-connect-to-localhost-port-8090-connection-refused">solution found here</a> but I get <code>nginx: [emerg] host not found in upstream "docker"</code>. Docker is my host's hostname.</p>
  
  <p>EDIT 3:
  My docker-compose.yml</p>
  
  <pre><code>version: '3'
  services:
    simple:
      build: ./simple
      container_name: simple
      ports:
        - 80:80
        - 443:443
  </code></pre>
  
  <p>My Dockerfile:</p>
  
  <pre><code>FROM nginx
  COPY nginx.conf /etc/nginx/nginx.conf
  
  EXPOSE 80 443
  CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I am getting the computer host via the "hostname" command in linux.</p>