【问题标题】:serving flask via nginx and gunicorn in docker在 docker 中通过 nginx 和 gunicorn 服务烧瓶
【发布时间】:2017-06-06 23:21:31
【问题描述】:

玩烧瓶我想在 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"

【问题讨论】:

  • 尝试从 nginx 容器 ping 应用程序。
  • @FarhadFarahi 确实如此,就像PING application (192.168.0.3): 56 data bytes 64 bytes from 192.168.0.3: seq=0 ttl=64 time=0.091 ms 64 bytes from 192.168.0.3: seq=1 ttl=64 time=0.129 ms
  • Gimmie 你运行的命令来设置环境,如果你使用 docker compose,请提供 compose 文件。
  • @FarhadFarahi 请查看编辑以及github.com/geoHeil/pythonServing

标签: python nginx flask docker-compose gunicorn


【解决方案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

【讨论】:

  • 奇怪。那还不行。访问或错误日志中没有任何内容。
  • 你做了所有的步骤吗?因为我试过了。专门删除旧图像以使 compose 构建新图像
  • 确实,我删除了图像。它对我不起作用。我正在使用 docker for mac。
  • 我在新的虚拟机 ubuntu 上重试。同样的问题。
  • sudo docker exec -it <nginx container ID> /bin/sh 然后 ls /etc/nginx/ 和 ls /etc/nginx/conf.d/,给我两个 ls 命令的输出
【解决方案2】:
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"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 2021-01-24
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2017-05-11
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多