我试图在Ubuntu中设置一个Gunicorn服务器:最新的Docker映像。
在Docker上启动时,我有以下输出
[2020-08-01 14:12:38 +0000] [6] [INFO] Starting gunicorn 20.0.4
[2020-08-01 14:12:38 +0000] [6] [DEBUG] Arbiter booted
[2020-08-01 14:12:38 +0000] [6] [INFO] Listening at: http://0.0.0.0:5000 (6)
[2020-08-01 14:12:38 +0000] [6] [INFO] Using worker: sync
[2020-08-01 14:12:38 +0000] [8] [INFO] Booting worker with pid: 8
[2020-08-01 14:12:38 +0000] [6] [DEBUG] 1 workers在那之后,我看不到终端上的任何输出。我用电脑上打开的sudo netstat -tunlp检查了端口,Gunicorn没有运行。
如果我在虚拟主机中使用相同的gunicorn --bind 0.0.0.0:5000 app:app --log-level debug设置,它会很好.我错过了什么吗?
# ------------------------------------------------------------------------------
# Production image based on ubuntu:latest with nginx & python3
# ------------------------------------------------------------------------------
FROM ubuntu:latest as prod-react
WORKDIR /usr/src/app
# update, upgrade and install packages
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get upgrade -y
RUN apt-get install -y nginx curl python3 python3-distutils python3-apt
# install pip
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3 get-pip.py
# copy flask-api requirements file and install modules
COPY ./flask-api/requirements.txt ./
RUN pip install -r requirements.txt
RUN pip install gunicorn
# copy flask code
COPY ./flask-api/app.py .
# ------------------------------------------------------------------------------
# Serve flask-api with gunicorn
# ------------------------------------------------------------------------------
EXPOSE 5000
CMD gunicorn --bind 0.0.0.0:5000 app:app --log-level debug烧瓶python代码
from flask import Flask, request
from flask_cors import CORS
from flask_restful import Api, Resource
import time
# ------------------------------------------------------------------------------
# Flask app config + CORS
# ------------------------------------------------------------------------------
app = Flask(__name__)
app.config.from_object(__name__)
app.config['DEBUG'] = True
# accept CORS from frontend app
CORS(app, origins=['*'], supports_credentials=True)
# ------------------------------------------------------------------------------
# RESTFUL API
# ------------------------------------------------------------------------------
API_PREFIX = '/api'
api = Api(prefix=API_PREFIX)
class AuthenticationAPI(Resource):
def get(self):
print('auth get', flush=True)
return {'res': 'get'}, 200
def post(self):
print('auth post', flush=True)
print(request.get_json())
return {'res': 'post'}, 200
class TimeAPI(Resource):
def get(self):
print('time get', flush=True)
return {'res': time.time()}, 200
api.add_resource(AuthenticationAPI, '/auth')
api.add_resource(TimeAPI, '/time')
api.init_app(app)发布于 2020-08-02 13:03:01
我设法使它在本地开发上工作。下面的Dockerfile包含所有修改,我对命令进行了注释,以使其正常工作。
# ------------------------------------------------------------------------------
# Temporary image for react.js app using a multi-stage build
# ref : https://docs.docker.com/develop/develop-images/multistage-build/
# ------------------------------------------------------------------------------
FROM node:latest as build-react
# create a shared folder and define it as current dir
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# copy the files required for node packages installation
COPY ./react-client/package.json ./
COPY ./react-client/yarn.lock ./
# install dependencies, copy code and build bundles
RUN yarn install
COPY ./react-client .
RUN yarn build
# ------------------------------------------------------------------------------
# Production image based on ubuntu:latest with nginx & python3
# ------------------------------------------------------------------------------
FROM ubuntu:latest as prod-react
WORKDIR /usr/src/app
# update, upgrade and install packages
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get upgrade -y
RUN apt-get install -y nginx curl python3 python3-distutils python3-apt
# install pip
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
RUN python3 get-pip.py
# copy flask-api requirements file and install modules
COPY ./flask-api/requirements.txt ./
RUN pip install -r requirements.txt
RUN pip install gunicorn
# copy flask code
COPY ./flask-api/app.py .
# copy built image and config onto nginx directory
COPY --from=build-react /usr/src/app/build /usr/share/nginx/html
COPY ./conf.nginx /etc/nginx/conf.d/default.conf
# ------------------------------------------------------------------------------
# Serve flask-api with gunicorn and react-client with nginx
# Ports :
# - 5000 is used for flask-api
# - 8080 is used by nginx to serve react-client
# You can change them but you'll have to change :
# - for flask-api : conf.nginx, axios calls (5000 -> @newApiPort)
# - for react-client : CORS origins (8080 -> @newClientPort)
#
# To build and run this :
# docker build -t @imageName .
# docker run -d --name @containerName -e "PORT=8080" -p 8080:8080 -p 5000:5000 @imageName
# ------------------------------------------------------------------------------
CMD gunicorn --bind 0.0.0.0:5000 app:app --daemon && \
sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/default.conf && \
nginx -g 'daemon off;'发布于 2020-08-01 14:28:21
从曝露指令的坞文档中:
公开指令实际上并不发布端口。它作为构建映像的人和运行容器的人之间的一种文档类型,而这些港口将被发布。要在运行容器时实际发布端口,请在运行的码头上使用-p标志发布和映射一个或多个端口,或者使用-P标志发布所有公开的端口,并将它们映射到高级端口。
因此,由于gunicorn正在监听端口500,所以您需要这样运行您的容器:
docker run -p 5000:5000 your_image传递您在运行容器时已经传递的任何其他选项。
https://stackoverflow.com/questions/63206239
复制相似问题