文章目录
概述
Compose
项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。从功能上看,跟
OpenStack
中的
Heat
十分类似。
其代码目前在
https://github.com/docker/compose
上开源。
通过前面文章的介绍,我们知道使用一个
Dockerfile
模板文件,可以让用户很方便的定义一个单独的应用容器。然而,在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web 项目,除了 Web 服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
Docker Compose介绍
通过 Docker-Compose 用户可以很容易地用一个配置文件定义一个多容器的应用,然后使用一条指令安装这个应用的所有依赖,完成构建。Docker-Compose 解决了容器与容器之间如何管理编排的问题。
Docker Compose 工作原理图
Compose
中有两个重要的概念:
- 服务 (
service
):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。 - 项目 (
project
):由一组关联的应用容器组成的一个完整业务单元,在docker-compose.yml
文件中定义。
Compose
的默认管理对象是项目,通过子命令对项目中的一组容器进行便捷地生命周期管理。
Compose
项目由 Python 编写,实现上调用了 Docker 服务提供的 API 来对容器进行管理。因此,只要所操作的平台支持 Docker API,就可以在其上利用
Compose
来进行编排管理。
Docker Compose安装
一、下载Docker Compose:
Docker Compose 存放在Git Hub,不太稳定。 你可以也通过执行下面的命令,高速安装Docker Compose。
执行命令
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-
uname -s
-
uname -m
> /usr/local/bin/docker-compose
[root@jiangwang ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
1004231004230026600:00:01 0:00:01 --:--:-- 26610016.7M 10016.7M 00 3797k 00:00:04 0:00:04 --:--:-- 6159k
[root@jiangwang ~]# cd /[root@jiangwang /]# cd usr/local/bin/[root@jiangwang bin]# ll
total 17932
-rwxr-xr-x 1 root root 388 Apr 262020 chardetect
-rwxr-xr-x 1 root root 396 Apr 262020 cloud-id
-rwxr-xr-x 1 root root 400 Apr 262020 cloud-init
-rwxr-xr-x 1 root root 2108 Apr 262020 cloud-init-per
-rw-r--r-- 1 root root 17586312 Mar 19 09:33 docker-compose
-rwxr-xr-x 1 root root 404 Apr 262020 easy_install
-rwxr-xr-x 1 root root 234 Apr 262020 easy_install-3.6
-rwxr-xr-x 1 root root 412 Apr 262020 easy_install-3.8
-rwxr-xr-x 1 root root 1758 Mar 718:40 jemalloc-config
-rwxr-xr-x 1 root root 145 Mar 718:40 jemalloc.sh
-rwxr-xr-x 1 root root 179069 Mar 718:40 jeprof
-rwxr-xr-x 1 root root 1003 Apr 262020 jsondiff
-rwxr-xr-x 1 root root 3661 Apr 262020 jsonpatch
-rwxr-xr-x 1 root root 1837 Apr 262020 jsonpointer
-rwxr-xr-x 1 root root 397 Apr 262020 jsonschema
-rwxr-xr-x 1 root root 1461 Jun 142017 libmcrypt-config
lrwxrwxrwx 1 root root 12 Mar 718:41 luajit -> luajit-2.0.4
-rwxr-xr-x 1 root root 449296 Mar 718:41 luajit-2.0.4
-rwxr-xr-x 1 root root 83224 Jun 142017 mcrypt
lrwxrwxrwx 1 root root 6 Mar 718:39 mdecrypt -> mcrypt
[root@jiangwang bin]#
二、授权
# 授权[root@jiangwang bin]# sudo chmod +x docker-compose# 查看docker compose 是否安装成功[root@jiangwang bin]# docker-compose versiondocker-compose version 1.25.5, build 8a1c60f6
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019# 出现以上信息,表明 docker-compose 安装成功
三、快速上手
我们以官网上的简单示例来看看 docker compose 的使用方法。
我们设计这么一个场景,使用 Python 启动一个 Web 服务,输出一个
hello()
方法,每次访问的时候在 Redis 缓存中进行计数,并且将统计的结果打印到页面中。
第一步、创建python服务
- 创建项目路径
# mkdir composetest 创建文件夹# cd composetest 进入文件夹[root@jiangwang home]# mkdir composetest[root@jiangwang home]# cd composetest/[root@jiangwang composetest]#
- 在目录下创建
app.py
文件
importtimeimport redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries =5while 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)
这段 Python 程序的内容就是,启动后连接 Redis 并且输出
hello()
方法,当每次访问的时候累计访问次数并且将结果放回到页面。
- 在同目录下创建
requirements.txt
文件,添加项目依赖的python包:
flask
redis
第二步,创建 Dockerfile
- 创建一个 Dockerfile 文件
FROM python:3.6-alpine # 使用基础镜像 Python 3.4
ADD . /code # 将当前目录映射到镜像`/code`目录下
WORKDIR /code # 设置工作目录为 `/code`
RUN pip install -r requirements.txt # 安装 Python 依赖包
CMD ["python", "app.py"]# 启动 `app.py` 程序
第三步,使用 Compose 文件定义一个服务
在当期目录下,我们创建一个 docker-compose.yml 文件,内容如下:
version: '3.8'
services:
web:
build: .
command: python app.py
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
这个 Compose 文件定义了两个服务, 一个 Pyhon Web 服务和 redis 服务。
- Pyhon Web 服务:使用 Dockerfile 构建了当前镜像。将 Web 容器内部的5000端口映射到 host 的5000端口;并将 Web 容器与 redis 容器连接。
- redis服务:该容器直接由官方的 redis 镜像创建。
第四步、使用 Compose 编译启动应用
使用命令
docker-compose up
启动
[root@jiangwang composetest]# docker-compose up
composetest_redis_1 is up-to-date
Recreating composetest_web_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 |1:C 19 Mar 2021 02:26:22.509 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 |1:C 19 Mar 2021 02:26:22.509 # Redis version=6.0.10, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 |1:C 19 Mar 2021 02:26:22.509 # 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 19 Mar 2021 02:26:22.511 * Running mode=standalone, port=6379.
redis_1 |1:M 19 Mar 2021 02:26:22.511 # 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 19 Mar 2021 02:26:22.511 # Server initialized
redis_1 |1:M 19 Mar 2021 02:26:22.511 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 |1:M 19 Mar 2021 02:26:22.511 # 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 madvise > /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 (set to 'madvise' or 'never').
redis_1 |1:M 19 Mar 2021 02:26:22.511 * Ready to accept connections
redis_1 |1:signal-handler (1616121381) Received SIGTERM scheduling shutdown...
redis_1 |1:M 19 Mar 2021 02:36:21.099 # User requested shutdown...
redis_1 |1:M 19 Mar 2021 02:36:21.099 * Saving the final RDB snapshot before exiting.
redis_1 |1:M 19 Mar 2021 02:36:21.101 * DB saved on disk
redis_1 |1:M 19 Mar 2021 02:36:21.101 # Redis is now ready to exit, bye bye...
redis_1 |1:C 19 Mar 2021 02:38:25.764 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 |1:C 19 Mar 2021 02:38:25.764 # Redis version=6.0.10, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 |1:C 19 Mar 2021 02:38:25.764 # 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 19 Mar 2021 02:38:25.765 * Running mode=standalone, port=6379.
redis_1 |1:M 19 Mar 2021 02:38:25.765 # 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 19 Mar 2021 02:38:25.765 # Server initialized
redis_1 |1:M 19 Mar 2021 02:38:25.765 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 |1:M 19 Mar 2021 02:38:25.765 # 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 madvise > /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 (set to 'madvise' or 'never').
redis_1 |1:M 19 Mar 2021 02:38:25.766 * Loading RDB produced by version 6.0.10
redis_1 |1:M 19 Mar 2021 02:38:25.766 * RDB age 124 seconds
redis_1 |1:M 19 Mar 2021 02:38:25.766 * RDB memory usage when created 0.77 Mb
redis_1 |1:M 19 Mar 2021 02:38:25.766 * DB loaded from disk: 0.000 seconds
redis_1 |1:M 19 Mar 2021 02:38:25.766 * Ready to accept connections
web_1 | * Serving Flask app "app"(lazy loading)
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: 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: 271-130-179
访问
localhost:5000
,测试一下:
[root@jiangwang composetest]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@jiangwang composetest]# curl localhost:5000
Hello World! I have been seen 2 times.
[root@jiangwang composetest]# curl localhost:5000
Hello World! I have been seen 3 times.
[root@jiangwang composetest]# curl localhost:5000
Hello World! I have been seen 4 times.
[root@jiangwang composetest]# curl localhost:5000
Hello World! I have been seen 5 times.
[root@jiangwang composetest]#
每访问一次,数字会不断的增长。
Docker Compose常用命令
查看启动的服务
[root@jiangwang composetest]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
composetest_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composetest_web_1 python app.py Up 0.0.0.0:5000->5000/tcp
停止服务
[root@jiangwang composetest]# docker-compose stop
Stopping composetest_web_1 ... done
Stopping composetest_redis_1 ... done
在后台启动服务
[root@jiangwang composetest]# docker-compose up -d
Starting composetest_redis_1 ... done
Starting composetest_web_1 ... done
其它常用命令
#查看帮助docker-compose -h
# -f 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定。docker-compose -f docker-compose.yml up -d
#启动所有容器,-d 将会在后台启动并运行所有的容器docker-compose up -d
#停用移除所有容器以及网络相关docker-compose down
#查看服务容器的输出docker-compose logs
#列出项目中目前的所有容器docker-composeps#构建(重新构建)项目中的服务容器。服务容器一旦构建后,将会带上一个标记名,例如对于 web 项目中的一个 db 容器,可能是 web_db。可以随时在项目目录下运行 docker-compose build 来重新构建服务docker-compose build
#拉取服务依赖的镜像docker-compose pull
#重启项目中的服务docker-compose restart
#删除所有(停止状态的)服务容器。推荐先执行 docker-compose stop 命令来停止容器。docker-composerm#在指定服务上执行一个命令。docker-compose run ubuntu ping docker.com
#设置指定服务运行的容器个数。通过 service=num 的参数来设置数量docker-compose scale web=3db=2#启动已经存在的服务容器。docker-compose start
#停止已经处于运行状态的容器,但不删除它。通过 docker-compose start 可以再次启动这些容器。docker-compose stop
版权归原作者 初念初恋 所有, 如有侵权,请联系我们删除。