从源代码开始构建、部署和管理应用程序

1.创建项目目录并准备应用程序的代码及其依赖

        1.创建项目目录,并将当前目录切换到该目录

[root@host1 ~]# mkdir python-web && cd python-web

        2.创建 app.py 文件并添加以下代码

[root@host1 python-web]# vi app.py
[root@host1 python-web]# cat app.py
import time
import redis
from flask import Flaskapp = Flask(__name__)cache = redis.Redis(host='redis', port=6379)def get_hit_count():retries = 5while True:try:return cache.incr('hits')except redis.exceptions.ConnectionError as exc:if retries == 0:raise excretries -= 1time.sleep(0.5)@app.route('/')
def hello():count = get_hit_count()return '你好!已访问{}次。\n'.format(count)if __name__ == '__main__':app.run(host='0.0.0.0', port=5000)

        3.创建另一个文本文件 requirements.txt

 

[root@host1 python-web]# vi requirements.txt
[root@host1 python-web]# cat requirements.txt
flask
redis

2.创建 Dockerfile

[root@host1 python-web]# vi Dockerfile
[root@host1 python-web]# cat Dockerfile
# syntax=docker/dockerfile:1
# 基于python:3.7-alpine镜像构建(轻量且适合生产环境)
FROM python:3.7-alpine# 将工作目录设置为/code
WORKDIR /code# 设置flask命令使用的环境变量
# 修正:FLASK APP→FLASK_APP,app.Py→app.py(Python文件通常小写)
ENV FLASK_APP=app.py
# 修正:FLASK RUN HOST→FLASK_RUN_HOST(环境变量用下划线连接)
ENV FLASK_RUN_HOST=0.0.0.0# 安装GCC和其他依赖(编译Python依赖时需要,如redis模块)
RUN apk add --no-cache gcc musl-dev linux-headers# 复制依赖清单并安装Python依赖
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt  # 添加--no-cache-dir减少镜像体积# 向镜像中添加元数据,描述容器监听端口5000
EXPOSE 5000# 将项目当前目录所有文件复制到镜像的工作目录
COPY . /code# 将容器启动的默认命令设置为flask run
CMD ["flask", "run"]

3.创建 Compose 文件以定义服务

[root@host1 python-web]# vi compose.yaml
[root@host1 python-web]# cat compose.yaml
services:web:# 从当前目录的Dockerfile构建镜像(修正:补全build配置,添加上下文路径)build: .ports:- "8000:5000"  # 主机8000端口映射到容器5000端口(Flask默认端口)depends_on:- redis  # 确保redis服务先启动,避免连接失败restart: always  # 容器异常时自动重启redis:# 使用轻量的Redis Alpine镜像(适合生产环境,体积小)image: "redis:alpine"# 可选:添加数据持久化(避免Redis重启后计数丢失)volumes:- redis_data:/datarestart: always  # 确保Redis服务稳定运行# 定义Redis数据卷(持久化存储计数数据)
volumes:redis_data:

4.通过 Compose 构建并运行应用程序

        1.启动应用程序

从 Docker 构建日志来看,RUN apk add --no-cache gcc musl-dev linux-headers 这一步耗时极长(超过 55 分钟),原因是默认的 Alpine 软件源(dl-cdn.alpinelinux.org)访问慢或网络不稳定,导致下载编译依赖(GCC、musl-dev 等)耗时过久。

        解决方法:更换为国内 Alpine 软件源

[root@host1 python-web]# vi Dockerfile
[root@host1 python-web]# cat Dockerfile
# syntax=docker/dockerfile:1
# 基于python:3.7-alpine镜像构建(轻量且适合生产环境)
FROM python:3.7-alpine# 将工作目录设置为/code
WORKDIR /code# 设置flask命令使用的环境变量
# 修正:FLASK APP→FLASK_APP,app.Py→app.py(Python文件通常小写)
ENV FLASK_APP=app.py
# 修正:FLASK RUN HOST→FLASK_RUN_HOST(环境变量用下划线连接)
ENV FLASK_RUN_HOST=0.0.0.0# 替换Alpine软件源为阿里云源(国内访问更快)
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories# 再执行依赖安装(此时从国内源拉取,速度会大幅提升)
RUN apk add --no-cache gcc musl-dev linux-headers# 复制依赖清单并安装Python依赖
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt  # 添加--no-cache-dir减少镜像体积# 向镜像中添加元数据,描述容器监听端口5000
EXPOSE 5000# 将项目当前目录所有文件复制到镜像的工作目录
COPY . /code# 将容器启动的默认命令设置为flask run
CMD ["flask", "run"]

        重新构建并启动服务:
在 python-web 目录下执行:

