docker-compose是定义和运行多容器Docker应用程序的工具。我们可以把特定的几个容器组合起来构建成一个应用向下图一样。看上去有点像我们以前用过的saltstack的使用方式一样,同样compose使用yaml语法,通过这种方式我们可以把就像单机版本(docker版)saltstack.compose也可以yml文件为管理单位进行项目管理。
现在大家是不是很感兴趣啊。
composer安装
yum-config-manager --add-repo https://docs.docker.com/engine/installation/linux/repo_files/centos/docker.repo yum -y install docker-engine centos7下面直接用pip install docker-compose
$ mkdir composetest $ cd composetest $ cat app.py 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@salt-node1 composetest]# cat requirements.txt flask redis
创建一个dockerfile
依赖于 python3.4添加本地文件到容器,工作目录当前目录下的code,安装flask和redis‘插件,启动flask的一个小app
[root@salt-node1 composetest]# cat Dockerfile FROM python:3.4-alpine ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD ["python", "app.py"]
本services总需要两个镜像分别是当前Dockerfile编译出的镜像和远程redis镜像,
web当前目录下构建出的镜像web自动映射端口月5000,挂载当前目录到镜像内的code目录
redis:直接拉取远程镜像并启动
注意:因为docker非默认网络可以自动解析容器名到对应容器,所以直接使用“redis”就可以访问到启动的redis容器。
version: ‘2‘ services: web: build: . ports: - "5000:5000" volumes: - .:/code redis: image: "registry.cn-hangzhou.aliyuncs.com/forker/redis:latest"
整个启动过程包括镜像构建,拉取,启动。
[root@salt-node1 composetest]# docker-compose up Creating network "composetest_default" with the default driver Building web Step 1/5 : FROM python:3.4-alpine ---> 765c483d587c 。。。。。 redis_1 | 1:M 26 Jan 14:25:40.111 * The server is now ready to accept connections on port 6379 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 code: 301-431-697
现在可以测试访问一下,因为上面flask的脚本每次访问都会写入redis一次
[root@salt-node1 ~]# curl http://0.0.0.0:5000/ Hello World! I have been seen 1 times. [root@salt-node1 ~]# curl http://0.0.0.0:5000/ Hello World! I have been seen 2 times. [root@salt-node1 ~]# curl http://0.0.0.0:5000/ Hello World! I have been seen 3 times. [root@salt-node1 ~]# curl http://0.0.0.0:5000/ Hello World! I have been seen 4 times.
现在修改下代码在app.py里面修改返回“return ‘Hello from Docker! I have been seen {} times.\n‘.format(count)”
[root@salt-node1 composetest]# vim app.py [root@salt-node1 composetest]# curl http://0.0.0.0:5000/ Hello from Docker! I have been seen 5 times. [root@salt-node1 composetest]# curl http://0.0.0.0:5000/ Hello from Docker! I have been seen 6 times. [root@salt-node1 composetest]# curl http://0.0.0.0:5000/ Hello from Docker! I have been seen 7 times.
[root@salt-node1 composetest]# docker-compose up -d Recreating composetest_web_1 Starting composetest_redis_1 [root@salt-node1 composetest]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------- composetest_redis_1 /bin/sh -c /usr/local/redi ... Up 6379/tcp composetest_web_1 python app.py Up 0.0.0.0:5000->5000/tcp
查看指定容器的环境变量
[root@salt-node1 composetest]# docker-compose run web env PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=230491939a67 TERM=xterm LANG=C.UTF-8 GPG_KEY=97FC712E4C024BBEA48A61ED3A5CA953F73C700D PYTHON_VERSION=3.4.6 PYTHON_PIP_VERSION=9.0.1 HOME=/root
停止服务
[root@salt-node1 composetest]# docker-compose stop Stopping composetest_web_1 ... done Stopping composetest_redis_1 ... done
删除容器,逻辑卷和网络
[root@salt-node1 composetest]# docker-compose down --volumes Removing composetest_web_run_1 ... done Removing composetest_web_1 ... done Removing composetest_redis_1 ... done Removing network composetest_defau
lt
本文出自 “运维之我的历程” 博客,转载请与作者联系!
原文地址:http://nginxs.blog.51cto.com/4676810/1894396