![关注抖音创业](https://img.kancloud.cn/c5/ce/c5ced2ea291122851697114ca415ff95_1125x1680.png) [TOC] # 案例 > 通过redis记录点击数,官网: https://docs.docker.com/compose/gettingstarted/ (1)创建文件夹 ``` mkdir composetest cd composetest ``` (2)创建业务脚本 vi app.py ``` import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return 'Hello World! I have been seen {} times.\n'.format(count) ``` (3)在同目录下创建 requirements.txt写入以下内容 ``` flask redis ``` (4)创建 Dockerfile ``` FROM python:3.7-alpine WORKDIR /code ENV FLASK_APP app.py ENV FLASK_RUN_HOST 0.0.0.0 RUN apk add --no-cache gcc musl-dev linux-headers COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["flask", "run"] ``` (5) 编写 `docker-compose.yml` ``` version: '3' services: web: build: . ports: - "5000:5000" networks: - app-net redis: image: "redis:alpine" networks: - app-net networks: app-net: driver: bridge ``` (6)通过docker-compose创建容器 `docker-compose up -d` 过程 ``` Creating network "composetest_app-net" with driver "bridge" Building web Step 1/9 : FROM python:3.7-alpine 3.7-alpine: Pulling from library/python c9b1b535fdd9: Pull complete 2cc5ad85d9ab: Pull complete 53a2fca3c2ea: Pull complete 30fce49de8b1: Pull complete ca406aaf66e0: Pull complete Digest: sha256:4e91ff3eed8bdd013178b6d8e55f656b394aeea42945f04389e4af5462e7cb6d Status: Downloaded newer image for python:3.7-alpine ---> a5d195bb2a63 Step 2/9 : WORKDIR /code ---> Running in ea0ca57eacba Removing intermediate container ea0ca57eacba ---> 67903e86148c Step 3/9 : ENV FLASK_APP app.py ---> Running in 0a5a9b5c7b29 Removing intermediate container 0a5a9b5c7b29 ---> 51279a538141 Step 4/9 : ENV FLASK_RUN_HOST 0.0.0.0 ---> Running in 4859db968894 Removing intermediate container 4859db968894 ---> 54e7d97587df Step 5/9 : RUN apk add --no-cache gcc musl-dev linux-headers ---> Running in 99d13fe00994 fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz (1/12) Installing libgcc (9.2.0-r3) (2/12) Installing libstdc++ (9.2.0-r3) (3/12) Installing binutils (2.33.1-r0) (4/12) Installing gmp (6.1.2-r1) (5/12) Installing isl (0.18-r0) (6/12) Installing libgomp (9.2.0-r3) (7/12) Installing libatomic (9.2.0-r3) (8/12) Installing mpfr4 (4.0.2-r1) (9/12) Installing mpc1 (1.1.0-r1) (10/12) Installing gcc (9.2.0-r3) ... ``` (7)访问 `curl 192.168.136.200:5000` (8)docker-compolse.yml 详解 > (1)version: '3' ``` 表示docker-compose的版本 ``` > (2)services ``` 一个service表示一个container ``` > (3)networks ``` 相当于docker network create app-net ``` > (4)volumes ``` 相当于-v v1:/var/lib/mysql ``` > (5)image ``` 表示使用哪个镜像,本地build则用build,远端则用image ``` > (6)ports ``` 相当于-p 8080:8080 ``` > (7)environment ``` 相当于-e ``` # scale扩缩容 ## docker-compolse.yml文件中的ports去掉 ``` version: '3' services: web: build: . networks: - app-net redis: image: "redis:alpine" networks: - app-net networks: app-net: driver: bridge ``` ## 创建service `docker-compose up -d . ` ## 对python容器进行扩缩容 ``` docker-compose up --scale web=3 -d ``` > 检查 ``` docker-compose ps docker-compose logs web ```