docker compose up
[+] Building 222.9s (14/15)                                                                          [+] Building 223.1s (14/15)                                                                          [+] Building 223.2s (14/15)                                                                          [+] Building 223.4s (14/15)                                                                          [+] Building 223.5s (14/15)                                                                          [+] Building 223.7s (14/15)                                                                          [+] Building 223.8s (14/15)                                                                          [+] Building 223.9s (14/15)                                                                          [+] Building 224.0s (16/16) FINISHED                                                                  => [internal] load local bake definitions                                                      0.0s. => => reading from stdin 479B                                                                  0.0s. => [internal] load build definition from Dockerfile                                            0.0s. => => transferring dockerfile: 1.20kB                                                          0.0s. => resolve image config for docker-image://docker.io/docker/dockerfile:1                       7.1s. => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:dabfc0969b935b2080555ace70ee69a  0.0s. => [internal] load metadata for docker.io/library/python:3.7-alpine                            6.7s. => [internal] load .dockerignore                                                               0.0s. => => transferring context: 2B                                                                 0.0s. => [1/6] FROM docker.io/library/python:3.7-alpine@sha256:f3d31c8677d03f0b3c724446077f229a6ce9  5.4s. => => resolve docker.io/library/python:3.7-alpine@sha256:f3d31c8677d03f0b3c724446077f229a6ce9  0.0s. => => sha256:e6da3ee9bb64dd12b98fa609487f112fe1e365522e6e8345309db15c22a80a51 1.37kB / 1.37kB  0.0s. => => sha256:1bac8ae77e4af0b868b62a75115616a20e025e0451eeed05d94a4cfc4523e58a 6.87kB / 6.87kB  0.0s. => => sha256:f3d31c8677d03f0b3c724446077f229a6ce9d3ac430f5c08cd7dff00292048c3 1.65kB / 1.65kB  0.0s. => => sha256:9875af95546db78168a6761b7fa205ed1cd0c153cd89356c1512e551c12b 622.29kB / 622.29kB  1.1s. => => sha256:96526aa774ef0126ad0fe9e9a95764c5fc37f409ab9e97021e7b4775d82bf6fa 3.40MB / 3.40MB  1.5s. => => extracting sha256:96526aa774ef0126ad0fe9e9a95764c5fc37f409ab9e97021e7b4775d82bf6fa       0.2s. => => sha256:148762f75a1f92cc9857e9c488bf95d5aac61e9905ec47a7408025b2dd5c3b7a 240B / 240B      1.8s. => => sha256:ea1518237b3753b3fe40ee773d77651704178d9baa72ae5012e13a992cfa6c63 2.85MB / 2.85MB  2.4s=> => extracting sha256:9875af95546db78168a6761b7fa205ed1cd0c153cd89356c1512e551c12b2d5c       0.8s=> => sha256:4819c95424fc4a94767c9329b02238ebcce0bc682384cb671379bc1fb8a12b 10.94MB / 10.94MB  2.8s=> => extracting sha256:4819c95424fc4a94767c9329b02238ebcce0bc682384cb671379bc1fb8a12b55       1.2s. => => extracting sha256:148762f75a1f92cc9857e9c488bf95d5aac61e9905ec47a7408025b2dd5c3b7a       0.0s. => => extracting sha256:ea1518237b3753b3fe40ee773d77651704178d9baa72ae5012e13a992cfa6c63       0.8s. => [2/6] WORKDIR /code                                                                         0.2s. => [internal] load build context                                                               0.0s. => => transferring context: 2.80kB                                                             0.0s. => [3/7] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories      1.2s. => [4/7] RUN apk add --no-cache gcc musl-dev linux-headers                                    48.5s. => [5/7] COPY requirements.txt requirements.txt                                                0.0s. => [6/7] RUN pip install --no-cache-dir -r requirements.txt  # 添加--no-cache-dir减少�          158.4s> [7/7] COPY . /code                                                                          0.0s=> [7/7] COPY . /code                                                                          0.0s=> exporting to image                                                                          1.2s=> => exporting layers                                                                         1.1s=> => writing image sha256:93ad1bb045373ec50c0fb123f35a4fa33a98cf7127bfb8d23fb6f92e7a4d1655    0.0s=> => naming to docker.io/library/python-web-web                                               0.0s=> resolving provenance for metadata file                                                      0.0s
[+] Running 5/5✔ python-web-web                  Built                                                        0.0s ✔ Network python-web_default      Created                                                      0.1s ✔ Volume "python-web_redis_data"  Created                                                      0.0s ✔ Container python-web-redis-1    Created                                                      0.1s ✔ Container python-web-web-1      Created                                                      0.0s 
Attaching to redis-1, web-1
redis-1  | Starting Redis Server
redis-1  | 1:C 17 Sep 2025 13:58:42.993 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-1  | 1:C 17 Sep 2025 13:58:42.993 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1  | 1:C 17 Sep 2025 13:58:42.994 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
redis-1  | 1:C 17 Sep 2025 13:58:42.994 * Configuration loaded
redis-1  | 1:M 17 Sep 2025 13:58:42.994 * monotonic clock: POSIX clock_gettime
redis-1  | 1:M 17 Sep 2025 13:58:42.996 * Running mode=standalone, port=6379.
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> RedisBloom version 8.2.0 (Git=unknown)
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-error-rate       :      0.01 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-initial-size     :       100 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-expansion-factor :         2 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-bucket-size      :         2 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-initial-size     :      1024 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-max-iterations   :        20 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-expansion-factor :         1 }
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * <bf> { cf-max-expansions   :        32 }
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * <bf> ]
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> Redis version found by RedisSearch : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> Low level api version 1 initialized successfully
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> Initialized thread pools!
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> Disabled workers threadpool of size 0
redis-1  | 1:M 17 Sep 2025 13:58:43.013 * <search> Subscribe to config changes
redis-1  | 1:M 17 Sep 2025 13:58:43.013 * <search> Enabled role change notification
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * <search> Register write commands
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-compaction-policy   :              }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-num-threads         :            3 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-retention-policy    :            0 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-duplicate-policy    :        block }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-chunk-size-bytes    :         4096 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-encoding            :   compressed }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> { ts-ignore-max-time-diff:            0 }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> { ts-ignore-max-val-diff :     0.000000 }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> ]
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> Detected redis oss
redis-1  | 1:M 17 Sep 2025 13:58:43.018 * <timeseries> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 13:58:43.018 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
redis-1  | 1:M 17 Sep 2025 13:58:43.022 * <ReJSON> Created new data type 'ReJSON-RL'
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> version: 80200 git sha: unknown branch: unknown
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V1 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V2 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V3 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V4 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Initialized shared string cache, thread safe: false.
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <search> Acquired RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 13:58:43.028 * Server initialized
redis-1  | 1:M 17 Sep 2025 13:58:43.029 * Ready to accept connections tcp
web-1    |  * Serving Flask app 'app.py'
web-1    |  * Debug mode: off
web-1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1    |  * Running on all addresses (0.0.0.0)
web-1    |  * Running on http://127.0.0.1:5000
web-1    |  * Running on http://172.18.0.3:5000
web-1    | Press CTRL+C to quit
web-1    | 172.18.0.1 - - [17/Sep/2025 14:01:25] "GET / HTTP/1.1" 200 -
web-1    | 172.18.0.1 - - [17/Sep/2025 14:01:30] "GET / HTTP/1.1" 200 -

        2.切换到另一个窗口,访问网址查看返回的信息

