diff --git a/demo/python310-flask/.dockerignore b/demo/python310-flask/.dockerignore new file mode 100644 index 0000000..335ae23 --- /dev/null +++ b/demo/python310-flask/.dockerignore @@ -0,0 +1,6 @@ +__pycache__/ +*.pyc +.venv/ +venv/ +.git/ +.DS_Store diff --git a/demo/python310-flask/.gitignore b/demo/python310-flask/.gitignore new file mode 100644 index 0000000..ad03bc7 --- /dev/null +++ b/demo/python310-flask/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +.venv/ +venv/ diff --git a/demo/python310-flask/Dockerfile b/demo/python310-flask/Dockerfile new file mode 100644 index 0000000..ea61f4b --- /dev/null +++ b/demo/python310-flask/Dockerfile @@ -0,0 +1,18 @@ +# 单阶段:runtime-python310 已含 Python 3.10.0 + pip 镜像源(见 image/runtime-python310),编译与运行共用 +FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-python310 +LABEL MAINTAINER="tianjun@odboy.cn" +ENV TimeZone=Asia/Shanghai +ENV PYTHONUNBUFFERED=1 +ENV APP_PORT=8083 +WORKDIR /home/admin + +# 先只拷贝依赖清单,依赖不变时复用 pip 层缓存 +COPY requirements.txt ./ +RUN pip3 install --no-cache-dir -r requirements.txt + +COPY app.py ./ +COPY templates ./templates + +EXPOSE 8083 +# exec 形式,保证 docker stop 的 SIGTERM 能传到 python 进程 +ENTRYPOINT ["python3", "app.py"] diff --git a/demo/python310-flask/README.md b/demo/python310-flask/README.md new file mode 100644 index 0000000..94cb474 --- /dev/null +++ b/demo/python310-flask/README.md @@ -0,0 +1,91 @@ +# python310-flask 示例 + +演示如何用 `runtime-python310` 镜像构建并运行一个 Python 3.10 + Flask Web 应用: + +| 阶段 | 镜像 | 说明 | +| --- |-----------------------------------------------------------------|-------------------------------------------------| +| 编译 + 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-python310` | 含 Python 3.10.0 与 pip 镜像源(见 `image/runtime-python310`) | + +与 dotnet 示例不同,Python 是解释型语言,不需要单独的编译镜像:`pip install` 与直接运行都在同一个镜像内完成。 + +### 示例内容 + +``` +demo/ +├── app.py # Flask 应用:GET / 运行时信息,GET /health,GET /api/hello/,GET /page 页面 +├── templates/ +│ └── index.html # /page 使用的 Jinja2 模板 +├── requirements.txt # 依赖清单(Flask 3.0.3) +├── Dockerfile # 单阶段构建:runtime-python310 +├── build.sh # 构建镜像(PUSH=1 时同时推送) +├── docker-compose.yml # 部署编排(端口 8083,固定 IP 172.30.0.20) +├── startup.sh # docker-compose up -d +└── shutdown.sh # docker-compose down +``` + +### 构建与部署 + +```shell +sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-python310-flask . +PUSH=1 sh build.sh # 构建并推送 +sh startup.sh # docker-compose -f docker-compose.yml -p python310-flask-demo up -d +``` + +访问: + +```shell +curl http://127.0.0.1:8083/ # {"message":"python310-flask is running","python":"3.10.0",...} +curl http://127.0.0.1:8083/health # ok +curl http://127.0.0.1:8083/api/hello/demo # {"message":"Hello, demo!",...} +``` + +浏览器打开 http://127.0.0.1:8083/page 可看到渲染后的运行信息页面。 + +### 关键点 + +**依赖层缓存**:先只 `COPY requirements.txt` 执行 `pip install`,再 `COPY` 源码。只改 `app.py` 时不会重装依赖,重复构建更快。 +基础镜像已预置 `~/.pip/pip.conf` 指向阿里云源,`pip install` 无需额外换源。 + +**监听地址**:容器内监听 `8083`(`APP_PORT`),避开 dotnet6/8/9 示例占用的 8080/8081/8082;宿主机映射与容器端口一致。 + +**时区**:代码内统一按 UTC+8 输出时间,即使容器 `TZ` 未生效也不会差 8 小时;compose 中用 `TZ` 再显式声明一次。 + +**日志**:`PYTHONUNBUFFERED=1` 关闭 stdout 缓冲,`docker logs` 能实时看到请求日志;Flask 日志本身输出到 stderr。 + +**入口**:`ENTRYPOINT ["python", "app.py"]`,exec 形式保证 `docker stop` 的 SIGTERM 能传到进程。 + +**健康检查**:运行镜像自带 `net-tools`,直接用 `netstat` 探测端口,不需要额外安装 curl。 + +### 运维命令 + +```shell +docker logs -f python310-flask-demo # 应用日志(stdout/stderr) +docker exec python310-flask-demo python -V # 确认 Python 版本 +docker exec python310-flask-demo pip list # 确认依赖版本 +``` + +### 调试时直接在运行镜像里运行 + +需要临时改代码看效果时,可以只挂载源码进运行镜像(不需要构建镜像): + +```shell +docker run --rm -it -p 8083:8083 \ + -v "$PWD":/home/admin -w /home/admin \ + registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-python310 \ + sh -c 'pip install -r requirements.txt && python app.py' +``` + +### 生产运行 + +示例用 `app.run()` 的内置服务器,便于直接 `python app.py` 演示。生产环境建议换成 WSGI 服务器, +例如在 `requirements.txt` 增加 `gunicorn==21.2.0`,入口改为: + +```dockerfile +ENTRYPOINT ["gunicorn", "-w", "4", "-b", "0.0.0.0:8083", "app:app"] +``` + +### 停止 + +```shell +sh shutdown.sh +``` diff --git a/demo/python310-flask/app.py b/demo/python310-flask/app.py new file mode 100644 index 0000000..cc798d1 --- /dev/null +++ b/demo/python310-flask/app.py @@ -0,0 +1,50 @@ +import os +import platform +import socket +from datetime import datetime, timedelta, timezone + +import flask + +app = flask.Flask(__name__) +# 国内时区 UTC+8,容器内 TZ 即使没生效也能输出正确时间 +TZ8 = timezone(timedelta(hours=8)) +START_TIME = datetime.now(TZ8) + + +def runtime_info(): + return { + "message": "python310-flask is running", + "python": platform.python_version(), + "flask": flask.__version__, + "impl": platform.python_implementation(), + "os": platform.platform(), + "hostname": socket.gethostname(), + "time": datetime.now(TZ8).strftime("%Y-%m-%d %H:%M:%S %z"), + "uptime": str(datetime.now(TZ8) - START_TIME).split(".")[0], + } + + +@app.get("/") +def index(): + return flask.jsonify(runtime_info()) + + +@app.get("/health") +def health(): + return "ok" + + +@app.get("/api/hello/") +def hello(name): + return flask.jsonify({"message": f"Hello, {name}!", "time": datetime.now(TZ8).isoformat()}) + + +@app.get("/page") +def page(): + return flask.render_template("index.html", info=runtime_info()) + + +if __name__ == "__main__": + # 容器内监听 8083,避免与 dotnet6/8/9 示例的 8080/8081/8082 冲突;可用环境变量 APP_PORT 覆盖 + port = int(os.environ.get("APP_PORT", "8083")) + app.run(host="0.0.0.0", port=port) diff --git a/demo/python310-flask/build.sh b/demo/python310-flask/build.sh new file mode 100755 index 0000000..06390ed --- /dev/null +++ b/demo/python310-flask/build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# 在示例目录下构建示例镜像 +set -euo pipefail +cd "$(dirname "$0")" + +IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-python310-flask} +docker build -t "${IMAGE}" . + +if [ "${PUSH:-0}" = "1" ]; then + docker push "${IMAGE}" +fi diff --git a/demo/python310-flask/docker-compose.yml b/demo/python310-flask/docker-compose.yml new file mode 100644 index 0000000..358de61 --- /dev/null +++ b/demo/python310-flask/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3.8' + +networks: + python310-flask-demo: + ipam: + driver: default + config: + - subnet: "172.30.0.0/24" + +services: + demo: + image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-python310-flask # 镜像由 build.sh 构建 + container_name: python310-flask-demo + restart: unless-stopped + ports: # 映射端口 + - "8083:8083" + environment: # 设置环境变量,相当于docker run命令中的-e + TZ: Asia/Shanghai + APP_PORT: "8083" + healthcheck: + # 运行镜像自带 net-tools,直接用端口探测,不额外安装 curl + test: ["CMD-SHELL", "netstat -tln | grep -q ':8083 '"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 5s + networks: + python310-flask-demo: + ipv4_address: 172.30.0.20 diff --git a/demo/python310-flask/requirements.txt b/demo/python310-flask/requirements.txt new file mode 100644 index 0000000..f306f93 --- /dev/null +++ b/demo/python310-flask/requirements.txt @@ -0,0 +1,2 @@ +Flask==3.0.3 +Werkzeug==3.0.3 diff --git a/demo/python310-flask/shutdown.sh b/demo/python310-flask/shutdown.sh new file mode 100755 index 0000000..abc09c3 --- /dev/null +++ b/demo/python310-flask/shutdown.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" +docker-compose -f docker-compose.yml -p python310-flask-demo down diff --git a/demo/python310-flask/startup.sh b/demo/python310-flask/startup.sh new file mode 100755 index 0000000..ab2a0ba --- /dev/null +++ b/demo/python310-flask/startup.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" +docker-compose -f docker-compose.yml -p python310-flask-demo up -d diff --git a/demo/python310-flask/templates/index.html b/demo/python310-flask/templates/index.html new file mode 100644 index 0000000..ef87888 --- /dev/null +++ b/demo/python310-flask/templates/index.html @@ -0,0 +1,28 @@ + + + + + python310-flask + + + +

python310-flask 运行信息

+ + {% for key, value in info.items() %} + + + + + {% endfor %} +
{{ key }}{{ value }}
+

+ 其它接口:/api/hello/<name>、/health +

+ +