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
至此,完成了构建、部署和管理一个应用程序的全过程示范!