使用
和docker 的Dockerfile类似文件,docker-compose使用 YAML 文件对容器进行管理。
术语
首先介绍几个术语。
服务 (
service
):一个应用容器,实际上可以运行多个相同镜像的实例。项目 (
project
):由一组关联的应用容器组成的一个完整业务单元。
可见,一个项目可以由多个服务(容器)关联而成,Compose
面向项目进行管理。
YAML 文件格式
YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写。使用空白,缩进,分行组织数据,从而使得表示更加简洁易读.
- 大小写敏感
- 缩进表示层级关系
- 缩进空格数不重要,相同层级左侧对齐即可。(不允许使用 tab 缩进!)
- 连续的项目(如:数组元素、集合元素)通过减号“-”来表示
- map结构里面的键值对(key/value)用冒号“:”来分割
- 使用#表示注释
场景
最常见的项目是 web 网站,该项目应该包含 web 应用和缓存。
下面我们用 Python
来建立一个能够记录页面访问次数的 web 网站。
web 应用
新建文件夹,在该目录中编写 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)
Dockerfile
编写 Dockerfile
文件,内容为
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
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
COPY requirements.txt requirements.txt
RUN pip install -U pip
RUN pip config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip config set install.trusted-host mirrors.aliyun.com
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
创建文件requirements.txt
flask
redis
docker-compose.yml
编写 docker-compose.yml
文件,这个是 Compose 使用的主模板文件。
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
运行 compose 项目
$ docker-compose up
Recreating composetest_web_1 ... done
Starting composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 | 1:C 18 May 2020 08:47:26.039 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 18 May 2020 08:47:26.039 # Redis version=6.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 18 May 2020 08:47:26.039 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 18 May 2020 08:47:26.041 * Running mode=standalone, port=6379.
redis_1 | 1:M 18 May 2020 08:47:26.041 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 18 May 2020 08:47:26.041 # Server initialized
redis_1 | 1:M 18 May 2020 08:47:26.041 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 18 May 2020 08:47:26.042 * Loading RDB produced by version 6.0.2
redis_1 | 1:M 18 May 2020 08:47:26.042 * RDB age 27 seconds
redis_1 | 1:M 18 May 2020 08:47:26.042 * RDB memory usage when created 0.77 Mb
redis_1 | 1:M 18 May 2020 08:47:26.042 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 18 May 2020 08:47:26.042 * Ready to accept connections
web_1 | * Serving Flask app "app.py"
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: off
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
此时访问本地 5000
端口,每次刷新页面,计数就会加 1。
添加挂载,重跑 compose
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
volumes:将当前目录挂载到/code里面 environment: 设置flask以development模式运行