专栏首页米奇爱编程利用Docker Compose快速搭建本地测试环境

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

前言

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

演示环境

$ 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的内容如下:

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系统:

$ 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得到答案:

$ 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版本信息。

$ 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 条评论
登录 后参与评论

相关文章

  • Docker基本操作

    相信看完了我上篇Docker基础原理的读者一定是手痒痒想实际操作以下Docker,下面我会用实际的命令演示一遍Docker的基本操作。本文的操作会包括镜像、容器...

    用户2937493
  • go test 上篇

    Go语言本身集成了轻量级的测试框架,由go test命令和testing包组成。包含单元测试和压力测试,是保证我们编写健壮Golang程序的有效工具。

    用户2937493
  • Redis配置讲解及实战

    Redis是一个开源的内存k-v数据库,同时也可用作缓存,消息队列。支持多种数据类型,如字符串,列表,字典,集合,有序集合。

    用户2937493
  • Docker实战(三)

    我们都知道如果用 docker hub 来做仓库会很慢,因为docker hub 服务器在国外,那么现在有必要讲一下如何搭建一个 docker 私有仓库,并进行...

    张乘辉
  • Docker镜像的基本操作

    若与
  • 【腾讯云的1001种玩法】Docker 部署dotnetcore

    大家一起来学习docker, dotnetcore。Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本...

    屈政斌
  • 安装docker后修改docker文件目录

    docker会下载容器,运行会挂载磁盘,所以我们需要把docker装在大容量的分区。

    Ryan-Miao
  • docker-compose记录

    OvO我是肉排菌呀
  • docker运行容器远程挂载卷

    用户1418372
  • 『中级篇』Docker的收费模式(53)

    PS:从这次开始咱们就开始使用收费版本了,其实也是收费的试用版不用花钱的,就是了解下收费版的优越性,更好层次的了解docker。

    IT故事会

扫码关注云+社区

领取腾讯云代金券