一、拉取Python3.6.4 镜像
docker pull python:3.6.4
二、宿主机创建testflask目录,并添加文件
touch app.py docker-compose.yml Dockerfile gunicorn.conf.py requirements.txt
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World! I have been seen {} times.\n'.format(1)
if __name__ == "__main__":
app.run(debug=True)
docker-compose.yml
version: '2'
services:
flaskweb:
build: .
volumes:
- .:/code # 把当前目录映射到docker容器的/code 目录下
ports:
- 8888:8888
Dockerfile
FROM python:3.6.4
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["gunicorn","app:app","-c","./gunicorn.conf.py"]
requirements.txt
flask
gunicorn
gevent
三、运行:
docker-compose up --build
四、配置nginx
在https://www.jianshu.com/p/4c57fcd0b9e3的基础上修改
docker-compose.yml
version: '2'
services:
nginx:
image: nginx:latest
restart: always
container_name: nginx
volumes:
- /opt/nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf
- /opt/nginx/conf.d:/etc/nginx/conf.d
- /opt/nginx/html:/usr/share/nginx/html
- /opt/nginx/logs/nginx:/var/log/nginx
ports:
- 8887:8887
conf.d/default.conf
server {
listen 8887;
server_name 192.168.56.102;
# rewrite ^(.*)$ https://www.vhxsl.com permanent;
location / {
include uwsgi_params;
add_header Access-Control-Allow-Origin *;
proxy_read_timeout 3600s;
proxy_redirect off;
proxy_http_version 1.1;
#proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://192.168.56.102:8888; #gunicorn IP
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
最后rebuild nginx容器
访问http://IP:8887