Top

Docker-Compose

Docker-compose的使用

我的环境是Centos7.4!3台服务器:Server

172.16.255.116-www1
↓简介
1.Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排. 2.Docker-Compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container).

↓原文出自

docker-compose入门:https://docs.docker.com/compose/gettingstarted/
Docker快速入门:https://blog.51cto.com/9291927/2310444

1. Compose和Docker兼容性.

历史版本:https://github.com/docker/compose/releases

无法显示

2. 下载docker-compose文件.

[root@www1 ~]# wget https://github.com/docker/compose/releases/download/1.24.0/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose
[root@www1 ~]# chmod +x /usr/local/bin/docker-compose
[root@www1 web]# docker-compose --version
docker-compose version 1.24.0, build 0aa59064

3. Compose-小实验.

3.1 创建实验目录.

↓创建一个测试目录.
[root@www1 ~]# mkdir /tmp/compose && cd /tmp/compose
↓创建web使用的目录.
[root@www1 compose]# mkdir web/src/ -p

3.2 编辑容器使用的脚本和文件.

↓创建一个python脚本.
[root@www1 compose]# vim web/src/app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
def hello():
count = redis.incr('hits')
return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
↓编辑需要安装的包.
[root@www1 compose]# vim web/src/requirements.txt
1
2
flask
redis

3.3 创建Dockerfile和docker-compose.

[root@www1 compose]# vim web/Dockerfile
1
2
3
4
5
6
7
8
9
10
#拉取一个python容器.
FROM python:3.4-alpine
#将src目录拷贝到容器的/src中.
ADD ./src /src
#指定工作目录.
WORKDIR /src
#容器中运行pip命令安装flask和redis.
RUN pip install -r requirements.txt
#启动容器时执行python app.py.
CMD ["python","app.py"]
[root@www1 compose]# vim docker-compose.yml
1
2
3
4
5
6
7
8
version: '3.7'
services: #下面定义了两个服务(web|redis).
web:
build: ./web/ #WEB的Dockerfile的路径.
ports: #映射端口5000到宿主机.
- "5000:5000"
redis:
image: "redis:alpine" #运行一个redis:alpine容器.
[root@www1 compose]# docker-compose -f docker-compose.yml up Creating compose_redis_1 ... done Creating compose_web_1 ... done web_1 | * Serving Flask app "app" (lazy loading) web_1 | * Environment: production web_1 | WARNING: Do not use the development server in a production environment. web_1 | Use a production WSGI server instead. web_1 | * Debug mode: on web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) web_1 | * Restarting with stat web_1 | * Debugger is active! web_1 | * Debugger PIN: 119-539-681

3.4 测试.

[root@www1 ~]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@www1 ~]# curl localhost:5000
Hello World! I have been seen 2 times.
[root@www1 ~]# curl localhost:5000
Hello World! I have been seen 3 times.
[root@www1 ~]# curl localhost:5000
Hello World! I have been seen 4 times.

4. 部分命令总结.

docker-compose up -d               #在后台启动docker-compose.yml.
docker-compose ps                  #查看当前运行的容器.
docker-compose stop                #停止docker-compose.yml.
docker-compose start               #恢复停止的docker-compose.yml.
docker-compose down --volumes      #删除docker-compose.yml生成的容器(--volumes还删除Redis容器使用的数据卷).