Activate the web console with: systemctl enable --now cockpit.socketLast login: Wed Sep 17 20:26:23 2025 from 192.168.197.1
[root@host1 ~]# curl http://127.0.0.1:8000
你好!已访问1次。

        3.再次访问(访问次数增加)

[root@host1 ~]# curl http://127.0.0.1:8000
你好!已访问2次。

        4.列出本地镜像

[root@host1 ~]# docker inspect python-web-web-1
[{"Id": "bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d","Created": "2025-09-17T13:58:42.405306129Z","Path": "flask","Args": ["run"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 2135377,"ExitCode": 0,"Error": "","StartedAt": "2025-09-17T13:58:42.910452502Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:93ad1bb045373ec50c0fb123f35a4fa33a98cf7127bfb8d23fb6f92e7a4d1655","ResolvConfPath": "/var/lib/docker/containers/bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d/resolv.conf","HostnamePath": "/var/lib/docker/containers/bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d/hostname","HostsPath": "/var/lib/docker/containers/bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d/hosts","LogPath": "/var/lib/docker/containers/bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d/bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d-json.log","Name": "/python-web-web-1","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "python-web_default","PortBindings": {"5000/tcp": [{"HostIp": "","HostPort": "8000"}]},"RestartPolicy": {"Name": "always","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [0,0],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","Dns": null,"DnsOptions": null,"DnsSearch": null,"ExtraHosts": [],"GroupAdd": null,"IpcMode": "private","Cgroup": "","Links": null,"OomScoreAdj": 0,"PidMode": "","Privileged": false,"PublishAllPorts": false,"ReadonlyRootfs": false,"SecurityOpt": null,"UTSMode": "","UsernsMode": "","ShmSize": 67108864,"Runtime": "runc","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": null,"BlkioDeviceReadBps": null,"BlkioDeviceWriteBps": null,"BlkioDeviceReadIOps": null,"BlkioDeviceWriteIOps": null,"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": null,"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": null,"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "bdfd1d99f4c64c061f5964f02017925b1a65ba1472ded4ac50a86082a1ccc84d","LowerDir": "/var/lib/docker/overlay2/e509a8bda8871809daa389c0505d7bb68214b89fbf4867d4b304ea346090f8b2-init/diff:/var/lib/docker/overlay2/86h850pq51gpgjbain1aoppqh/diff:/var/lib/docker/overlay2/52rtl9j1nd19o9v2o6mwfe7q3/diff:/var/lib/docker/overlay2/1xnn8in9npul0sdfk5gt2wgbx/diff:/var/lib/docker/overlay2/nl7tl9w6woxo25oqm3f0kcost/diff:/var/lib/docker/overlay2/m748pgoqdvmnqhl7di2x8xbhe/diff:/var/lib/docker/overlay2/ipw476fdhhke32u4v1a7use2d/diff:/var/lib/docker/overlay2/f2a344fe1cc99a438fab8f5602bc0dcb86fb0aa82fda80ee40a1039f4d0fafa7/diff:/var/lib/docker/overlay2/18b534855aa85b0db1ff3d0e6a93cf60a12ccef4f6d650946d13ab519a0ad8a5/diff:/var/lib/docker/overlay2/e0c4bc8e675b7748f81df0c5d27aafd66cbd00f2828d0af3233e594c05bc6338/diff:/var/lib/docker/overlay2/695828d6ed58a86166abaa5c855fecf8f7dc669a33ad589646afad5126408d34/diff:/var/lib/docker/overlay2/aa944ec9fdee27314d69b114825bef040c8becff22e87466a71dd47f05c4e545/diff","MergedDir": "/var/lib/docker/overlay2/e509a8bda8871809daa389c0505d7bb68214b89fbf4867d4b304ea346090f8b2/merged","UpperDir": "/var/lib/docker/overlay2/e509a8bda8871809daa389c0505d7bb68214b89fbf4867d4b304ea346090f8b2/diff","WorkDir": "/var/lib/docker/overlay2/e509a8bda8871809daa389c0505d7bb68214b89fbf4867d4b304ea346090f8b2/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "bdfd1d99f4c6","Domainname": "","User": "","AttachStdin": false,"AttachStdout": true,"AttachStderr": true,"ExposedPorts": {"5000/tcp": {}},"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","LANG=C.UTF-8","GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D","PYTHON_VERSION=3.7.17","PYTHON_PIP_VERSION=23.0.1","PYTHON_SETUPTOOLS_VERSION=57.5.0","PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/9af82b715db434abb94a0a6f3569f43e72157346/public/get-pip.py","PYTHON_GET_PIP_SHA256=45a2bb8bf2bb5eff16fdd00faef6f29731831c7c59bd9fc2bf1f3bed511ff1fe","FLASK_APP=app.py","FLASK_RUN_HOST=0.0.0.0"],"Cmd": ["flask","run"],"Image": "python-web-web","Volumes": null,"WorkingDir": "/code","Entrypoint": null,"OnBuild": null,"Labels": {"com.docker.compose.config-hash": "355a478e5fb88463926dd63bff979fe609b6dcf99dbb1aa0c1197301d23ee134","com.docker.compose.container-number": "1","com.docker.compose.depends_on": "redis:service_started:false","com.docker.compose.image": "sha256:93ad1bb045373ec50c0fb123f35a4fa33a98cf7127bfb8d23fb6f92e7a4d1655","com.docker.compose.oneoff": "False","com.docker.compose.project": "python-web","com.docker.compose.project.config_files": "/root/python-web/compose.yaml","com.docker.compose.project.working_dir": "/root/python-web","com.docker.compose.service": "web","com.docker.compose.version": "2.39.2"}},"NetworkSettings": {"Bridge": "","SandboxID": "fd0bb67737ef9bff017b37f34b7cc65fb4ec4024655266ab827b53f678c8faaa","SandboxKey": "/var/run/docker/netns/fd0bb67737ef","Ports": {"5000/tcp": [{"HostIp": "0.0.0.0","HostPort": "8000"},{"HostIp": "::","HostPort": "8000"}]},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "","Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","MacAddress": "","Networks": {"python-web_default": {"IPAMConfig": null,"Links": null,"Aliases": ["python-web-web-1","web"],"MacAddress": "22:c0:20:3d:c2:ab","DriverOpts": null,"GwPriority": 0,"NetworkID": "fe2ff45e520f0d239bb027be95f3afb5df8dcd5206a723897e564d50d0f6a718","EndpointID": "a93deb522627df549c825ee56f35150cc0c4cecb3ad32a6673f0af2670f6fadf","Gateway": "172.18.0.1","IPAddress": "172.18.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": ["python-web-web-1","web","bdfd1d99f4c6"]}}}}
]

        5.停止应用程序

 => => extracting sha256:148762f75a1f92cc9857e9c488bf95d5aac61e9905ec47a7408025b2dd5c3b7a       0.0s. => => extracting sha256:ea1518237b3753b3fe40ee773d77651704178d9baa72ae5012e13a992cfa6c63       0.8s. => [2/6] WORKDIR /code                                                                         0.2s. => [internal] load build context                                                               0.0s. => => transferring context: 2.80kB                                                             0.0s. => [3/7] RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories      1.2s. => [4/7] RUN apk add --no-cache gcc musl-dev linux-headers                                    48.5s. => [5/7] COPY requirements.txt requirements.txt                                                0.0s. => [6/7] RUN pip install --no-cache-dir -r requirements.txt  # 添加--no-cache-dir减少�          158.4s> [7/7] COPY . /code                                                                          0.0s=> [7/7] COPY . /code                                                                          0.0s=> exporting to image                                                                          1.2s=> => exporting layers                                                                         1.1s=> => writing image sha256:93ad1bb045373ec50c0fb123f35a4fa33a98cf7127bfb8d23fb6f92e7a4d1655    0.0s=> => naming to docker.io/library/python-web-web                                               0.0s=> resolving provenance for metadata file                                                      0.0s
[+] Running 5/5✔ python-web-web                  Built                                                        0.0s ✔ Network python-web_default      Created                                                      0.1s ✔ Volume "python-web_redis_data"  Created                                                      0.0s ✔ Container python-web-redis-1    Created                                                      0.1s ✔ Container python-web-web-1      Created                                                      0.0s 
Attaching to redis-1, web-1
redis-1  | Starting Redis Server
redis-1  | 1:C 17 Sep 2025 13:58:42.993 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-1  | 1:C 17 Sep 2025 13:58:42.993 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1  | 1:C 17 Sep 2025 13:58:42.994 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
redis-1  | 1:C 17 Sep 2025 13:58:42.994 * Configuration loaded
redis-1  | 1:M 17 Sep 2025 13:58:42.994 * monotonic clock: POSIX clock_gettime
redis-1  | 1:M 17 Sep 2025 13:58:42.996 * Running mode=standalone, port=6379.
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> RedisBloom version 8.2.0 (Git=unknown)
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-error-rate       :      0.01 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-initial-size     :       100 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { bf-expansion-factor :         2 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-bucket-size      :         2 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-initial-size     :      1024 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-max-iterations   :        20 }
redis-1  | 1:M 17 Sep 2025 13:58:42.997 * <bf> { cf-expansion-factor :         1 }
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * <bf> { cf-max-expansions   :        32 }
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * <bf> ]
redis-1  | 1:M 17 Sep 2025 13:58:42.998 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> Redis version found by RedisSearch : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
redis-1  | 1:M 17 Sep 2025 13:58:43.011 * <search> Low level api version 1 initialized successfully
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> Initialized thread pools!
redis-1  | 1:M 17 Sep 2025 13:58:43.012 * <search> Disabled workers threadpool of size 0
redis-1  | 1:M 17 Sep 2025 13:58:43.013 * <search> Subscribe to config changes
redis-1  | 1:M 17 Sep 2025 13:58:43.013 * <search> Enabled role change notification
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * <search> Register write commands
redis-1  | 1:M 17 Sep 2025 13:58:43.014 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-compaction-policy   :              }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-num-threads         :            3 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-retention-policy    :            0 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-duplicate-policy    :        block }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-chunk-size-bytes    :         4096 }
redis-1  | 1:M 17 Sep 2025 13:58:43.016 * <timeseries> { ts-encoding            :   compressed }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> { ts-ignore-max-time-diff:            0 }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> { ts-ignore-max-val-diff :     0.000000 }
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> ]
redis-1  | 1:M 17 Sep 2025 13:58:43.017 * <timeseries> Detected redis oss
redis-1  | 1:M 17 Sep 2025 13:58:43.018 * <timeseries> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 13:58:43.018 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
redis-1  | 1:M 17 Sep 2025 13:58:43.022 * <ReJSON> Created new data type 'ReJSON-RL'
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> version: 80200 git sha: unknown branch: unknown
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V1 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V2 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V3 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V4 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Exported RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <ReJSON> Initialized shared string cache, thread safe: false.
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
redis-1  | 1:M 17 Sep 2025 13:58:43.023 * <search> Acquired RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 13:58:43.028 * Server initialized
redis-1  | 1:M 17 Sep 2025 13:58:43.029 * Ready to accept connections tcp
web-1    |  * Serving Flask app 'app.py'
web-1    |  * Debug mode: off
web-1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1    |  * Running on all addresses (0.0.0.0)
web-1    |  * Running on http://127.0.0.1:5000
web-1    |  * Running on http://172.18.0.3:5000
web-1    | Press CTRL+C to quit
web-1    | 172.18.0.1 - - [17/Sep/2025 14:01:25] "GET / HTTP/1.1" 200 -
web-1    | 172.18.0.1 - - [17/Sep/2025 14:01:30] "GET / HTTP/1.1" 200 -
Gracefully Stopping... press Ctrl+C again to forceContainer python-web-web-1  StoppingContainer python-web-web-1  StoppedContainer python-web-redis-1  Stopping
web-1 exited with code 137
redis-1  | 1:signal-handler (1758117908) Received SIGTERM scheduling shutdown...
redis-1  | 1:M 17 Sep 2025 14:05:08.421 * User requested shutdown...
redis-1  | 1:M 17 Sep 2025 14:05:08.421 * Saving the final RDB snapshot before exiting.
redis-1  | 1:M 17 Sep 2025 14:05:08.423 * DB saved on disk
redis-1  | 1:M 17 Sep 2025 14:05:08.424 # Redis is now ready to exit, bye bye...Container python-web-redis-1  Stopped

