跳转至

Docker 编排工具 docker-compose


2016-09-30 by dongnan

简介

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责对 Docker 容器应用的快速编排(部署)。

我们知道使用一个 Dockerfile 模板文件,可以让用户很方便的定义一个单独的应用容器。然而在日常工作中经常会碰到需要多个容器相互配合来完成某项任务的情况。

例如要实现一个 Web 项目,除了 Web 服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。

Compose 恰好满足了这样的需求。它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目。

使用

首先介绍几个术语。

  • 服务 ( service ):一个应用容器,也可以是运行多个相同镜像的实例。
  • 项目 ( project ):由一组关联的应用容器组成的一个完整业务单元。

可见,一个项目可以由多个服务(容器)关联而成, Compose 面向项目进行管理

Compose 模板文件

模板文件是使用 Compose 的核心,涉及到的指令关键字也比较多。 大部分指令跟 docker run 相关参数的含义都是类似的。默认的模板文件名称为 docker-compose.yml

安装部署

准备工作

docker-compose 依赖 docker-engine ,所以需要先安装 docker ,可以参考官方文档

安装 compose

# 下载到本地
curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

# 赋予可执行权限
chmod +x /usr/local/bin/docker-compose

验证 compose

# 注意请使用新版本代替示例中的版本。
docker-compose --version
docker-compose version 1.8.0, build f3628c7

扩展

docker-compose 部署 harbor 仓库

docker-compose常用命令

命令帮助

docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pulls service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

举个栗子

停止服务

docker-compose stop
Stopping deploy_jobservice_1 ... done
Stopping deploy_proxy_1 ... done
Stopping deploy_registry_1 ... done
Stopping deploy_ui_1 ... done
Stopping deploy_mysql_1 ... done
Stopping deploy_log_1 ... done

启动服务

docker-compose start
Starting log ... done
Starting ui ... done
Starting mysql ... done
Starting jobservice ... done
Starting registry ... done
Starting proxy ... done

销毁服务

#停止/移除,容器,网络,镜像,存储卷
docker-compose down
Stopping deploy_jobservice_1 ... done
Stopping deploy_proxy_1 ... done
Stopping deploy_registry_1 ... done
Stopping deploy_ui_1 ... done
Stopping deploy_mysql_1 ... done
Stopping deploy_log_1 ... done
Removing deploy_jobservice_1 ... done
Removing deploy_proxy_1 ... done
Removing deploy_registry_1 ... done
Removing deploy_ui_1 ... done
Removing deploy_mysql_1 ... done
Removing deploy_log_1 ... done
Removing network deploy_default

创建服务

docker-compose up -d
Creating network "deploy_default" with the default driver
Creating deploy_log_1
Creating deploy_registry_1
Creating deploy_mysql_1
Creating deploy_ui_1
Creating deploy_jobservice_1
Creating deploy_proxy_1

列出容器

docker-compose ps
       Name                      Command                 State                       Ports                  
------------------------------------------------------------------------------------------------------------
deploy_jobservice_1   /go/bin/harbor_jobservice        Up                                                    
deploy_log_1          /bin/sh -c cron && rsyslogd -n   Up           0.0.0.0:1514->514/tcp                    
deploy_mysql_1        docker-entrypoint.sh mysqld      Up           3306/tcp                                
deploy_proxy_1        nginx -g daemon off;             Up           0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
deploy_registry_1     /entrypoint.sh serve /etc/ ...   Restarting   5000/tcp, 0.0.0.0:5001->5001/tcp        
deploy_ui_1           /go/bin/harbor_ui                Up           80/tcp

docker-compose的volume

docker run 一样保存在 /var/lib/docker/volumes/ 目录。

docker inspect  r-ywwd-nginx
#...省略
        "Mounts": [
            {
                "Type": "volume",
                "Name": "compose_web_data",
                "Source": "/var/lib/docker/volumes/compose_web_data/_data",
                "Destination": "/var/www/html",
                "Driver": "local",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            }
        ],
#...省略

参考

回到页面顶部