docker-compose 簡介以及常用命令
什麼是docker-compose?
在 docker - 部署一個複雜的springboot服務 該文中,我們部署了一個“複雜”的springboot服務,實現了一個計數服務。通過這次部署操作,我們瞭解到部署多容器的APP至少需要經過以下幾個步驟:
(1)寫Dockerfile構建映象或者從docker registry中拉取映象
(2)構建多個容器
(3)管理這些容器(啟動、停止等)
可見,如果APP涉及到的容器很多,要管理這些容器是比較複雜的,至少命令列敲到想哭。那麼,有沒有一個工具,幫我們批處理這些容器呢?
docker-compose是docker公司推出的一個服務編排工具,換句話說就是一個批處理容器的工具。該工具可以通過yml檔案定義多容器的應用,並建立和管理這些容器。
安裝docker-compose
在文章 安裝 docker-compose 中,已經詳細介紹了compose的幾種安裝方法,可以參考安裝。
docker-compose.yml
docker-compose.yml是compose的預設的指令碼名字,在執行compose命令構建的時候,如果不指定檔名,將會預設使用docker-compose.yml檔案,和Dockerfile檔案類似。
docker-compose.yml是有版本的,現在最新版本是v3版本,v1版本不推薦使用,v2和v3是可相容的,而且區別不是很大。這裡需要提到一點區別,v2只能用於多個容器部署在一個宿主主機,而v3可以叢集方式部署在多個宿主主機(swarm)。另外,不同的compose檔案版本對docker的版本是有要求的。如果有興趣,可以在docker的官網中詳細瞭解,版本間的區別連結的地址。我們採用官方推薦的v3版本學習docker-compose。
在compose中,主要有四個基本節點: version、services、networks、volumes。是不是很熟悉,其實compose就是使用指令碼方式代替我們之前通過命令列啟動每個服務。下面是一個基本的compose指令碼。
version: '3'
services:
springboot-docker:
image: myimage:1.0
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
networks:
- mybridge
volumes:
- mydata:/app
networks:
mybridge:
driver: bridge
volumes:
mydata:
本文將通過一個簡單的 docker-compose 檔案,熟悉常用的docker-compose命令。docker-compose.yml檔案存放在dockercompose資料夾中。
version: '3'
services:
busybox:
image: busybox
restart: always
command: /bin/echo
redis:
image: redis:4.0.11
ports:
- 6379:6379
volumes:
- /data/redis:/data
restart: always
command: redis-server
我們檢視 docker-compose 有多少種命令,有個大概的瞭解。docker-compose命令即可檢視。
[root@localhost dockercompose]# docker-compose
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 和 -p
-f :指定檔案。在執行up命令啟動容器時,如果沒有指定docker-compose.yml檔案,將會讀取當前目錄下的docker-compose.yml檔案
-p:啟動後的服務的名字預設情況下采用compose.yml檔案所在的目錄+服務名+副本數命名,例如上面的例子的名字為: dockercompose_redis_1 。如果指定-p,則會用專案名代替該檔案目錄名。
下面開始介紹一些常用的docker-compose命令。
create/start/up/stop/kill/rm/restart/down
docker-compose create | 建立所有的服務 |
docker-compose start | 啟動被停止或未啟動的服務 |
docker-compose up | 建立所有服務並且啟動服務,即同時執行了create和start命令 |
docker-compose stop | 停止所有服務 |
docker-compose kill | 強行停止所有服務 |
docker-compose rm | 刪除停止的服務 |
docker-compose restart | 重啟所有服務) |
docker-compose down | 停止、刪除所有的服務以及網路、映象 |
其中,up命令啟動所有的服務時,如果沒有使用-d命令,會在前臺啟動所有的服務,前臺視窗將列印服務的啟動日誌。建議通過 -d 啟動。例如通過up啟動上面的redis服務。
[root@localhost dockercompose]# docker-compose up -d
Creating network "dockercompose_default" with the default driver
然後可以通過 docker-compose logs -f 獲取輸出的日誌
ps
該命令可以檢視容器列表,可以獲取到容器的簡單資訊。
[root@localhost dockercompose]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------
dockercompose_busybox_1 /bin/echo Up
dockercompose_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
上面的資訊可以得到容器名、啟動命令、容器狀態以及埠的對映情況。
scale
該命令用於擴充套件容器,為service設定多個容器(副本)。我們為上面的busybox服務擴充套件為2個副本。
[root@localhost dockercompose]# docker-compose scale busybox=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting dockercompose_busybox_1 ... done
Creating dockercompose_busybox_2 ... done
稍等一下,通過ps命令可以看到已經啟動了兩個busybox服務。
前面的輸出中,提示該命令已經過期,使用--scale選項指定副本。
我們down了服務後,重新使用up命令並指定副本數
[root@localhost dockercompose]# docker-compose up -d --scale busybox=2
[root@localhost dockercompose]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------
dockercompose_busybox_1 /bin/echo Restarting
dockercompose_busybox_2 /bin/echo Restarting
dockercompose_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
不明白為什麼一直都是restarting。。。。。忽略它吧。。