zoukankan      html  css  js  c++  java
  • 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, 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 强制停止项目中的所有服务容器(也可以指定具体的服务)

    参考资料:

  • 相关阅读:
    php7垃圾回收机制
    PHP-FPM
    go main.main之前的准备
    ElementUI MessageBox 弹框
    ElementUI table 样式修改
    ElementUI Dropdown 下拉菜单
    vue+django实现下载文件
    ElementUI 点击展开/隐藏
    vue+django实现websocket连接
    ElementUI 上传文件以及限制
  • 原文地址:https://www.cnblogs.com/xishuai/p/docker-compose.html
Copyright © 2011-2022 走看看