從源代碼開始構建、部署和管理應用程序

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
英文地址,請注明出處:http://en.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;表示服務器端出現了錯誤。這些由三位數字組成的…