通过nginx和gunicorn在码头工作服务烧瓶

时间:2017-01-22 10:04:19

标签: python nginx flask docker-compose gunicorn

玩烧瓶我想在Docker中进行真正的设置和运行。这意味着烧瓶应该通过nginx和gunicorn提供。我设置了一个示例代码存储库https://github.com/geoHeil/pythonServing,但到目前为止还无法使nginx正常工作。

Flask在application:5000上提供,docker应该将应用程序解析为其各自的名称。

Nginx配置如下:

server {

    listen 8080;
    server_name application;
    charset utf-8;

    location / {
        proxy_pass http://application:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

对我来说很好看。到目前为止,我无法找到问题。

修改

撰写文件在这里。启动命令是

docker-compose build
docker-compose up

version: '2'
services:
  application:
    restart: always
    build: ./application
    command: gunicorn -w 4 --bind :5000 wsgi:application
    links:
      - db
    expose:
     - "5000"
    ports:
      - "5000:5000"
  nginx:
    restart: always
    build: ./nginx
    links:
      - application
    expose:
      - 8080
    ports:
      - "8880:8080"

2 个答案:

答案 0 :(得分:1)

您的 nginx 配置文件位置错误。

修复步骤:

sudo docker-compose down

删除nginx图片:

sudo docker images
sudo docker rmi 
REPOSITORY                       TAG                 IMAGE ID            CREATED              SIZE
pythonserving_nginx              latest              152698f13c7a        About a minute ago   54.3 MB

sudo docker rmi pythonserving_nginx

现在更改nginx Dockerfile

FROM nginx:1.11.8-alpine
MAINTAINER geoheil

ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf

请注意nginx配置的位置。

现在尝试这个docker-compose文件(使用用户定义的网络):

version: '2'
services:
  application:
    restart: always
    build: ./application
    command: gunicorn -w 4 --bind :5000 wsgi:application
    networks:
      - testnetwork
    expose:
     - "5000"
    ports:
      - "5000:5000"
  db:
    restart: always
    image: postgres:9.6.1-alpine
    networks:
      - testnetwork
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=d
      - POSTGRES_PASSWORD=d
      - POSTGRES_DB=d
    volumes:
      - ./postgres:/var/lib/postgresql
  nginx:
    restart: always
    build: ./nginx
    networks:
      - testnetwork
    expose:
      - 8080
    ports:
      - "8880:8080"
networks:
  testnetwork:

带上容器:

sudo docker-compose up

浏览至http://localhost:8880

答案 1 :(得分:0)

Smaple docker file

FROM python:3.5

RUN apt-get update
RUN apt-get install -y --no-install-recommends \
        libatlas-base-dev gfortran nginx supervisor

RUN pip3 install uwsgi

COPY ./requirements.txt /project/requirements.txt

RUN pip3 install -r /project/requirements.txt

RUN useradd --no-create-home nginx

RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache

COPY nginx.conf /etc/nginx/
COPY flask-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/

COPY /app /project

WORKDIR /project

CMD ["/usr/bin/supervisord"]