Nginx with multiple docker-compose error 111

  docker, docker-compose, nginx

I ‘m trying to deploy a service with multiple docker-compose files using Nginx as reverse-proxy.
My folder structure is:
enter image description here

and my files are:

Apple docker-compose.yaml:

version: "3.5"

services:
  apples:
    build:
      context: ./
      dockerfile: Dockerfile

    networks:
      reverseproxy_santo-dio:
        aliases:
          # ApplesApp hostname on "fruit-network"
          - apples_net

networks:
  reverseproxy_santo-dio:
    external: true

Apple docker:

FROM httpd:2.4
RUN echo "<html><body><h1>A</h1>App A works!</body></html>" > /usr/local/apache2/htdocs/index.html

Orange docker-compose:

version: "3.5"
services:
  orange:
    build:
      context: ./
      dockerfile: Dockerfile

    networks:
     reverseproxy_santo-dio:
        aliases:
          - orange_net
      

networks:
  reverseproxy_santo-dio:
    external: true

Orange docker:

FROM httpd:2.4
RUN echo "<html><body><h1>B</h1>App B works!</body></html>" > /usr/local/apache2/htdocs/index.html 

My reverse-proxy is:

version: '3.5'
services:
  nginx: 
    image: nginx:latest
    container_name: reverse-proxy
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - ./log/:/var/log/nginx
    ports:
      - 80:80
      - 443:443
    networks:
      - santo-dio
    restart: always


networks:
  santo-dio:

where the nginx.conf:

server {
  # Redirect http:// to https://
    listen 80; # default_server;
    listen [::]:80;
    error_log  /var/log/nginx/mylog.error_log  debug;
    
     
    #frontend 
    location / {
        proxy_pass http://apples_net;
    }

    #backend
    location /api/ {
        proxy_pass http://orange_net:8080/api;
    }
  
}

To deploy the service I lauch:

  1. reverse proxy
  2. apple
  3. orange

When I go in my browser apple (localhost) works but orange (localhost/api) answers me with 502 Bad Gateway.
In my logfile:

2021/05/04 11:07:23 [error] 32#32: *4 connect() failed (111:
Connection refused) while connecting to upstream, client:
xxx.xxx.xx.x, server: , request: "GET /api/ HTTP/1.1", upstream: "http://xxx.xxx.xxx.x:8080/api", host: "localhost"

Where is my error?
I started from this project: multiple-docker-compose-same-ports

Thank

Source: Docker Questions

LEAVE A COMMENT