[英]serving flask via nginx and gunicorn in docker
Playing around with flask I would like to get a real setup up and running in docker.在玩烧瓶时,我想在 docker 中进行真正的设置和运行。 This means flask should be served via nginx and gunicorn.
这意味着flask 应该通过nginx 和gunicorn 提供服务。 I set up a sample code repository https://github.com/geoHeil/pythonServing but so far can't get nginx to work properly.
我设置了一个示例代码存储库https://github.com/geoHeil/pythonServing但到目前为止还不能让 nginx 正常工作。
Flask is served on application:5000
, docker should resolve application to its respective name. Flask 服务于
application:5000
,docker 应将应用程序解析为其各自的名称。
Nginx config is as follows: 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;
}
}
which looks good to me.这对我来说看起来不错。 So far I cant find the problem.
到目前为止我找不到问题。
compose file is here.撰写文件在这里。 Command to start was
启动命令是
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"
Your nginx config file is in a wrong location.您的nginx配置文件位于错误的位置。
Steps to fix:修复步骤:
sudo docker-compose down
Delete nginx image:删除 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
Now change the nginx Dockerfile :现在更改 nginx Dockerfile :
FROM nginx:1.11.8-alpine
MAINTAINER geoheil
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
Please note the location of nginx config.请注意 nginx 配置的位置。
Now try this docker-compose file (Using User-defined Networks):现在试试这个 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:
And Bring up containers:并调出容器:
sudo docker-compose up
Browse to http://localhost:8880浏览到http://localhost:8880
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"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.