-1

I have a dockerized flask app which has the following structure

code_docker
├── docker-compose.yml
├── micr
│   ├── app.py
│   ├── Dockerfile
│   ├── requirements.txt
│   └── templates
│       └── index.html
├── nginx
│   ├── Dockerfile
│   └── nginx.conf
└── README.md

I am using nginx as a load balancer, The code inside docker-compose.yml and nginx.conf are mentioned below

docker-compose.yml

version: '3'

services:

  nginx:
    container_name: nginx
    build: ./nginx
    restart: always
    deploy:
      restart_policy:
        condition: on-failure
    ports:
      - "8540:8540"
    depends_on:
      - micr

  micr:
    build: ./micr
    restart: always 
    deploy: 
      resources:
        limits:
          cpus: '0.50' # 50% of available processing time (CPU)
          memory: 50M  # 50M of memory
        reservations:
          cpus: '0.25'
          memory: 20M
      restart_policy:
        condition: on-failure   
    expose:
      - "8530"
    command: gunicorn -w 1 -b :8530 app:app

nginx.conf

user nginx;

worker_processes 4;

events {
    worker_connections 1024;
}

http {
    keepalive_timeout 15;

    server {
        listen 8540;
        server_name localhost;

        location / {
            proxy_pass http://localhost:8530;
            #proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forward-Host $server_name;
        }
    }
}

I can issue the below command to scale the container without any problem as can be seen from the message

docker-compose up --scale micr=4

But when I try to access them I get this error

enter image description here

Atinesh
  • 1,790
  • 9
  • 36
  • 57

1 Answers1

1
  • You need to create a network for Nginx and proxied containers. For details see this.
services:
  nginx:
    networks:
      - proxy
    ...

  micr:
    networks:
      - proxy
    ...

networks:
  proxy:
  • You need to use service names as container host addresses to archive communication between containers. So set proxy_pass to http://micr:8530
  • You should set docker embedded dns to nginx resolver to achieve ability to recreate containers without having to restart nginx container. See this for details
Artemij Rodionov
  • 1,721
  • 1
  • 17
  • 22