DockerCompose 概念

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责实现对 Docker 容器集群的快速编排,快速的部署分布式应用。

通过之前学习已经了解到使用一个 Dockerfile 模板文件,可以很方便的定义一个单独的应用容器。然而在日常工作中,经常碰到的情况是需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web 项目,除了 Web 服务容器本身,往往还需要再加上后端的数据库服务容器、负载均衡容器等等。Compose 就是用来处理这些事情的。它通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目project。

Compose 中有两个重要的概念:

  • 服务 (service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例
  • 项目 (project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义

Compose 的默认管理对象是项目,通过子命令对项目中的一组容器进行便捷地生命周期管理,Compose 项目由 Python 编写,实现上调用了 Docker 服务提供的 API 来对容器进行管理。因此只要所操作的平台支持 Docker API 就可以在其上利用 Compose 来进行编排管理。

安装与卸载

Compose 支持 Linux、macOS、Windows 10 三大平台,可以通过 Python 的包管理工具 pip 进行安装,也可以直接下载编译好的二进制文件使用,也能够直接在 Docker 容器中运行。

前两种方式是传统方式,适合本地环境下安装使用;最后一种方式则不破坏系统环境,更适合云计算场景,Docker for MacDocker for Windows 自带 docker-compose 二进制文件,安装 Docker 之后可以直接使用。
查看本机是否安装

  1. [root@wangpengliang ~]# docker-compose --version
  2. -bash: docker-compose: 未找到命令

Linux下安装 Compose

二进制包安装

在 Linux 上的也安装十分简单,从 官方 GitHub Release 处直接下载编译好的二进制文件即可。

例如,在 Linux 64 位系统上直接下载对应的二进制包

  1. $ sudo curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-uname -s-uname -m > /usr/local/bin/docker-compose
  2. $ sudo chmod +x /usr/local/bin/docker-compose

查看本机架构

  1. [root@wangpengliang ~]# uname -m
  2. x86_64

这里选择使用二进制包下载安装

  1. [root@wangpengliang ~]# curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 633 100 633 0 0 63 0 0:00:10 0:00:09 0:00:01 155
  5. 100 8649k 100 8649k 0 0 292k 0 0:00:29 0:00:29 --:--:-- 1989k
  6. [root@wangpengliang ~]# curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
  7. % Total % Received % Xferd Average Speed Time Time Time Current
  8. Dload Upload Total Spent Left Speed
  9. 100 10554 100 10554 0 0 1435 0 0:00:07 0:00:07 --:--:-- 2753
  10. [root@wangpengliang ~]# chmod +x /usr/local/bin/docker-compose
  11. [root@wangpengliang ~]# docker-compose --version
  12. docker-compose version 1.17.1, build 6d101fb

PIP 安装

注意: x86_64 架构的 Linux 建议按照上边的方法下载二进制包进行安装,如果计算机的架构是 ARM (例如,树莓派),再使用 pip 安装

这种方式是将 Compose 当作一个 Python 应用来从 pip 源中安装

执行安装命令:

  1. sudo pip install -U docker-compose

看到类似如下输出,说明安装成功

  1. Collecting docker-compose
  2. Downloading docker-compose-1.17.1.tar.gz (149kB): 149kB downloaded
  3. ...
  4. Successfully installed docker-compose cached-property requests texttable websocket-client docker-py dockerpty six enum34 backports.ssl-match-hostname ipaddress

bash 补全命令

  1. $ curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

容器中执行
Compose 既然是一个 Python 应用,自然也可以直接用容器来执行

  1. $ curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose
  2. $ chmod +x /usr/local/bin/docker-compose

查看下载的 run.sh 脚本内容

  1. set -e
  2. VERSION="1.8.0"
  3. IMAGE="docker/compose:$VERSION"
  4. # Setup options for connecting to docker host
  5. if [ -z "$DOCKER_HOST" ]; then
  6. DOCKER_HOST="/var/run/docker.sock"
  7. fi
  8. if [ -S "$DOCKER_HOST" ]; then
  9. DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -e DOCKER_HOST"
  10. else
  11. DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
  12. fi
  13. # Setup volume mounts for compose config and context
  14. if [ "$(pwd)" != '/' ]; then
  15. VOLUMES="-v $(pwd):$(pwd)"
  16. fi
  17. if [ -n "$COMPOSE_FILE" ]; then
  18. compose_dir=$(dirname $COMPOSE_FILE)
  19. fi
  20. # TODO: also check --file argument
  21. if [ -n "$compose_dir" ]; then
  22. VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
  23. fi
  24. if [ -n "$HOME" ]; then
  25. VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
  26. fi
  27. # Only allocate tty if we detect one
  28. if [ -t 1 ]; then
  29. DOCKER_RUN_OPTIONS="-t"
  30. fi
  31. if [ -t 0 ]; then
  32. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
  33. fi
  34. exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"

可以看到,其实是下载了 docker/compose 镜像并运行

卸载

如果是二进制包方式安装的,删除二进制文件即可

  1. $ sudo rm /usr/local/bin/docker-compose

如果是通过 pip 安装的,则执行如下命令即可

  1. $ sudo pip uninstall docker-compose

使用 DockerCompose

之前说到Compose 中有两个重要的概念:

  • 服务 (service):一个应用容器,实际上可以运行多个相同镜像的实例。
  • 项目 (project):由一组关联的应用容器组成的一个完整业务单元。

可见,一个项目可以由多个服务(容器)关联而成,Compose 面向项目进行管理。

创建Web应用

示例用 Python 创建一个能够记录页面访问次数的 web 网站。新建文件夹,在该目录中编写 app.py 文件

  1. from flask import Flask
  2. from redis import Redis
  3. app = Flask(__name__)
  4. redis = Redis(host='redis', port=6379)
  5. @app.route('/')
  6. def hello():
  7. count = redis.incr('hits')
  8. return 'Hello World! 该页面已被访问 {} 次。\n'.format(count)
  9. if __name__ == "__main__":
  10. app.run(host="0.0.0.0", debug=True)

Dockerfile

编写 Dockerfile 文件内容为:

  1. FROM python:3.6-alpine
  2. ADD . /code
  3. WORKDIR /code
  4. RUN pip install redis flask
  5. CMD ["python", "app.py"]

docker-compose.yml

编写 docker-compose.yml 文件,这个是 Compose 使用的主模板文件

  1. version: '3'
  2. services:
  3. web:
  4. build: .
  5. ports:
  6. - "5000:5000"
  7. redis:
  8. image: "redis:alpine"

运行 compose

  1. docker-compose up

创建pythontests 目录,创建 app.py Dockerfile docker-compose.yml 文件

  1. [root@wangpengliang pythontests]# ls
  2. app.py docker-compose.yml Dockerfile

运行 docker-compose

  1. [root@wangpengliang pythontests]# docker-compose up
  2. Building web
  3. Step 1/5 : FROM python:3.6-alpine # 下载基础镜像python:3.6-alpine
  4. 3.6-alpine: Pulling from library/python
  5. 540db60ca938: Pull complete
  6. a7ad1a75a999: Pull complete
  7. 5545670c3922: Pull complete
  8. c89910f38943: Pull complete
  9. b6a40d090e87: Pull complete
  10. Digest: sha256:492bb540e9c9bc9f586d5d69467c66bc32072d9af48463b1f0054d4ff9b93709
  11. Status: Downloaded newer image for python:3.6-alpine
  12. ---> ac438c122d19
  13. Step 2/5 : ADD . /code # 拷贝文件到/code目录
  14. ---> 68cbe5b2f598
  15. Step 3/5 : WORKDIR /code # 设置/code目录为工作目录
  16. ---> Running in 4fd09a1580a8
  17. Removing intermediate container 4fd09a1580a8 # 拆卸中间容器:4fd09a1580a8
  18. ---> 2e4bbd31555d
  19. Step 4/5 : RUN pip install redis flask # 运行pip安装redis、flask
  20. ---> Running in c12f4ece64f1
  21. Collecting redis
  22. Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
  23. Collecting flask
  24. Downloading Flask-2.0.1-py3-none-any.whl (94 kB)
  25. Collecting click>=7.1.2
  26. Downloading click-8.0.1-py3-none-any.whl (97 kB)
  27. Collecting Werkzeug>=2.0
  28. Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
  29. Collecting itsdangerous>=2.0
  30. Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
  31. Collecting Jinja2>=3.0
  32. Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
  33. Collecting importlib-metadata
  34. Downloading importlib_metadata-4.5.0-py3-none-any.whl (17 kB)
  35. Collecting MarkupSafe>=2.0
  36. Downloading MarkupSafe-2.0.1.tar.gz (18 kB)
  37. Collecting dataclasses
  38. Downloading dataclasses-0.8-py3-none-any.whl (19 kB)
  39. Collecting typing-extensions>=3.6.4
  40. Downloading typing_extensions-3.10.0.0-py3-none-any.whl (26 kB)
  41. Collecting zipp>=0.5
  42. Downloading zipp-3.4.1-py3-none-any.whl (5.2 kB)
  43. Building wheels for collected packages: MarkupSafe
  44. Building wheel for MarkupSafe (setup.py): started
  45. Building wheel for MarkupSafe (setup.py): finished with status 'done'
  46. Created wheel for MarkupSafe: filename=MarkupSafe-2.0.1-py3-none-any.whl size=9761 sha256=e2a25263f4c7babbdd0cdda87cad139e54eb972cf742f2f7b7893003e0ecfe97
  47. Stored in directory: /root/.cache/pip/wheels/05/46/9b/189d9acb1f643857fb8ad990ca04c02509c35d3ad6fac81794
  48. Successfully built MarkupSafe
  49. Installing collected packages: zipp, typing-extensions, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, Jinja2, itsdangerous, click, redis, flask
  50. Successfully installed Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.1 dataclasses-0.8 flask-2.0.1 importlib-metadata-4.5.0 itsdangerous-2.0.1 redis-3.5.3 typing-extensions-3.10.0.0 zipp-3.4.1
  51. WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
  52. Removing intermediate container c12f4ece64f1
  53. ---> 061f502fa95e
  54. Step 5/5 : CMD ["python", "app.py"]
  55. ---> Running in c6ac95ba9659
  56. Removing intermediate container c6ac95ba9659
  57. ---> aabe65a53ffd
  58. Successfully built aabe65a53ffd
  59. Successfully tagged pythontests_web:latest
  60. WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
  61. Pulling redis (redis:alpine)...
  62. alpine: Pulling from library/redis
  63. 29712d301e8c: Pull complete
  64. 8173c12df40f: Pull complete
  65. 8cc52074f78e: Pull complete
  66. aa7854465cce: Pull complete
  67. 6ab1d05b4973: Pull complete
  68. Digest: sha256:eaaa58f8757d6f04b2e34ace57a71d79f8468053c198f5758fd2068ac235f303
  69. Status: Downloaded newer image for redis:alpine
  70. Creating pythontests_web_1
  71. Creating pythontests_redis_1
  72. Attaching to pythontests_redis_1, pythontests_web_1
  73. redis_1 | 1:C 09 Jun 2021 03:16:24.588 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  74. redis_1 | 1:C 09 Jun 2021 03:16:24.588 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
  75. redis_1 | 1:C 09 Jun 2021 03:16:24.588 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
  76. redis_1 | 1:M 09 Jun 2021 03:16:24.588 * monotonic clock: POSIX clock_gettime
  77. redis_1 | 1:M 09 Jun 2021 03:16:24.590 * Running mode=standalone, port=6379.
  78. redis_1 | 1:M 09 Jun 2021 03:16:24.590 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  79. redis_1 | 1:M 09 Jun 2021 03:16:24.590 # Server initialized
  80. redis_1 | 1:M 09 Jun 2021 03:16:24.590 # 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.
  81. redis_1 | 1:M 09 Jun 2021 03:16:24.590 * Ready to accept connections
  82. web_1 | * Serving Flask app 'app' (lazy loading)
  83. web_1 | * Environment: production
  84. web_1 | WARNING: This is a development server. Do not use it in a production deployment.
  85. web_1 | Use a production WSGI server instead.
  86. web_1 | * Debug mode: on
  87. web_1 | * Running on all addresses.
  88. web_1 | WARNING: This is a development server. Do not use it in a production deployment.
  89. web_1 | * Running on http://172.19.0.2:5000/ (Press CTRL+C to quit)
  90. web_1 | * Restarting with stat
  91. web_1 | * Debugger is active!
  92. web_1 | * Debugger PIN: 103-300-970
  93. web_1 | 172.19.0.1 - - [09/Jun/2021 03:17:00] "GET / HTTP/1.1" 200 -

测试结果

  1. [root@wangpengliang pythontests]# curl 172.18.0.2:5000
  2. Hello World! 该页面已被访问 1 次。
  3. [root@wangpengliang pythontests]# curl 172.18.0.2:5000
  4. Hello World! 该页面已被访问 2 次。
  5. [root@wangpengliang pythontests]# curl 172.18.0.2:5000
  6. Hello World! 该页面已被访问 3 次。
  7. [root@wangpengliang pythontests]# curl 172.18.0.2:5000
  8. Hello World! 该页面已被访问 4 次。
  9. [root@wangpengliang pythontests]# curl 172.18.0.2:5000
  10. Hello World! 该页面已被访问 5 次。