利用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/
&nbs