title: HAProxy/Docker: 502 Bad Gateway when hitting Docker container running Flask/React app
body: <p>I am trying to Dockerize a Flask/React web application for ease of development/collaboration, but having issues getting a proper response from the application. I am able to get the image built and Flask server started in a container, but having issues actually hitting it.</p>
  
  <p>We use HAProxy to forward requests, and things work fine when I have the proxy and web server running locally. The issue has been getting docker into the mix. I believe it must be a port mapping issue, but I'm out of ideas and feel I may be missing key HAProxy/Docker subtleties. The proxy.cfg file looks as follows (with extraneous hosts not included):</p>
  
  <pre><code>global
   maxconn 4096
   pidfile ~/tmp/haproxy.pid
  
  defaults
   log global
   log 127.0.0.1 local0
   log 127.0.0.1 local1 notice
   mode http
   timeout connect 300000
   timeout client 300000
   timeout server 300000
   maxconn 2000
   option redispatch
   retries 3
   option httpclose
   option httplog
   option forwardfor
   option httpchk HEAD / HTTP/1.0
  
  
  frontend dev
     bind *:8080 ssl crt ./proxy.pem
  
      acl allow_web path_beg /app/
  
      use_backend be_web if allow_web
  
  backend be_web
   balance roundrobin
   server web_5000 localhost:5000
  </code></pre>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM node:10.6.0
  
  RUN apt-get update
  RUN apt-get install -y python-pip python-dev build-essential 
  
  WORKDIR /usr/src/app
  
  COPY ./package.json .
  RUN npm install
  COPY . .
  RUN pip install -e ./server
  
  CMD ["npm", "start"]
  </code></pre>
  
  <p>docker-compose.yml:</p>
  
  <pre><code>version: "3"
  
  services:
      userportal:
          build: .
          volumes:
              - /usr/src/app/node_modules
              - .:/usr/src/app
          ports:
              - "5000:5000"
  </code></pre>
  
  <p>The Flask server binds to port 5000 hence the mapping. I've tried substituting the container IP address for <code>localhost</code> (e.g. <code>172.19.0.2:5000</code>) but same result.</p>
  
  <p>Edit:</p>
  
  <p>I tried adding the proxy as a service in the docker-compose.yml and changing the host from <code>localhost:5000</code> to <code>userportal_1:5000</code>, but this led to a 503. docker-compose.yml:</p>
  
  <pre><code>services:
      userportal:
          build: .
          volumes:
              - /usr/src/app/client/node_modules
              - ./client:/usr/src/app/client
              - ./server:/usr/src/app/server
          ports:
              - "5000:5000"
      proxy:
          image: haproxy:alpine
          volumes:
              - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
              - ./proxy.pem:/certs/proxy.pem
          ports:
              - "8080:8080"
  </code></pre>