0

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

2
  • You proxy_pass http://orange_net:8080; is the "orange" container listening on that port, or the default HTTP port 80? Commented May 4, 2021 at 11:38
  • I think it uses the port 80, becose in the network it has "orange_net" as alias. But if I use "orange_net:80" I get the same bad response. Commented May 4, 2021 at 12:44

1 Answer 1

0

I found a solution. I had a problem with nginx.conf. I don't understand why

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

it doesn't works but patience. This setup is perfect for my purpose.

REVERSE-PROXY:

#/reverse-proxy/docker-compose.yaml
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:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.10.10.0/16

--

#/reverse-proxy/nginx.conf
server {
  server_name apples_net.com;
  #resolver 127.0.0.11 valid=5s; # Local Docker DNS
   error_log  /var/log/nginx/apples_net.error_log  debug;

  location / {
    auth_request off;
    #proxy_pass http://apples_net:80;
    proxy_pass http://10.10.10.2:80;
  }
}

server {
  server_name orange_net.com;
  #resolver 127.0.0.11 valid=5s; # Local Docker DNS
   error_log  /var/log/nginx/orange_net.error_log  debug;

  location / {
    auth_request off;
    #proxy_pass http://orange_net:80;
    proxy_pass http://10.10.10.1:80;
    
  }
}

APPLES:

#/apples/docker-compose.yaml
#/apples/Dockerfile
version: "3.5"

services:
  apples:
    build:
      context: ./
      dockerfile: Dockerfile
    networks:
      reverseproxy_santo-dio:
        ipv4_address: 10.10.10.2
        aliases:
          - apples_net

networks:
  reverseproxy_santo-dio:
    external: true

ORANGES:

#/oranges/docker-compose.yaml
#/oranges/Dockerfiles
version: "3.5"
services:
  orange:
    build:
      context: ./
      dockerfile: Dockerfile
    # expose:
    #   - 8081
    networks:
     reverseproxy_santo-dio:
        ipv4_address: 10.10.10.1
        aliases:
          - orange_net
      

networks:
  reverseproxy_santo-dio:
    external: true

In this version I mapped the IP addresses of my applications. But it's possible to skip that step and just use aliases

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.