5.编辑 Compose 文件以添加绑定挂载

[root@host1 python-web]# vi compose.yaml
[root@host1 python-web]# cat compose.yaml
services:web:build: .  # 补全构建上下文,从当前目录的Dockerfile构建ports:- "8000:5000"  # 修复:端口号应为5000(Flask默认端口),并闭合引号volumes:- .:/code  # 修复:中文冒号改为英文冒号,实现主机目录与容器目录实时同步environment:FLASK_DEBUG: "true"  # 修复:变量名应为FLASK_DEBUG(下划线),中文冒号改为英文冒号depends_on:- redis  # 保持服务依赖,确保redis先启动restart: alwaysredis:image: "redis:alpine"  # 保持Redis镜像配置volumes:- redis_data:/data  # 数据持久化(可选)restart: alwaysvolumes:redis_data:  # 定义Redis数据卷

6.使用 Compose 重新构建并运行应用程序

[root@host1 python-web]# docker compose up
[+] Running 1/1✔ Container python-web-web-1  Recreated                                                        0.1s 
Attaching to redis-1, web-1
redis-1  | Starting Redis Server
redis-1  | 1:C 17 Sep 2025 14:14:35.307 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-1  | 1:C 17 Sep 2025 14:14:35.307 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-1  | 1:C 17 Sep 2025 14:14:35.307 * Redis version=8.2.1, bits=64, commit=00000000, modified=1, pid=1, just started
redis-1  | 1:C 17 Sep 2025 14:14:35.307 * Configuration loaded
redis-1  | 1:M 17 Sep 2025 14:14:35.308 * monotonic clock: POSIX clock_gettime
redis-1  | 1:M 17 Sep 2025 14:14:35.311 * Running mode=standalone, port=6379.
redis-1  | 1:M 17 Sep 2025 14:14:35.312 * <bf> RedisBloom version 8.2.0 (Git=unknown)
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { bf-error-rate       :      0.01 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { bf-initial-size     :       100 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { bf-expansion-factor :         2 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { cf-bucket-size      :         2 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { cf-initial-size     :      1024 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { cf-max-iterations   :        20 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { cf-expansion-factor :         1 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> { cf-max-expansions   :        32 }
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * <bf> ]
redis-1  | 1:M 17 Sep 2025 14:14:35.313 * Module 'bf' loaded from /usr/local/lib/redis/modules//redisbloom.so
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Redis version found by RedisSearch : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> RediSearch version 8.2.1 (Git=dba8dd0)
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Low level api version 1 initialized successfully
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> gc: ON, prefix min length: 2, min word length to stem: 4, prefix max expansions: 200, query timeout (ms): 500, timeout policy: return, cursor read size: 1000, cursor max idle (ms): 300000, max doctable size: 1000000, max number of search results:  1000000, 
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Initialized thread pools!
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Disabled workers threadpool of size 0
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Subscribe to config changes
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Enabled role change notification
redis-1  | 1:M 17 Sep 2025 14:14:35.321 * <search> Cluster configuration: AUTO partitions, type: 0, coordinator timeout: 0ms
redis-1  | 1:M 17 Sep 2025 14:14:35.322 * <search> Register write commands
redis-1  | 1:M 17 Sep 2025 14:14:35.322 * Module 'search' loaded from /usr/local/lib/redis/modules//redisearch.so
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> RedisTimeSeries version 80200, git_sha=1439d4a439ca9c063e6ef124a510abff09a5d493
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> Redis version found by RedisTimeSeries : 8.2.1 - oss
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> Registering configuration options: [
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-compaction-policy   :              }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-num-threads         :            3 }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-retention-policy    :            0 }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-duplicate-policy    :        block }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-chunk-size-bytes    :         4096 }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-encoding            :   compressed }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-ignore-max-time-diff:            0 }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> { ts-ignore-max-val-diff :     0.000000 }
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> ]
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> Detected redis oss
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * <timeseries> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 14:14:35.323 * Module 'timeseries' loaded from /usr/local/lib/redis/modules//redistimeseries.so
redis-1  | 1:M 17 Sep 2025 14:14:35.324 * <ReJSON> Created new data type 'ReJSON-RL'
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> version: 80200 git sha: unknown branch: unknown
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Exported RedisJSON_V1 API
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Exported RedisJSON_V2 API
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Exported RedisJSON_V3 API
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Exported RedisJSON_V4 API
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Exported RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Enabled diskless replication
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <ReJSON> Initialized shared string cache, thread safe: false.
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * Module 'ReJSON' loaded from /usr/local/lib/redis/modules//rejson.so
redis-1  | 1:M 17 Sep 2025 14:14:35.325 * <search> Acquired RedisJSON_V5 API
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * Server initialized
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * <search> Loading event starts
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * <search> Enabled workers threadpool of size 4
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * Loading RDB produced by version 8.2.1
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * RDB age 567 seconds
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * RDB memory usage when created 0.99 Mb
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * Done loading RDB, keys loaded: 1, keys expired: 0.
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * <search> Disabled workers threadpool of size 4
redis-1  | 1:M 17 Sep 2025 14:14:35.326 * <search> Loading event ends
redis-1  | 1:M 17 Sep 2025 14:14:35.327 * DB loaded from disk: 0.001 seconds
redis-1  | 1:M 17 Sep 2025 14:14:35.327 * Ready to accept connections tcp
web-1    |  * Serving Flask app 'app.py'
web-1    |  * Debug mode: on
web-1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1    |  * Running on all addresses (0.0.0.0)
web-1    |  * Running on http://127.0.0.1:5000
web-1    |  * Running on http://172.18.0.3:5000
web-1    | Press CTRL+C to quit
web-1    |  * Restarting with stat
web-1    |  * Debugger is active!
web-1    |  * Debugger PIN: 497-745-887
web-1    | 172.18.0.1 - - [17/Sep/2025 14:14:47] "GET / HTTP/1.1" 200 -
[root@host1 ~]# curl http://127.0.0.1:8000
你好!已访问3次。

