无法扩展Docker容器

时间:2019-02-13 08:56:09

标签: python docker nginx flask docker-compose

我有一个具有以下结构的dockerized flask应用程序

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

我使用nginx作为负载均衡器,下面提到docker-compose.ymlnginx.conf中的代码

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;
        }
    }
}

我可以发出以下命令来缩放容器,而不会出现任何问题,如message

所示
docker-compose up --scale micr=4

但是当我尝试访问它们时,出现此错误

enter image description here

1 个答案:

答案 0 :(得分:1)

  • 您需要为Nginx和代理容器创建一个网络。有关详细信息,请参见this
services:
  nginx:
    networks:
      - proxy
    ...

  micr:
    networks:
      - proxy
    ...

networks:
  proxy:
  • 您需要使用服务名称作为容器主机地址,以归档容器之间的通信。 因此,将proxy_pass设置为http://micr:8530
  • 您应该将docker Embedded dns设置为nginx resolver,以实现无需重新启动nginx容器即可重新创建容器的功能。有关详情,请参见this