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 三剑客之 Compose的更多相关文章
- Docker三剑客之compose
简介 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排.从功能上看,跟 OpenStack 中的 Heat 十分类似.其代码目前在 https://g ...
- Docker三剑客之 Compose
简介 Docker-Compose 是 Docker 的一种编排服务,是一个用于在 Docker 上定义并运行复杂应用的工具,可以让用户在集群中部署分布式应用. 通过 Docker-Compose 用 ...
- Docker三剑客之Docker Compose
一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...
- 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三剑客: Docker-Machine Docker Machine is a tool that lets you install Docker Engine on virtual ho ...
- Docker(六):Docker 三剑客之 Docker Swarm
实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...
- Docker 三剑客之 Docker Swarm(基于 overlay 组网通信)
相关文章:Docker 三剑客之 Docker Swarm 这一篇主要是对 Docker Swarm 的完善,增加基于 overlay 组网通信,以便 Docker 容器可以跨主机访问. 不同主机间的 ...
随机推荐
- JAVA提高四:反射基本应用
在前面一节<http://www.cnblogs.com/pony1223/p/7659210.html>,我们学习了JAVA的反射的相关知识,那么本节我们对前面所学习的知识做一个应用相关 ...
- Github Page--CSDN新人的第二选择
我也是个CSDN新人,使用的CSDN的初衷应该和众人类似,就是想总结下平时的学习成果,或者一些想法. CSDN好的地方: 书写界面简洁,支持markdown语法 人还算多,也比较年轻 相对较活跃 内容 ...
- 【特效】hover效果之十字动画
效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/3101.htm html: <div class="wrap&q ...
- VB6文件操作自定义函数合集之一
'--与文件及文件夹操作相关的函数 '--必须引用FSO的ACTIVE OBJECT Dim strList As String '--列表串,返回文件列表 '================ '-- ...
- 通过xrdp服务实现windows远程桌面连接树莓派
如题:通过xrdp服务实现windows远程桌面连接树莓派 受同学影响,最近接触到了树莓派,又加上自己技痒想试一下这个小东西究竟能做什么,所以开始了树莓派学习之旅. 正题开始-xrdp实现window ...
- Volley图片加载并加入缓存处理(转自http://blog.csdn.net/viewhandkownhealth/article/details/50957024)
直接上代码 两种方式 ImageView 和NetworkImageView 如有问题或者好的建议.意见 欢迎大家加入技术群(群号: 387648673 ) 先自定义全局Application 获取 ...
- node+vue进阶【课程学习系统项目实战详细讲解】打通前后端全栈开发(1):创建项目,完成登录功能
第一章 建议学习时间8小时·分两次学习 总项目预计10章 学习方式:详细阅读,并手动实现相关代码(如果没有node和vue基础,请学习前面的vue和node基础博客[共10章]) 视频教程地 ...
- Angular5.0.0新特性
文章来自官网部分翻译https://blog.angular.io/version-5-0-0-of-angular-now-available-37e414935ced Angular5.0.0版本 ...
- Angular通信$q服务和promise对象
promise 约定(promise)是一个对象,表示在未来时间点会发生的某件事情,约定可以是三种状态之一:等待.完成或拒绝.约定将从等待状态开始,然后可以转换为完成或者拒绝状态,一旦约定完成或者被拒 ...
- angular指令中的preLink函数和postLink函数
指令模板选项有complie和link两个字段,两者之间存在如下关系: 当compile字段存在时,link字段将被忽略,compile函数的返回值将作为link字段. 当compile不存在,lin ...