Docker 三剑客之 Docker Compose
Docker Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排,开源地址:https://github.com/docker/compose
Docker Compose 中的两个重要概念:
- 服务 (service):一个应用容器,实际上可以运行多个相同镜像的实例。
- 项目 (project):由一组关联的应用容器组成的一个完整业务单元。
一个项目可以由多个服务关联(容器)而成,并使用docker-compose.yml
进行管理。
安装命令:
$ curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
这种方式安装巨慢无比,我们可以使用 PIP 安装,如果没有 PIP 环境,需要手动安装下:
$ apt-get update
$ apt-get -y install python-pip
或
$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
$ python get-pip.py
$ pip --help
$ pip -V
PIP 安装命令:
$ sudo pip install -U docker-compose
PIP 安装之后,执行 Docker Compose 命令的时候,会出现下面错误:
$ docker-compose up
Cannot open self /usr/local/bin/docker-compose or archive /usr/local/bin/docker-compose.pkg
解决方式,从官方源中手动下载安装包,然后移动到制定目录赋予权限,命令如下:
$ cp -i docker-compose-Linux-x86_64 /usr/local/bin
$ mv docker-compose-Linux-x86_64 docker-compose
$ chmod +x /usr/local/bin/docker-compose
安装好之后,我们就可以使用 Docker Compose 进行容器编排了,我们就拿之前做的 HelloDocker.Sample 项目做为示例。
我们在HelloDocker.Sample.Web
项目目录下,创建docker-compose.yml
配置文件,示例:
consul:
command: agent -bind=10.9.10.89 -client=0.0.0.0 -node=vagrant-ubuntu-xishuai -retry-join=10.9.2.236
image: consul:latest
ports:
- 8300:8300
- 8400:8400
- 8500:8500
- 8600:53/udp
net: host
fabio:
image: magiconair/fabio:latest
ports:
- 9999:9999
- 9998:9998
net: host
volumes:
- /etc/fabio/fabio.properties:/etc/fabio/fabio.properties
environment:
- SERVICE_9998_TAGS=urlprefix-/fabio
hello-docker:
ports:
- 54917:54917
build: .
net: host
其实看配置就明了了,一个项目中包含三个服务(consul
, fabio
和hell-docker
容器),Docker Compose 对这三个服务进行编排部署,我们执行docker-compose up
命令,启动项目:
$ docker-compose up -d
Creating hellodockerweb_fabio_1 ...
Creating hellodockerweb_hello-docker_1 ...
Creating hellodockerweb_consul_1 ...
Creating hellodockerweb_fabio_1
Creating hellodockerweb_hello-docker_1
Creating hellodockerweb_consul_1 ... done
我们可以执行docker-compose ps
, 查看运行的服务(容器):
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------
hellodockerweb_consul_1 docker-entrypoint.sh agent ... Up
hellodockerweb_fabio_1 /fabio -cfg /etc/fabio/fab ... Up
hellodockerweb_hello-docker_1 dotnet HelloDocker.Web.dll Up
或者执行docker-compose logs
,查看日志:
docker-compose logs
Attaching to hellodockerweb_consul_1, hellodockerweb_hello-docker_1, hellodockerweb_fabio_1
consul_1 | ==> Starting Consul agent...
consul_1 | ==> Consul agent running!
consul_1 | Version: 'v1.0.0'
consul_1 | Node ID: '521358bc-efc0-1a69-4a58-a9bfc8a9b09c'
consul_1 | Node name: 'vagrant-ubuntu-xishuai'
consul_1 | Datacenter: 'dc1' (Segment: '')
consul_1 | Server: false (Bootstrap: false)
consul_1 | Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, DNS: 8600)
consul_1 | Cluster Addr: 10.9.10.89 (LAN: 8301, WAN: 8302)
consul_1 | Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
consul_1 |
consul_1 | ==> Log data will now stream in as it occurs:
consul_1 |
consul_1 | 2017/11/21 04:41:25 [INFO] serf: EventMemberJoin: vagrant-ubuntu-xishuai 10.9.10.89
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp)
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp)
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Started HTTP server on [::]:8500 (tcp)
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Retry join LAN is supported for: aws azure gce softlayer
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Joining LAN cluster...
consul_1 | 2017/11/21 04:41:25 [INFO] agent: (LAN) joining: [10.9.2.236]
consul_1 | 2017/11/21 04:41:25 [WARN] manager: No servers available
consul_1 | 2017/11/21 04:41:25 [ERR] agent: failed to sync remote state: No known Consul servers
consul_1 | 2017/11/21 04:41:25 [INFO] serf: EventMemberJoin: agent_1 10.9.2.236
consul_1 | 2017/11/21 04:41:25 [INFO] serf: EventMemberJoin: windows64-xishuai 10.9.10.187
consul_1 | 2017/11/21 04:41:25 [INFO] agent: (LAN) joined: 1 Err: <nil>
consul_1 | 2017/11/21 04:41:25 [INFO] agent: Join LAN completed. Synced with 1 initial agents
consul_1 | 2017/11/21 04:41:25 [INFO] consul: adding server agent_1 (Addr: tcp/10.9.2.236:8300) (DC: dc1)
consul_1 | 2017/11/21 04:41:34 [INFO] agent: Synced service 'fabio-vagrant-ubuntu-trusty-9998'
hello-docker_1 | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
hello-docker_1 | No XML encryptor configured. Key {3d8c2a18-ed43-4df9-980f-8573175ebecf} may be persisted to storage in unencrypted form.
hello-docker_1 | Hosting environment: Production
hello-docker_1 | Content root path: /app
hello-docker_1 | Now listening on: http://[::]:54917
hello-docker_1 | Application started. Press Ctrl+C to shut down.
fabio_1 | 2017/11/21 04:41:25 [INFO] Setting log level to INFO
fabio_1 | 2017/11/21 04:41:25 [INFO] Runtime config
fabio_1 | {
fabio_1 | "Proxy": {
fabio_1 | "Strategy": "rnd",
fabio_1 | "Matcher": "prefix",
fabio_1 | "NoRouteStatus": 404,
fabio_1 | "MaxConn": 10000,
fabio_1 | "ShutdownWait": 0,
fabio_1 | "DialTimeout": 30000000000,
fabio_1 | "ResponseHeaderTimeout": 0,
fabio_1 | "KeepAliveTimeout": 0,
fabio_1 | "FlushInterval": 1000000000,
fabio_1 | "LocalIP": "10.0.2.15",
fabio_1 | "ClientIPHeader": "",
fabio_1 | "TLSHeader": "",
fabio_1 | "TLSHeaderValue": "",
fabio_1 | "GZIPContentTypes": null,
fabio_1 | "RequestID": ""
fabio_1 | },
fabio_1 | "Registry": {
fabio_1 | "Backend": "consul",
fabio_1 | "Static": {
fabio_1 | "Routes": ""
fabio_1 | },
fabio_1 | "File": {
fabio_1 | "Path": ""
fabio_1 | },
consul_1 | 2017/11/21 04:41:34 [WARN] agent: check 'service:8a600176-4c58-4edc-95af-71fc27345954' has deregister interval below minimum of 1m0s
consul_1 | 2017/11/21 04:41:34 [INFO] agent: Synced service '8a600176-4c58-4edc-95af-71fc27345954'
consul_1 | 2017/11/21 04:41:34 [INFO] agent: Synced service 'fabio-vagrant-ubuntu-trusty-9998'
consul_1 | 2017/11/21 04:41:35 [INFO] agent: Synced check 'service:fabio-vagrant-ubuntu-trusty-9998'
consul_1 | 2017/11/21 04:41:40 [INFO] agent: Synced check 'service:8a600176-4c58-4edc-95af-71fc27345954'
consul_1 | ==> Newer Consul version available: 1.0.1 (currently running: 1.0.0)
consul_1 | 2017/11/21 04:41:57 [WARN] memberlist: Refuting a suspect message (from: agent_1)
fabio_1 | "Consul": {
fabio_1 | "Addr": "10.9.10.89:8500",
fabio_1 | "Scheme": "http",
fabio_1 | "Token": "",
fabio_1 | "KVPath": "/fabio/config",
fabio_1 | "TagPrefix": "urlprefix-",
fabio_1 | "Register": true,
fabio_1 | "ServiceAddr": "10.9.10.89:9998",
fabio_1 | "ServiceName": "fabio",
fabio_1 | "ServiceTags": null,
fabio_1 | "ServiceStatus": [
fabio_1 | "passing"
fabio_1 | ],
fabio_1 | "CheckInterval": 1000000000,
fabio_1 | "CheckTimeout": 3000000000,
fabio_1 | "CheckScheme": "http",
fabio_1 | "CheckTLSSkipVerify": false
fabio_1 | },
fabio_1 | "Timeout": 10000000000,
fabio_1 | "Retry": 500000000
fabio_1 | },
fabio_1 | "Listen": [
fabio_1 | {
fabio_1 | "Addr": ":9999",
fabio_1 | "Proto": "http",
fabio_1 | "ReadTimeout": 0,
fabio_1 | "WriteTimeout": 0,
fabio_1 | "CertSource": {
fabio_1 | "Name": "",
fabio_1 | "Type": "",
fabio_1 | "CertPath": "",
fabio_1 | "KeyPath": "",
fabio_1 | "ClientCAPath": "",
fabio_1 | "CAUpgradeCN": "",
fabio_1 | "Refresh": 0,
fabio_1 | "Header": null
fabio_1 | },
fabio_1 | "StrictMatch": false,
fabio_1 | "TLSMinVersion": 0,
fabio_1 | "TLSMaxVersion": 0,
fabio_1 | "TLSCiphers": null
fabio_1 | }
fabio_1 | ],
fabio_1 | "Log": {
fabio_1 | "AccessFormat": "common",
fabio_1 | "AccessTarget": "",
fabio_1 | "RoutesFormat": "delta",
fabio_1 | "Level": "INFO"
fabio_1 | },
fabio_1 | "Metrics": {
fabio_1 | "Target": "",
fabio_1 | "Prefix": "{{clean .Hostname}}.{{clean .Exec}}",
fabio_1 | "Names": "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
fabio_1 | "Interval": 30000000000,
fabio_1 | "Timeout": 10000000000,
fabio_1 | "Retry": 500000000,
fabio_1 | "GraphiteAddr": "",
fabio_1 | "StatsDAddr": "",
fabio_1 | "Circonus": {
fabio_1 | "APIKey": "",
fabio_1 | "APIApp": "fabio",
fabio_1 | "APIURL": "",
fabio_1 | "CheckID": "",
fabio_1 | "BrokerID": ""
fabio_1 | }
fabio_1 | },
fabio_1 | "UI": {
fabio_1 | "Listen": {
fabio_1 | "Addr": ":9998",
fabio_1 | "Proto": "http",
fabio_1 | "ReadTimeout": 0,
fabio_1 | "WriteTimeout": 0,
fabio_1 | "CertSource": {
fabio_1 | "Name": "",
fabio_1 | "Type": "",
fabio_1 | "CertPath": "",
fabio_1 | "KeyPath": "",
fabio_1 | "ClientCAPath": "",
fabio_1 | "CAUpgradeCN": "",
fabio_1 | "Refresh": 0,
fabio_1 | "Header": null
fabio_1 | },
fabio_1 | "StrictMatch": false,
fabio_1 | "TLSMinVersion": 0,
fabio_1 | "TLSMaxVersion": 0,
fabio_1 | "TLSCiphers": null
fabio_1 | },
fabio_1 | "Color": "light-green",
fabio_1 | "Title": "",
fabio_1 | "Access": "rw"
fabio_1 | },
fabio_1 | "Runtime": {
fabio_1 | "GOGC": 800,
fabio_1 | "GOMAXPROCS": 1
fabio_1 | },
fabio_1 | "ProfileMode": "",
fabio_1 | "ProfilePath": "/tmp"
fabio_1 | }
fabio_1 | 2017/11/21 04:41:25 [INFO] Version 1.5.3 starting
fabio_1 | 2017/11/21 04:41:25 [INFO] Go runtime is go1.9.2
fabio_1 | 2017/11/21 04:41:25 [INFO] Metrics disabled
fabio_1 | 2017/11/21 04:41:25 [INFO] Setting GOGC=800
fabio_1 | 2017/11/21 04:41:25 [INFO] Setting GOMAXPROCS=1
fabio_1 | 2017/11/21 04:41:25 [WARN] Error initializing backend. Get http://10.9.10.89:8500/v1/agent/self: dial tcp 10.9.10.89:8500: getsockopt: connection refused
fabio_1 | 2017/11/21 04:41:25 [INFO] consul: Connecting to "10.9.10.89:8500" in datacenter "dc1"
fabio_1 | 2017/11/21 04:41:25 [INFO] Admin server access mode "rw"
fabio_1 | 2017/11/21 04:41:25 [INFO] Admin server listening on ":9998"
fabio_1 | 2017/11/21 04:41:25 [INFO] Waiting for first routing table
fabio_1 | 2017/11/21 04:41:25 [INFO] consul: Using dynamic routes
fabio_1 | 2017/11/21 04:41:25 [INFO] consul: Using tag prefix "urlprefix-"
fabio_1 | 2017/11/21 04:41:25 [INFO] consul: Watching KV path "/fabio/config"
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Manual config changed to #3821244
fabio_1 | 2017/11/21 04:41:34 [INFO] HTTP proxy listening on :9999
fabio_1 | 2017/11/21 04:41:34 [INFO] Access logging disabled
fabio_1 | 2017/11/21 04:41:34 [INFO] Using routing strategy "rnd"
fabio_1 | 2017/11/21 04:41:34 [INFO] Using route matching "prefix"
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Health changed to #3821756
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Registered fabio with id "fabio-vagrant-ubuntu-trusty-9998"
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Registered fabio with address "10.9.10.89"
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Registered fabio with tags ""
fabio_1 | 2017/11/21 04:41:34 [INFO] consul: Registered fabio with health check to "http://[10.9.10.89]:9998/health"
fabio_1 | 2017/11/21 04:41:35 [INFO] Config updates
fabio_1 | + route add test /test http://10.9.2.236:23421/
fabio_1 | 2017/11/21 04:41:35 [INFO] consul: Health changed to #3821762
fabio_1 | 2017/11/21 04:41:40 [INFO] consul: Health changed to #3821765
fabio_1 | 2017/11/21 04:41:42 [INFO] Config updates
fabio_1 | + hello-docker /hello-docker http://10.9.10.89:54917/
fabio_1 | + route add
我们可以查看 Consul UI 中的服务是否运行正常:
或者查看 Fabio UI 中的服务是否存在(通过健康检查后会出现):
YAML 配置命令
配置 | 说明 |
---|---|
build | 指定 Dockerfile 所在的目录地址,用于构建镜像,并使用此镜像创建容器,比如上面配置的build: . |
command | 容器的执行命令 |
dns | 自定义 dns 服务器 |
expose | 暴露端口配置,但不映射到宿主机,只被连接的服务访问 |
extends | 对docker-compose.yml 的扩展,配置在服务中 |
image | 使用的镜像名称或镜像 ID |
links | 链接到其它服务中的容器(一般桥接网络模式使用) |
net | 设置容器的网络模式(四种:bridge , none , container:[name or id] 和host ) |
ports | 暴露端口信息,主机和容器的端口映射 |
volumes | 数据卷所挂载路径设置 |
Docker Compose 常用命令
命令 | 说明 |
---|---|
docker-compose build | 构建项目中的镜像,--force-rm :删除构建过程中的临时容器;--no-cache :不使用缓存构建;--pull :获取最新版本的镜像 |
docker-compose up -d | 构建镜像、创建服务和启动项目,-d 表示后台运行 |
docker-compose run ubuntu ls -d | 指定服务上运行一个命令,-d 表示后台运行 |
docker-compose logs | 查看服务容器输出日志 |
docker-compose ps | 列出项目中所有的容器 |
docker-compose pause [service_name] | 暂停一个服务容器 |
docker-compose unpause [service_name] | 恢复已暂停的一个服务容器 |
docker-compose restart | 重启项目中的所有服务容器(也可以指定具体的服务) |
docker-compose stop | 停止运行项目中的所有服务容器(也可以指定具体的服务) |
docker-compose start | 启动已经停止项目中的所有服务容器(也可以指定具体的服务) |
docker-compose rm | 删除项目中的所有服务容器(也可以指定具体的服务),-f :强制删除(包含运行的) |
docker-compose kill | 强制停止项目中的所有服务容器(也可以指定具体的服务) |
参考资料:
- Docker & ASP.NET Core 2.0 微服务跨平台实践
- Docker Compose
- Docker Compose—简化复杂应用的利器
- 使用 Docker Compose 管理多个容器
- https://github.com/CreatorDev/DeviceServer/blob/master/docker-compose.yml
- https://github.com/avthart/fabio-compose/blob/master/docker-compose.yml
- https://github.com/strarsis/consul-fabio-example/blob/master/docker-compose.yml
- https://github.com/DimensionDataResearch/fabio-registrator-demo/blob/master/docker-compose.yml
- https://github.com/strarsis/consul-fabio-example/blob/master/docker-compose.yml
Docker 三剑客之 Docker Compose的更多相关文章
- Docker 三剑客之 Docker Swarm
上一篇:Docker 三剑客之 Docker Compose 阅读目录: Docker Machine 创建 Docker 主机 Docker Swarm 配置集群节点 Docker Service ...
- Docker(五):Docker 三剑客之 Docker Machine
上篇文章Docker(四):Docker 三剑客之 Docker Compose介绍了 Docker Compose,这篇文章我们来了解 Docker Machine . Docker Machine ...
- Docker三剑客之Docker Swarm
一.什么是Docker Swarm Swarm是Docker公司推出的用来管理docker集群的平台,几乎全部用GO语言来完成的开发的,代码开源在https://github.com/docker/s ...
- Docker 三剑客之 Docker Swarm(基于 overlay 组网通信)
相关文章:Docker 三剑客之 Docker Swarm 这一篇主要是对 Docker Swarm 的完善,增加基于 overlay 组网通信,以便 Docker 容器可以跨主机访问. 不同主机间的 ...
- Docker(四):Docker 三剑客之 Docker Compose
前两篇文章我们介绍了 Dockerfile 的使用Docker(二):Dockerfile 使用介绍,我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就 ...
- Docker三剑客之Docker Compose
一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...
- docker——三剑客之Docker swarm
Docker Swarm是Docker官方的三剑客项目之一,提供Docker容器集群服务,是Docker官方对容器云生态进行支持的核心方案.使用它,用户可以将多个Docker主机封装为单个大型的虚拟D ...
- Docker(六):Docker 三剑客之 Docker Swarm
实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...
- docker——三剑客之Docker Machine
Docker Machine是Docker官方三剑客项目之一,负责使用Docker的第一步,在多种平台上快速安装Docker环境.它支持多种平台,让用户在很短时间内搭建一套Docker主机集群. Ma ...
- Docker三剑客之Docker Machine
一.什么是Docker Machine Docker Machine 是Docker官方编排项目之一,使用go语言编写的,使用不同引擎在多种平台上快速的安装Docker环境,开源地址:https:// ...
随机推荐
- CVTE C/C++开发工程师笔试题(二)
问题描述:打印重复元素 给定一个数组,内容可能包含1到N的整数,N最大为40000,数组可能含有重复的值,且N的取值不定.若只剩余5KB内存可用,请设计函数尽可能快的答应数组中所有重复的元素. voi ...
- MySQL数据库开发常见问题及几点优化!
从一下三个方面考虑: 库表设计 慢 SQL 问题 误操作.程序 bug 时怎么办 一.库表设计 1.1.引擎选择 在 MySQL5.1 中,引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运 ...
- srs之深入浅出看流媒体
本文转载:https://blog.csdn.net/zjqlovell/article/details/50786040 CDN这几年爆炸式增长,带宽提速是根源,而HTTP始终还是那个屌样,因此目前 ...
- vue中提示$index is not defined
今天学习Vue中遇到了一个报错信息:$index is not defined,是我写了个for循环在HTML中,然后是因为版本的问题 下面是解决方法: 原来的是 v-for="person ...
- 数据流图——从软考真题中学画数据流图DFD
文章目录 题目 画顶层图 画0层图 解题技巧 题目 建议将题目复制到word后与此文分屏查看.后面需要多次查看题目. 某高校欲开发一个成绩管理系统,记录并管理所有选修课程的学生的平时成绩和考试成绩, ...
- mysql too many connection 解决
最近的项目用了动态切换数据源起初感觉还好,后来发现每次切换数据库都会创建一个新的连接,这样就导致大量的sleep线程.而mysql的默认sleep时间是28800秒....默认最大连接数为151,这就 ...
- Java的xml与map,与Bean互转
xml与map互转,主要使用dom4j import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j. ...
- python 实现快排序
def q_sort(arr): if len(arr)<2: return arr pivot = arr[0] less = [x for x in arr[1:] if x <= p ...
- 从零开始学java (四)反射
反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. ...
- numpy最值
>>> a = np.arange(9).reshape((3,3))>>> aarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])&g ...