7.升级应用程序

        1.更改 app.py 文件中的问候语并保存

[root@host1 python-web]# vi app.py
[root@host1 python-web]# cat app.py
import time
import redis
from flask import Flaskapp = Flask(__name__)cache = redis.Redis(host='redis', port=6379)def get_hit_count():retries = 5while True:try:return cache.incr('hits')except redis.exceptions.ConnectionError as exc:if retries == 0:raise excretries -= 1time.sleep(0.5)@app.route('/')
def hello():count = get_hit_count()return '欢迎测试 Compose 功能!已访问{}次。\n'.format(count)if __name__ == '__main__':app.run(host='0.0.0.0', port=5000)

        2.访问该应用程序

dis-1  | 1:M 17 Sep 2025 14:18:38.183 * Loading RDB produced by version 8.2.1
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * RDB age 168 seconds
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * RDB memory usage when created 0.99 Mb
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * Done loading RDB, keys loaded: 1, keys expired: 0.
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * <search> Disabled workers threadpool of size 4
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * <search> Loading event ends
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * DB loaded from disk: 0.000 seconds
redis-1  | 1:M 17 Sep 2025 14:18:38.183 * Ready to accept connections tcp
web-1    |  * Serving Flask app 'app.py'
web-1    |  * Debug mode: on
web-1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web-1    |  * Running on all addresses (0.0.0.0)
web-1    |  * Running on http://127.0.0.1:5000
web-1    |  * Running on http://172.18.0.3:5000
web-1    | Press CTRL+C to quit
web-1    |  * Restarting with stat
web-1    |  * Debugger is active!
web-1    |  * Debugger PIN: 104-510-904
web-1    | 172.18.0.1 - - [17/Sep/2025 14:18:48] "GET / HTTP/1.1" 200 -
[root@host1 ~]# curl http://127.0.0.1:8000
欢迎测试 Compose 功能!已访问4次。

        3.停止应用程序

