利用Docker Compose快速搭建本地测试环境

2019-09-11 14:39:29 浏览数 (1)

前言

Compose是一个定义和运行多个Docker应用的工具,用一个YAML(dockder-compose.yml)文件就能配置我们的应用。然后用一个简单命令就能启动所有的服务。Compose编排Docker服务的优势是在单机测试场景,因为Compose的安装简单,开箱即用,yaml的定义也复用了Dockerfile的语法。但是集群中容器编排服务还是推荐K8S或者Mesos Marathon这样的编排调度系统。

演示环境

代码语言:javascript复制
$ uname -a
Darwin 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

$ docker version
Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        6247962
 Built:             Sun Feb 10 04:12:39 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

示例

老规矩,我会先用一个示例来引入接下来要讲解的内容。下面我将演示用Docker Compose一键运行集中式日志系统ELK。ELK是一套日志收集方案,是三款软件产品的名字缩写,Elasticsearch,Logstash 和 Kibana。

ELK的docker-compose.yml 来自github仓库docker-elk。docker-compose.yml的内容如下:

代码语言:javascript复制
version: '2'

services:

  elasticsearch:
    build:
      context: elasticsearch/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      ES_JAVA_OPTS: "-Xmx256m -Xms256m"
      ELASTIC_PASSWORD: changeme
    networks:
      - elk

  logstash:
    build:
      context: logstash/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - ./logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml:ro
      - ./logstash/pipeline:/usr/share/logstash/pipeline:ro
    ports:
      - "5000:5000"
      - "9600:9600"
    environment:
      LS_JAVA_OPTS: "-Xmx256m -Xms256m"
    networks:
      - elk
    depends_on:
      - elasticsearch

  kibana:
    build:
      context: kibana/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml:ro
    ports:
      - "5601:5601"
    networks:
      - elk
    depends_on:
      - elasticsearch

networks:

  elk:
    driver: bridge

进入到docker-compose.yml目录,通过docker-compose up -d 这条命令就能启动一个本地环境的ELK系统:

代码语言:javascript复制
$ docker-compose up -d
Building kibana
Step 1/2 : ARG ELK_VERSION
Step 2/2 : FROM docker.elastic.co/kibana/kibana:${ELK_VERSION}
7.1.1: Pulling from kibana/kibana
8ba884070f61: Already exists
3dd92e31d54b: Downloading [====================>                              ]  18.31MB/44.81MB
47d37f21fc3a: Pull complete
97eef8537832: Pull complete
151d3d8996bb: Pull complete
003e03312113: Pull complete
0fbb6a4413a0: Pull complete
c455cf619e40: Pull complete
Digest: sha256:fbf558c6df18500f00ab9c1e1ce2d566ad2c1800d023106e4a4a85274b0d40cd
Status: Downloaded newer image for docker.elastic.co/kibana/kibana:7.1.1
 ---> 67f17df6ca3e
Successfully built 67f17df6ca3e
Successfully tagged docker-elk_kibana:latest
WARNING: Image for service kibana was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker-elk_elasticsearch_1 ... done
Creating docker-elk_kibana_1        ... done
Creating docker-elk_logstash_1      ... done
Attaching to docker-elk_elasticsearch_1, docker-elk_kibana_1, docker-elk_logstash_1
$ docker-compose ps
           Name                         Command               State                            Ports                          
------------------------------------------------------------------------------------------------------------------------------
docker-elk_elasticsearch_1   /usr/local/bin/docker-entr ...   Up      0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp          
docker-elk_kibana_1          /usr/local/bin/kibana-docker     Up      0.0.0.0:5601->5601/tcp                                  
docker-elk_logstash_1        /usr/local/bin/docker-entr ...   Up      0.0.0.0:5000->5000/tcp, 5044/tcp, 0.0.0.0:9600->9600/tcp

访问kibana页面:

如果传统物理进程搭建ELK系统,首先要下载和安装Java JDK,下载ELK的版本,然后安装Elasticsearch,Logstash 和 Kibana。这整一套下来没有半天根本搞不定,现在集成到docker-compose只需要一条命令docker-compose up就能运行起来。所以利用docker-compose可以快速搭建本地测试环境,大大提高开发效率。相对于使用docker run这样的方式一个个起停容器,docker-compose提供了一个容器编排的服务,定义一组容器的起停依赖关系。也可以通过docker-compose stop logstash 来停止单一的logstash等服务。

详细说明

通过上面的例子我们已经掌握了Docker Comopose的起停等简单用法,那么Docker Compose一共有多少种用法呢,我们可以通过--help得到答案:

代码语言:javascript复制
$ 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
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -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
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent

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
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull 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
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

-f 指定compose文件,不是必须项,如果没有指定就变量当前路径或者父目录的docker-compose.yml文件。

-p 指定项目名称, 默认是目录名,例如例子中默认的docker-elk。

-v 显示Docker Compose版本。

--tls* 是加密证书相关选项。本机环境一般用不上。

build 创建容器服务,例如用Dockerfile build镜像。

ps 显示所有容器。

pull 拉取服务镜像。

push 推送服务镜像。

start 启动服务。

stop 停止服务。

up 创建和启动容器服务。相当于build(or pull)和start。

version 显示Docker Compose版本信息。

代码语言:javascript复制
$ docker-compose version
docker-compose version 1.23.2, build 1110ad01
docker-py version: 3.6.0
CPython version: 3.6.6
OpenSSL version: OpenSSL 1.1.0h  27 Mar 2018
$ docker-compose stop logstash
Stopping docker-elk_logstash_1 ... done
$ docker-compose ps
           Name                         Command               State                        Ports                     
---------------------------------------------------------------------------------------------------------------------
docker-elk_elasticsearch_1   /usr/local/bin/docker-entr ...   Up       0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp
docker-elk_kibana_1          /usr/local/bin/kibana-docker     Up       0.0.0.0:5601->5601/tcp                        
docker-elk_logstash_1        /usr/local/bin/docker-entr ...   Exit 0      
$ docker-compose start logstash
Starting logstash ... done
$ docker-compose ps
           Name                         Command               State                            Ports                          
------------------------------------------------------------------------------------------------------------------------------
docker-elk_elasticsearch_1   /usr/local/bin/docker-entr ...   Up      0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp          
docker-elk_kibana_1          /usr/local/bin/kibana-docker     Up      0.0.0.0:5601->5601/tcp                                  
docker-elk_logstash_1        /usr/local/bin/docker-entr ...   Up      0.0.0.0:5000->5000/tcp, 5044/tcp, 0.0.0.0:9600->9600/tcp

总结

文章用一键运行ELK系统演示了Docker Compose搭建环境的高效性,然后详细介绍了Docker Compose的大部分选型的含义和用法。

参考

https://docs.docker.com/compose/overview/

<!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures} -->

0 人点赞