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