web-1    |  * Debugger is active!
web-1    |  * Debugger PIN: 104-510-904
web-1    | 172.18.0.1 - - [17/Sep/2025 14:18:48] "GET / HTTP/1.1" 200 -
Gracefully Stopping... press Ctrl+C again to forceContainer python-web-web-1  StoppingContainer python-web-web-1  StoppedContainer python-web-redis-1  Stopping
web-1 exited with code 0
redis-1  | 1:signal-handler (1758118775) Received SIGTERM scheduling shutdown...
redis-1  | 1:M 17 Sep 2025 14:19:35.826 * User requested shutdown...
redis-1  | 1:M 17 Sep 2025 14:19:35.826 * Saving the final RDB snapshot before exiting.
redis-1  | 1:M 17 Sep 2025 14:19:35.831 * DB saved on disk
redis-1  | 1:M 17 Sep 2025 14:19:35.831 # Redis is now ready to exit, bye bye...Container python-web-redis-1  Stopped

8.试用其他 docker compose 命令

        1.在后台运行服务

[root@host1 python-web]# docker compose up -d
[+] Running 2/2✔ Container python-web-redis-1  Started                                                        0.4s ✔ Container python-web-web-1    Started                                                        0.5s

        2.查看当前正在运行的服务

[root@host1 python-web]# docker compose ps
NAME                 IMAGE            COMMAND                   SERVICE   CREATED          STATUS          PORTS
python-web-redis-1   redis:alpine     "docker-entrypoint.s…"   redis     22 minutes ago   Up 30 seconds   6379/tcp
python-web-web-1     python-web-web   "flask run"               web       6 minutes ago    Up 29 seconds   0.0.0.0:8000->5000/tcp, [::]:8000->5000/tcp

        3.查看 web 服务的环境变量

