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, fabiohell-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 三剑客之 Compose的更多相关文章

  1. Docker三剑客之compose

    简介 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排.从功能上看,跟 OpenStack 中的 Heat 十分类似.其代码目前在 https://g ...

  2. Docker三剑客之 Compose

    简介 Docker-Compose 是 Docker 的一种编排服务,是一个用于在 Docker 上定义并运行复杂应用的工具,可以让用户在集群中部署分布式应用. 通过 Docker-Compose 用 ...

  3. Docker三剑客之Docker Compose

    一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...

  4. Docker 三剑客之 Docker Swarm

    上一篇:Docker 三剑客之 Docker Compose 阅读目录: Docker Machine 创建 Docker 主机 Docker Swarm 配置集群节点 Docker Service ...

  5. Docker(五):Docker 三剑客之 Docker Machine

    上篇文章Docker(四):Docker 三剑客之 Docker Compose介绍了 Docker Compose,这篇文章我们来了解 Docker Machine . Docker Machine ...

  6. Docker三剑客之Docker Swarm

    一.什么是Docker Swarm Swarm是Docker公司推出的用来管理docker集群的平台,几乎全部用GO语言来完成的开发的,代码开源在https://github.com/docker/s ...

  7. Docker 三剑客

    Docker三剑客: Docker-Machine Docker Machine is a tool that lets you install Docker Engine on virtual ho ...

  8. Docker(六):Docker 三剑客之 Docker Swarm

    实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...

  9. Docker 三剑客之 Docker Swarm(基于 overlay 组网通信)

    相关文章:Docker 三剑客之 Docker Swarm 这一篇主要是对 Docker Swarm 的完善,增加基于 overlay 组网通信,以便 Docker 容器可以跨主机访问. 不同主机间的 ...

随机推荐

  1. Mysql的二进制安装和基础入门操作

    前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...

  2. jquery系列教程4-事件操作全解

    点击打开: jquery系列教程1-选择器全解 jquery系列教程2-style样式操作全解 jquery系列教程3-DOM操作全解 jquery系列教程4-事件操作全解 jquery系列教程5-动 ...

  3. Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP

    Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP 项目截图     这是我的目录结构 五步使用RxJava+Retrofit2+Okhttp+RxCache 第一步 ...

  4. SAP 动态设置 GUI STATUS 灰色不可用 或者隐藏(转)

    http://blog.sina.com.cn/s/blog_66110f6201017rul.html 有时候需要根据用户的权限或者是操作动态设置gui状态上的某些按钮的可用和不可用.   1.先定 ...

  5. Install Ubuntu On Windows10(win10上安装linux系统)

    一.准备: 硬件:U盘 软件:ultraiso.Ubuntu镜像文件 二.安装linux: 1.Ubuntu官网(http://www.ubuntu.org.cn/download/alternati ...

  6. http服务详解(1)——一次完整的http服务请求处理过程

    前言:要熟练掌握一个服务,首先需要非常了解这个服务的工作过程,这篇就详细解释了http服务的请求处理过程. 一次完整的http请求处理过程 (1)流程图 (2)过程详解 0.DNS域名解析:递归查询. ...

  7. 学web前端开发写给新手的建议,超实用!

    01 前面的话 如今我们使用的互联网,客户端与服务器端的交互无时无刻不在发生.比如我们在浏览器打开网页,浏览器就是客户端,将网页数据发过来的也就是服务器.其实服务器,并没有什么特别的,也就是一台昼夜不 ...

  8. 【NOIP2016提高组】 Day1 T3 换教室

    题目链接:https://www.luogu.org/problemnew/show/P1850 此题正解为dp. 我们先用floyd处理出任意两个教室之间的距离,用dis[i][j]表示. 用f[i ...

  9. JQuery-基础学习1

    1)JQuery语法 jquery语法是为HTML元素的选取编制,可以对元素执行某些操作. 基础语法是:$(selector).action() 美元符号定义JQuery 选择符(selector)& ...

  10. 使用脚本监控windows服务的方法

    以下脚本可监控某一个windows服务,发现其停止就立即重启之. @echo off rem 定义循环间隔时间和监测的服务: set secs=60 set srvname="NetWin ...