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 容器可以跨主机访问. 不同主机间的 ...
随机推荐
- Jquery仿京东分类导航层简单实现
<script src="/js/jquery-1.11.1.min.js" type="text/javascript"></script& ...
- 关于width与padding
http://blog.csdn.net/yaoyuan_difang/article/details/24735529
- WPF通过代码动态的加载样式
tabitem.SetResourceReference(TabItem.StyleProperty, "mainTabItemStyle"); tabitem.Content = ...
- ssm搭建报错
在搭建ssm框架时候踩得坑:1.对于拦截器url-parttern的设置:第一次设置的是/** 本以为这个是表示拦截所有,没想到这是错误的写法,正确的写法是/ 启动项目不会报错,但是会出现404 ...
- 关于限制DHCP服务器广播的另类方法
0. 引言 如图,家里有两台计算机,分别处于不同房间.其中一台PC1能够与光猫直连:另外一台PC2较远,考虑到光猫有WLAN功能,便使用了一个路由器无线桥接到光猫,然后有线连接到PC2(均与光猫处于同 ...
- CentOS 6.2下搭建Web服务器
1Centos 6.2下搭建web服务器 如今,Linux在Web应用越来越广,许多企业都采用Linux来搭建Web服务器,这样即节省了购买正版软件的费用,而且还能够提高服务器的安全性. 之前我们介绍 ...
- OpenWRT 恢复出厂设置命令
如果通过无线或者有线口无法连接到router,可以用恢复某些设置重新设置路由器. 1. 开机,等着一个工作灯亮的时候立即按下rest键2秒,然后就开始拼命闪烁,很好现在进入failsafe模式了. 2 ...
- LINUX 笔记-文件名的匹配
特殊的匹配符号: * 匹配文件名中的任何字符串,包括空字符串 ? 匹配文件名中的任何单个字符串 [...] 匹配[]中包含的任何字符 [!...] 匹配[]中非感吧号!之后的字符
- R学习笔记 第二篇:矩阵、数组和列表
向量是一维的,只有长度(行),没有其他维度.R中存在更高维度的对象,他们是矩阵,数据框,数组.这些对象的下标都是使用中括号[],第一个维度是row,第二个维度是column,依次类推,[row,col ...
- 算法改进 | java语言中判断素数
参考文章:http://blog.csdn.net/kp_liu/article/details/37569507 http://blog.csdn.net/huang_miao_xin/articl ...