[root@host1 python-web]# docker compose run web env
[+] Creating 1/1✔ Container python-web-redis-1  Running                                                        0.0s 
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=c997007712f7
TERM=xterm
FLASK_DEBUG=true
LANG=C.UTF-8
GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
PYTHON_VERSION=3.7.17
PYTHON_PIP_VERSION=23.0.1
PYTHON_SETUPTOOLS_VERSION=57.5.0
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/9af82b715db434abb94a0a6f3569f43e72157346/public/get-pip.py
PYTHON_GET_PIP_SHA256=45a2bb8bf2bb5eff16fdd00faef6f29731831c7c59bd9fc2bf1f3bed511ff1fe
FLASK_APP=app.py
FLASK_RUN_HOST=0.0.0.0
HOME=/root

        4.停止应用程序,完全删除容器以及卷

[root@host1 python-web]# docker compose down --volumes
[+] Running 4/4✔ Container python-web-web-1    Removed                                                        0.4s ✔ Container python-web-redis-1  Removed                                                        0.3s ✔ Volume python-web_redis_data  Removed                                                        0.0s ✔ Network python-web_default    Removed                                                        0.2s

        至此,完成了构建、部署和管理一个应用程序的全过程示范!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/97919.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/97919.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Flutter-[2]第一个应用

摘要 根据官方文档搭配好环境&#xff0c;使用vscode创建完应用后&#xff0c;会遇到以下问题 设备无法选择打开了lib\main.dart右上角也没有运行按钮 环境 Windows11Flutter 3.35.4 必要设置 1. 查看是否开启Windows桌面应用开发flutter config --list输出如下: All Settings:…

QListWidget选择阻止问题解决方案

QListWidget选择阻止问题解决方案QListWidget选择阻止问题解决方案问题背景QListWidget工作机制详解1. 事件处理流程2. 关键机制说明2.1 鼠标事件与信号的分离2.2 信号阻塞的局限性2.3 断开连接方法的问题问题的根本原因1. 异步事件处理2. 多层状态管理3. 事件优先级解决方案演…

TCL华星计划投建第8.6代印刷OLED产线

近日&#xff0c;TCL科技集团股份有限公司&#xff08;000100.SZ&#xff09;发布公告&#xff0c;公司、旗下子公司TCL华星与广州市人民政府、广州经济技术开发区管理委员会共同签署项目合作协议&#xff0c;拟共同出资于广州市建设一条月加工2290mm2620mm玻璃基板能力约2.25万…

MATLAB 时间序列小波周期分析

1. 文件结构 WaveletPeriod/ ├── main_wavelet_period.m % 一键运行 ├── wavelet_power_spectrum.m % 小波功率谱 显著性 ├── period_peak_detect.m % 自动周期峰值 ├── plot_wavelet_results.m % 时频图 周期图 └── example/└── temp.csv …

如何精准配置储

当电费账单变身利润引擎&#xff0c;您的企业是否做好了准备&#xff1f;鹧鸪云储能仿真软件&#xff0c;不止于仿真——我们以智能算法为核心&#xff0c;为企业定制“高收益、高适配、可持续”的储能配置方案&#xff0c;将用电数据转化为新一轮增长动能。智慧大脑&#xff1…

Uniapp崩溃监控体系构建:内存泄漏三维定位法(堆栈/资源/线程)

在Uniapp开发中&#xff0c;内存泄漏是导致应用崩溃的核心隐患。通过堆栈分析、资源追踪和线程监控三维定位法&#xff0c;可系统化定位泄漏源。以下是完整实施方案&#xff1a;一、堆栈维度&#xff1a;泄漏对象溯源内存快照比对使用Chrome DevTools定期获取内存快照&#xff…

NLP中Subword算法:WordPiece、BPE、BBPE、SentencePiece详解以及代码实现

本文将介绍以下内容&#xff1a; 1. Subword与传统tokenization技术的对比2. WordPiece3. Byte Pair Encoding (BPE)4. Byte-level BPE(BBPE)5. SentencePiece 以及各Subword算法代码实现 一、Subword与传统tokenization技术的对比 1. 传统tokenization技术 传统tokenizatio…

