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 Mac
、 Docker for Windows
自带 docker-compose
二进制文件,安装 Docker 之后可以直接使用。
查看本机是否安装
[root@wangpengliang ~]# docker-compose --version
-bash: docker-compose: 未找到命令
Linux下安装 Compose
二进制包安装
在 Linux 上的也安装十分简单,从 官方 GitHub Release 处直接下载编译好的二进制文件即可。
例如,在 Linux 64 位系统上直接下载对应的二进制包
$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-uname -s-uname -m > /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
查看本机架构
[root@wangpengliang ~]# uname -m
x86_64
这里选择使用二进制包下载安装
[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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 633 100 633 0 0 63 0 0:00:10 0:00:09 0:00:01 155
100 8649k 100 8649k 0 0 292k 0 0:00:29 0:00:29 --:--:-- 1989k
[root@wangpengliang ~]# curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10554 100 10554 0 0 1435 0 0:00:07 0:00:07 --:--:-- 2753
[root@wangpengliang ~]# chmod +x /usr/local/bin/docker-compose
[root@wangpengliang ~]# docker-compose --version
docker-compose version 1.17.1, build 6d101fb
PIP 安装
注意:
x86_64
架构的 Linux 建议按照上边的方法下载二进制包进行安装,如果计算机的架构是ARM
(例如,树莓派),再使用 pip 安装
这种方式是将 Compose 当作一个 Python 应用来从 pip 源中安装
执行安装命令:
sudo pip install -U docker-compose
看到类似如下输出,说明安装成功
Collecting docker-compose
Downloading docker-compose-1.17.1.tar.gz (149kB): 149kB downloaded
...
Successfully installed docker-compose cached-property requests texttable websocket-client docker-py dockerpty six enum34 backports.ssl-match-hostname ipaddress
bash 补全命令
$ curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
容器中执行
Compose 既然是一个 Python 应用,自然也可以直接用容器来执行
$ curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
查看下载的 run.sh
脚本内容
set -e
VERSION="1.8.0"
IMAGE="docker/compose:$VERSION"
# Setup options for connecting to docker host
if [ -z "$DOCKER_HOST" ]; then
DOCKER_HOST="/var/run/docker.sock"
fi
if [ -S "$DOCKER_HOST" ]; then
DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -e DOCKER_HOST"
else
DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
fi
# Setup volume mounts for compose config and context
if [ "$(pwd)" != '/' ]; then
VOLUMES="-v $(pwd):$(pwd)"
fi
if [ -n "$COMPOSE_FILE" ]; then
compose_dir=$(dirname $COMPOSE_FILE)
fi
# TODO: also check --file argument
if [ -n "$compose_dir" ]; then
VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
fi
if [ -n "$HOME" ]; then
VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
fi
# Only allocate tty if we detect one
if [ -t 1 ]; then
DOCKER_RUN_OPTIONS="-t"
fi
if [ -t 0 ]; then
DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
fi
exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"
可以看到,其实是下载了 docker/compose
镜像并运行
卸载
如果是二进制包方式安装的,删除二进制文件即可
$ sudo rm /usr/local/bin/docker-compose
如果是通过 pip
安装的,则执行如下命令即可
$ sudo pip uninstall docker-compose
使用 DockerCompose
之前说到Compose
中有两个重要的概念:
- 服务 (
service
):一个应用容器,实际上可以运行多个相同镜像的实例。 - 项目 (
project
):由一组关联的应用容器组成的一个完整业务单元。
可见,一个项目可以由多个服务(容器)关联而成,Compose
面向项目进行管理。
创建Web应用
示例用 Python
创建一个能够记录页面访问次数的 web 网站。新建文件夹,在该目录中编写 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! 该页面已被访问 {} 次。\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
Dockerfile
编写 Dockerfile
文件内容为:
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install redis flask
CMD ["python", "app.py"]
docker-compose.yml
编写 docker-compose.yml
文件,这个是 Compose 使用的主模板文件
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
运行 compose
docker-compose up
创建pythontests
目录,创建 app.py
Dockerfile
docker-compose.yml
文件
[root@wangpengliang pythontests]# ls
app.py docker-compose.yml Dockerfile
运行 docker-compose
[root@wangpengliang pythontests]# docker-compose up
Building web
Step 1/5 : FROM python:3.6-alpine # 下载基础镜像python:3.6-alpine
3.6-alpine: Pulling from library/python
540db60ca938: Pull complete
a7ad1a75a999: Pull complete
5545670c3922: Pull complete
c89910f38943: Pull complete
b6a40d090e87: Pull complete
Digest: sha256:492bb540e9c9bc9f586d5d69467c66bc32072d9af48463b1f0054d4ff9b93709
Status: Downloaded newer image for python:3.6-alpine
---> ac438c122d19
Step 2/5 : ADD . /code # 拷贝文件到/code目录
---> 68cbe5b2f598
Step 3/5 : WORKDIR /code # 设置/code目录为工作目录
---> Running in 4fd09a1580a8
Removing intermediate container 4fd09a1580a8 # 拆卸中间容器:4fd09a1580a8
---> 2e4bbd31555d
Step 4/5 : RUN pip install redis flask # 运行pip安装redis、flask
---> Running in c12f4ece64f1
Collecting redis
Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Collecting flask
Downloading Flask-2.0.1-py3-none-any.whl (94 kB)
Collecting click>=7.1.2
Downloading click-8.0.1-py3-none-any.whl (97 kB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting importlib-metadata
Downloading importlib_metadata-4.5.0-py3-none-any.whl (17 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1.tar.gz (18 kB)
Collecting dataclasses
Downloading dataclasses-0.8-py3-none-any.whl (19 kB)
Collecting typing-extensions>=3.6.4
Downloading typing_extensions-3.10.0.0-py3-none-any.whl (26 kB)
Collecting zipp>=0.5
Downloading zipp-3.4.1-py3-none-any.whl (5.2 kB)
Building wheels for collected packages: MarkupSafe
Building wheel for MarkupSafe (setup.py): started
Building wheel for MarkupSafe (setup.py): finished with status 'done'
Created wheel for MarkupSafe: filename=MarkupSafe-2.0.1-py3-none-any.whl size=9761 sha256=e2a25263f4c7babbdd0cdda87cad139e54eb972cf742f2f7b7893003e0ecfe97
Stored in directory: /root/.cache/pip/wheels/05/46/9b/189d9acb1f643857fb8ad990ca04c02509c35d3ad6fac81794
Successfully built MarkupSafe
Installing collected packages: zipp, typing-extensions, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, Jinja2, itsdangerous, click, redis, flask
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
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
Removing intermediate container c12f4ece64f1
---> 061f502fa95e
Step 5/5 : CMD ["python", "app.py"]
---> Running in c6ac95ba9659
Removing intermediate container c6ac95ba9659
---> aabe65a53ffd
Successfully built aabe65a53ffd
Successfully tagged pythontests_web:latest
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`.
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
29712d301e8c: Pull complete
8173c12df40f: Pull complete
8cc52074f78e: Pull complete
aa7854465cce: Pull complete
6ab1d05b4973: Pull complete
Digest: sha256:eaaa58f8757d6f04b2e34ace57a71d79f8468053c198f5758fd2068ac235f303
Status: Downloaded newer image for redis:alpine
Creating pythontests_web_1
Creating pythontests_redis_1
Attaching to pythontests_redis_1, pythontests_web_1
redis_1 | 1:C 09 Jun 2021 03:16:24.588 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
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
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
redis_1 | 1:M 09 Jun 2021 03:16:24.588 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 09 Jun 2021 03:16:24.590 * Running mode=standalone, port=6379.
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.
redis_1 | 1:M 09 Jun 2021 03:16:24.590 # Server initialized
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.
redis_1 | 1:M 09 Jun 2021 03:16:24.590 * 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 all addresses.
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | * Running on http://172.19.0.2:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 103-300-970
web_1 | 172.19.0.1 - - [09/Jun/2021 03:17:00] "GET / HTTP/1.1" 200 -
测试结果
[root@wangpengliang pythontests]# curl 172.18.0.2:5000
Hello World! 该页面已被访问 1 次。
[root@wangpengliang pythontests]# curl 172.18.0.2:5000
Hello World! 该页面已被访问 2 次。
[root@wangpengliang pythontests]# curl 172.18.0.2:5000
Hello World! 该页面已被访问 3 次。
[root@wangpengliang pythontests]# curl 172.18.0.2:5000
Hello World! 该页面已被访问 4 次。
[root@wangpengliang pythontests]# curl 172.18.0.2:5000
Hello World! 该页面已被访问 5 次。