title: How to configure Nginx with Docker to run Node and Mongo in the correct way
body: <p>I'm building a project in Node Mongo and using Docker plus Nginx to run all on my local machine MacBook pro-Catalina.
  I have several issues I'm unable to fix on my own and I don't know how to configure Nginx and Docker to make all work correctly. At the end of the post will be shown my Dockerfile, Docker compose and Nginx.</p>
  <ol>
  <li>The Node container is not able to connect to the Mongo DB with this error when I run my containers: <code>failed to connect to server [127.0.0.1:27017] on first connect</code> but via a GUI as compass I'm able to connect to the same URL.</li>
  <li>EDIT I tried to use the name of the container and it's IP bu error <code>failed to connect to server [wetaxitask_mongodb:27017] on first connect [Error: connect ECONNREFUSED 172.18.0.3:27017</code></li>
  <li>The next issue is I'm getting an error of Python even tho I'm installing it on my container and the error is as shown in screenshot as I don't know how to describe: <a href="https://i.stack.imgur.com/VH0Sx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VH0Sx.jpg" alt="enter image description here" /></a></li>
  <li>When all is running whatever endpoint I'm trying to hit my response is the Nginx welcome page only so probably I did wrong the conf itself<a href="https://i.stack.imgur.com/byNSY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/byNSY.png" alt="enter image description here" /></a></li>
  </ol>
  <p>Dockerfile:</p>
  <pre><code># Developpment stage
  FROM node:12.18-alpine AS base-builder
  RUN apk update
  RUN apk add --no-cache python make g++
  RUN apk add --no-cache libc6-compat
  WORKDIR /usr/src/app
  COPY [&quot;tsconfig.json&quot;, &quot;package.json&quot;, &quot;package-lock.json*&quot;, &quot;npm-shrinkwrap.json*&quot;, &quot;./&quot;]
  ADD . /usr/src/app
  COPY ./.env /usr/src/app/.env
  RUN npm cache clean --force
  RUN npm install
  
  # ------------------------------------------------------
  # Production Build
  # ------------------------------------------------------
  FROM nginx:1.19.0-alpine
  RUN apk add --no-cache --repository http://nl.alpinelinux.org/alpine/edge/main libuv \
      &amp;&amp;  apk add --update nodejs npm
  
  WORKDIR /usr/src/app
  RUN ls -l /usr/src/app/
  COPY --from=0  /usr/src/app/ .
  RUN rm /etc/nginx/conf.d/default.conf
  RUN rm -rf /docker-entrypoint.d
  COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
  EXPOSE 3000
  EXPOSE 80
  #CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;,&quot;npm&quot;, &quot;start&quot;]
  CMD nginx ; exec npm start
  </code></pre>
  <p>Docker compose:</p>
  <pre><code>version: '3.8'
  
  services:
    wetaxitask:
      container_name: wetaxitask_api_dev
      image: wetaxitask
      restart: always
      build: .
      depends_on:
        - mongodb
        - redis
      env_file: .env
      ports:
        - 8080:80
      links:
        - redis
        - mongodb
  
    mongodb:
      container_name: wetaxitask_mongodb
      image: mongo:latest
      restart: always
      ports:
        - 27017:27017
      volumes:
        - ./data:/data/db
      environment:
        - MONGO_INITDB_DATABASE=wetaxitask
  
    redis:
      container_name: wetaxitask_redis
      image: redis:latest
      ports:
        - 6379:6379
  </code></pre>
  <p>Nginx:</p>
  <pre><code>server {
  
    listen 80;
  
    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }
  
    location /welcome {
      proxy_pass http://127.0.0.1:3000;
  
    }
  
    error_page   500 502 503 504  /50x.html;
  
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
  </code></pre>