十一章 无界面压测

一、采用无界面压测的原因1.节约系统资源。 2.更快捷&#xff0c;只需要启动命令即可进行压测 3.主要是用于性能压测集成.无界面压测命令参数&#xff1a; -n 表示无界面压测 -t 制定你的 jmx 脚本 -l 生成 jtl 测试报告二、注意配置文件设置:输出为xml jmeter.save.s…

从零实现 Qiankun 微前端:基座应用控制子应用路由与信息交互

随着前端业务的快速发展,单体应用模式(Monolith)越来越难以支撑复杂业务场景。微前端(Micro Frontends)应运而生,它将大型应用拆解成多个子应用(Micro App),通过主应用进行统一调度和集成。 在微前端技术栈中,Qiankun(乾坤)是一个广泛使用的解决方案,基于 single…

在业务应用中集成 go-commons,实现应用+系统双指标监控

在日常 Go 服务开发中&#xff0c;我们通常需要同时监控 业务指标&#xff08;比如 QPS、请求延迟、错误率&#xff09;&#xff0c;也需要关注 系统指标&#xff08;CPU、内存、磁盘占用情况&#xff09;。 过去这类场景通常要引入多个库&#xff1a;一个负责业务指标采集&…

容器化部署番外篇之docker网络通信06

一、四种网络模式 Bridge模式&#xff1a;容器的默认网关&#xff0c;默认新建容器的网络模式Host模式&#xff1a;容器和宿主机共用一个 Network&#xff0c;使用主机的IP:PORT就可以访问容器&#xff0c;但安全性不高&#xff0c;用得少Container模式&#xff1a;这个模式指定…

Linux 线程的概念

序言&#xff1a; 在这篇博客中我们将讲解线程的概念&#xff0c;如何理解线程&#xff0c;线程和进程的区别&#xff0c;线程的优缺点等&#xff0c;我相信你看完这篇博客后会以别样的视角重新理解线程&#xff0c;下面的内容全部是基于Linux操作系统的。 一、线程的概念 1…

vscode 中通义灵码显示登录过期

本文主要分享&#xff1a;vscode 中通义灵码显示登录过期的解决办法。vscode 中的小插件通义灵码&#xff0c;用的好好的&#xff0c;突然提示&#xff1a;登录过期&#xff0c;尝试访问网页版阿里云&#xff0c;登录后&#xff0c;关闭 vscode 重新打开&#xff0c;通义灵码还…

ESP32C3-MINI-1开发板踩坑记录

某东买了一个ESP32C3-MINI-1开发板&#xff0c;名字跟ESP官网的很像&#xff0c;想着应该差不多的&#xff0c;价格便宜17块&#xff0c;而官网的就贵了60还不包邮&#xff0c;买来才发现是巨坑。 看结论&#xff0c;直接到最后&#xff0c;前面都是我的踩坑过程。第一块板子发…

基于粒子群算法的山地环境无人机最短路径规划研究(含危险区域约束的三维优化方法)

无人机在复杂地形与危险环境中的自主路径规划是保障任务顺利执行的关键问题。本文针对山地环境下单无人机三维路径规划难题&#xff0c;提出了一种基于粒子群算法&#xff08;PSO&#xff09;的优化方法。首先&#xff0c;建立了包含真实地形高程、危险区域和飞行约束条件的三维…

Linux-> UDP 编程2

目录 本文说明 一&#xff1a;字典程序的几个问题 1&#xff1a;字典的本质 2&#xff1a;翻译功能的本质 3&#xff1a;让服务端和翻译功能相关联 二&#xff1a;字典类(Dict.hpp) 1&#xff1a;加载词典(Load) 2&#xff1a;翻译单词(Translate) 三&#xff1a;服务…

辉视养老方案:重塑老年生活的温馨与安心

在当今社会&#xff0c;随着老龄化进程的加速&#xff0c;如何为老年人提供更加便捷、舒适且安全的养老环境&#xff0c;成为了全社会共同关注的焦点。辉视养老方案应运而生&#xff0c;它以科技为翼&#xff0c;以关爱为心&#xff0c;通过远程探望、客控系统、信息服务、IPTV…

SQuAD:机器阅读理解领域的里程碑数据集

本文由「大千AI助手」原创发布&#xff0c;专注用真话讲AI&#xff0c;回归技术本质。拒绝神话或妖魔化。搜索「大千AI助手」关注我&#xff0c;一起撕掉过度包装&#xff0c;学习真实的AI技术&#xff01; 1 什么是SQuAD&#xff1f; SQuAD&#xff08;Stanford Question Ans…

【vim,Svelte】怎样使用 vim 编辑 Svelte 那些奇奇怪怪名字的文件?

当你要使用 vim&#xff08;或者neovim&#xff09;来编辑 Svelte 下面的文件时&#xff0c;比如这些文件&#xff1a; page.svelte layout.svelte$ vim page.svelte $ vim "page.svelte" $ vim page.svelte $ vim \page.svelte使用上面的命令&#xff0c;你会遇到这…

深入解析 HTTP 状态码

在日常的网络浏览和 Web 开发过程中&#xff0c;我们总会不可避免地遇到各种 HTTP 状态码。比如常见的 “404 Not Found”&#xff0c;它意味着我们所请求的页面不存在&#xff1b;还有 “500 Internal Server Error”&#xff0c;表示服务器端出现了错误。这些由三位数字组成的…