关于docker:Docker八Docker-之-Docker-Compose

45次阅读

共计 9149 个字符,预计需要花费 23 分钟才能阅读完成。

人生有涯,学海无涯

简介

Compose 我的项目是 Docker 官网的开源我的项目,负责实现对 Docker 容器集群的疾速编排。从性能上看,跟 OpenStack 中的 Heat 非常相似。

其代码目前在 https://github.com/docker/com… 上开源。

通过后面文章的介绍,咱们晓得应用一个 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 装置

官网文档:https://docs.docker.com/compo…

一、下载 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
100   423  100   423    0     0    266      0  0:00:01  0:00:01 --:--:--   266
100 16.7M  100 16.7M    0     0  3797k      0  0: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 26  2020 chardetect
-rwxr-xr-x 1 root root      396 Apr 26  2020 cloud-id
-rwxr-xr-x 1 root root      400 Apr 26  2020 cloud-init
-rwxr-xr-x 1 root root     2108 Apr 26  2020 cloud-init-per
-rw-r--r-- 1 root root 17586312 Mar 19 09:33 docker-compose
-rwxr-xr-x 1 root root      404 Apr 26  2020 easy_install
-rwxr-xr-x 1 root root      234 Apr 26  2020 easy_install-3.6
-rwxr-xr-x 1 root root      412 Apr 26  2020 easy_install-3.8
-rwxr-xr-x 1 root root     1758 Mar  7 18:40 jemalloc-config
-rwxr-xr-x 1 root root      145 Mar  7 18:40 jemalloc.sh
-rwxr-xr-x 1 root root   179069 Mar  7 18:40 jeprof
-rwxr-xr-x 1 root root     1003 Apr 26  2020 jsondiff
-rwxr-xr-x 1 root root     3661 Apr 26  2020 jsonpatch
-rwxr-xr-x 1 root root     1837 Apr 26  2020 jsonpointer
-rwxr-xr-x 1 root root      397 Apr 26  2020 jsonschema
-rwxr-xr-x 1 root root     1461 Jun 14  2017 libmcrypt-config
lrwxrwxrwx 1 root root       12 Mar  7 18:41 luajit -> luajit-2.0.4
-rwxr-xr-x 1 root root   449296 Mar  7 18:41 luajit-2.0.4
-rwxr-xr-x 1 root root    83224 Jun 14  2017 mcrypt
lrwxrwxrwx 1 root root        6 Mar  7 18:39 mdecrypt -> mcrypt
[root@jiangwang bin]# 

二、受权

# 受权
[root@jiangwang bin]# sudo chmod +x docker-compose
# 查看 docker compose 是否装置胜利
[root@jiangwang bin]# docker-compose version
docker-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 文件
import time

import redis
from flask import Flask

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

def get_hit_count():
    retries = 5
    while 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-compose ps

#构建(从新构建)我的项目中的服务容器。服务容器一旦构建后,将会带上一个标记名,例如对于 web 我的项目中的一个 db 容器,可能是 web_db。能够随时在我的项目目录下运行 docker-compose build 来从新构建服务
docker-compose build

#拉取服务依赖的镜像
docker-compose pull

#重启我的项目中的服务
docker-compose restart

#删除所有(进行状态的)服务容器。举荐先执行 docker-compose stop 命令来进行容器。docker-compose rm 

#在指定服务上执行一个命令。docker-compose run ubuntu ping docker.com

#设置指定服务运行的容器个数。通过 service=num 的参数来设置数量
docker-compose scale web=3 db=2

#启动曾经存在的服务容器。docker-compose start

#进行曾经处于运行状态的容器,但不删除它。通过 docker-compose start 能够再次启动这些容器。docker-compose stop

参考

Get started with Docker Compose

应用 Docker-Compose 编排容器

正文完
 0