2019-11-05 23:35:43 +08:00
|
|
|
|
# 使用
|
2015-05-08 20:10:01 +08:00
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
## 术语
|
2017-12-10 11:52:09 +08:00
|
|
|
|
|
2015-05-08 20:10:01 +08:00
|
|
|
|
首先介绍几个术语。
|
|
|
|
|
|
2017-11-26 09:54:04 +08:00
|
|
|
|
* 服务 (`service`):一个应用容器,实际上可以运行多个相同镜像的实例。
|
2017-11-23 15:48:52 +08:00
|
|
|
|
|
2017-11-26 09:54:04 +08:00
|
|
|
|
* 项目 (`project`):由一组关联的应用容器组成的一个完整业务单元。
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-11-26 09:54:04 +08:00
|
|
|
|
可见,一个项目可以由多个服务(容器)关联而成,`Compose` 面向项目进行管理。
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
## 场景
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
最常见的项目是 web 网站,该项目应该包含 web 应用和缓存。
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
下面我们用 `Python` 来建立一个能够记录页面访问次数的 web 网站。
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
### web 应用
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
新建文件夹,在该目录中编写 `app.py` 文件
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-11-13 18:54:38 +08:00
|
|
|
|
```python
|
2017-12-10 11:52:09 +08:00
|
|
|
|
from flask import Flask
|
|
|
|
|
from redis import Redis
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
redis = Redis(host='redis', port=6379)
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def hello():
|
|
|
|
|
count = redis.incr('hits')
|
|
|
|
|
return 'Hello World! 该页面已被访问 {} 次。\n'.format(count)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app.run(host="0.0.0.0", debug=True)
|
2015-05-11 14:46:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
### Dockerfile
|
2017-12-10 11:52:09 +08:00
|
|
|
|
|
2017-11-01 00:20:30 +08:00
|
|
|
|
编写 `Dockerfile` 文件,内容为
|
2017-12-10 11:52:09 +08:00
|
|
|
|
|
2019-10-29 14:31:45 +08:00
|
|
|
|
```docker
|
2017-12-10 11:52:09 +08:00
|
|
|
|
FROM python:3.6-alpine
|
2015-05-11 14:46:51 +08:00
|
|
|
|
ADD . /code
|
2017-12-10 11:52:09 +08:00
|
|
|
|
WORKDIR /code
|
|
|
|
|
RUN pip install redis flask
|
|
|
|
|
CMD ["python", "app.py"]
|
2015-05-11 14:46:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
### docker-compose.yml
|
2017-12-10 11:52:09 +08:00
|
|
|
|
|
|
|
|
|
编写 `docker-compose.yml` 文件,这个是 Compose 使用的主模板文件。
|
2015-05-11 14:46:51 +08:00
|
|
|
|
|
2017-11-01 00:20:30 +08:00
|
|
|
|
```yaml
|
2017-12-10 11:52:09 +08:00
|
|
|
|
version: '3'
|
2017-11-01 00:20:30 +08:00
|
|
|
|
services:
|
2017-11-23 15:48:52 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
web:
|
|
|
|
|
build: .
|
2015-05-11 14:46:51 +08:00
|
|
|
|
ports:
|
2017-12-10 11:52:09 +08:00
|
|
|
|
- "5000:5000"
|
2018-12-31 14:50:01 +08:00
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
redis:
|
|
|
|
|
image: "redis:alpine"
|
2015-05-11 14:46:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-05 23:35:43 +08:00
|
|
|
|
### 运行 compose 项目
|
2017-12-10 11:52:09 +08:00
|
|
|
|
|
2017-11-13 18:54:38 +08:00
|
|
|
|
```bash
|
|
|
|
|
$ docker-compose up
|
2015-05-11 14:46:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2017-12-10 11:52:09 +08:00
|
|
|
|
此时访问本地 `5000` 端口,每次刷新页面,计数就会加 1。
|