使用docker容器部署flask项目 发表于 2022-09-25 更新于 2023-12-14 分类于 docker flask项目123456789101112# /server.pyfrom flask import Flask, render_templateapp = Flask(__name__)@app.route('/index')def index(): return render_template('index.html', name='tom')if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) 1234567891011<!-- /template/index.html --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>hello,{{name}}!</body></html> 1234567# 项目启动脚本#!/bin/bash# 激活虚拟环境. env/bin/activateexport FLASK_APP=serverflask run -h 0.0.0.0 制作docker镜像1234567891011# Dockerfile# 基于python3.9FROM python:3.9# 设置工作目录WORKDIR /approot1/flask# 将本项目中所有文件上传到服务器ADD . .# 下载依赖。在项目正常运行后,可以使用pipreqs生成requirements.txt文件RUN pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com -r requirements.txt# 要注意使用双引号CMD ["/bin/bash","run.sh"] 1234# 构建镜像docker build -t flask:v1.0# 生成容器docker run -p 5000:5000 -d flask:v1.0