环境
Ubuntu 22
apt-get update && apt install docker.io配置docker镜像加速
vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://mirrors.163.com/dockerhub",
"https://mcr.aliyuncs.com",
"https://docker.xuanyuan.me",
"https://docker.1ms.run"
]
}加载配置,运行docker
#重载配置
systemctl daemon-reload
#重启docker
systemctl restart docker
#设置开机自启命令
systemctl enable docker创建一个自定义目录,并在该目录下创建以下目录及文件
mkdir -p /home/flask && cd /home/flask && mkdir -p app logs conf#### 目录结构
├── app
│ ├── app.py # 项目入口文件
│ └── requirements.txt # 项目上需要的python依赖组件
├── conf
│ └── debian.sources
│ └── uwsgi.ini # uwsgi 服的配置文件
│ └── nginx.conf # nginx 服务配置文件
├── Dockerfile
├── logs # 日志目录
app.py
import logging
from flask import Flask
app = Flask(__name__)
# 配置 Flask 日志
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/var/log/flask_app.log'),
logging.StreamHandler()
]
)
@app.route('/')
def hello_world():
app.logger.info('Accessing the root route')
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')requirements.txt
对应的组件也可以不用写具体版本,也可以写 >=xxx 版本;
pip
uWSGI == 2.0.28
Flask == 3.1.0debian.sources
这个就是系统包文件,因为下面我们用的基础镜像是debian系统,更新自动组件慢,这里换成国内镜像加速地址,不一定是huaweicloud,还有很多;
Types: deb
# http://snapshot.debian.org/archive/debian/20250224T000000Z
URIs: http://repo.huaweicloud.com/debian
Suites: bookworm bookworm-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb
# http://snapshot.debian.org/archive/debian-security/20250224T000000Z
URIs: http://repo.huaweicloud.com/debian-security
Suites: bookworm-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg#### uwsgi.ini
[uwsgi]
module = app:app
master = true
processes = 2
threads = 2
socket = 127.0.0.1:8000
vacuum = true
die-on-term = true
buffer-size = 65536
# 日志配置
logto = /var/log/uwsgi/uwsgi.lognginx.conf
端口选82、日志文件access-1.log 主要是为了和系统默认值区分,
server {
listen 82;
server_name _;
access_log /var/log/nginx/access-1.log;
error_log /var/log/nginx/error-1.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}注意:如果 uwsgi.ini 里是http = 127.0.0.1:8000 这种http监听,那么nginx配置这里就直接是:
location / {
proxy_pass http://127.0.0.1:8000/
}
如果是socket= 127.0.0.1:8000 这种socket监听,那么nginx配置这里就直接是:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
参考:https://www.jianshu.com/p/6b252125221b
# 使用 Python 基础镜像
FROM python:3.9-slim
# 设置非交互式前端
ENV DEBIAN_FRONTEND=noninteractive
# 设置工作目录
WORKDIR /app
# 更新镜像源
COPY conf/debian.sources /etc/apt/sources.list.d/debian.sources
# 复制其他项目文件
COPY app/ .
COPY conf/uwsgi.ini /etc/uwsgi/uwsgi.ini
COPY conf/nginx.conf /etc/nginx/conf.d/app.conf
# 安装 Nginx 和编译工具
# 创建日志目录和日志文件并给 www-data:www-data
# 安装 requirements.txt 文件里的组件
RUN apt-get update && apt-get install -y gcc \
&& apt install -y net-tools curl vim procps iputils-ping \
&& apt-get install -y --fix-broken -o Dpkg::Options::="--force-confnew" nginx \
&& apt-get clean && rm -rf /var/lib/apt/lists/* \
&& mkdir -p /var/log/nginx /var/log/uwsgi \
&& touch /var/log/flask_app.log \
&& chown -R www-data:www-data /var/log/uwsgi/ /var/log/nginx/ \
&& pip install -i https://mirrors.huaweicloud.com/repository/pypi/simple --upgrade --no-cache-dir -r requirements.txt
# 暴露端口
EXPOSE 80
# 启动服务
CMD ["sh", "-c", "nginx -t && service nginx start && uwsgi --ini /etc/uwsgi/uwsgi.ini"]注意:安装nginx的时候 -o Dpkg::Options::="--force-confnew" 带了这个参数,是因为如果不带它会一直报如下错误:
5.208 dpkg: dependency problems prevent configuration of nginx:
5.208 nginx depends on nginx-common (<< 1.22.1-9.1~); however:
5.208 Package nginx-common is not configured yet.
5.208 nginx depends on nginx-common (>= 1.22.1-9); however:
5.208 Package nginx-common is not configured yet.
5.208
5.208 dpkg: error processing package nginx (--configure):
5.208 dependency problems - leaving unconfigured
5.208 Setting up iproute2 (6.1.0-3) ...
5.288 Processing triggers for libc-bin (2.36-9+deb12u9) ...
5.302 Errors were encountered while processing:
5.302 nginx-common
5.302 nginx
5.309 E: Sub-process /usr/bin/dpkg returned an error code (1)这是因为系统依赖导致的问题,网上对这个报错没有答案,测试了好多次加上如上参数ok;
构建镜像
docker build -t my-flask-app .运行容器
docker run -d -p 80:82 -v ./logs/:/var/log/nginx -v ./logs/:/var/log/uwsgi -v ./logs/:/var/log my-flask-app公网访问

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。