compose 是一個可定義和運行多個容器的docker容器應用管理工具,使用compose,你能夠經過編寫YAML文件配置你的應用程序或者服務,你能夠建立並運行你的配置,尤爲是在同一宿主主機下面,要運行多個容器,compose是一個不錯的管理工具。nginx
compose的使用通常包括三個步驟git
1.使用Dockerfile定義你的應用運行的環境,讓它能夠在任何地方運行github
2.在docker-compose.yml文件中定義組成你須要運行的服務的環境,docker
3.運行docker-compose up來啓動你的服務bash
如下實驗環境均在Centos操做系統上完成curl
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
安裝以後直接執行會報錯,以下:工具
[root@localhost ~]# /usr/local/bin/docker-compose -bash: /usr/local/bin/docker-compose: Permission denied
緣由是沒有執行權限,賦執行權限便可ui
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 、
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 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
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
url
3.docker-compose.yml文件格式詳解操作系統
一份標準的docker-compose.yml文件應該包含version,services以及networks三個部分,其中最重要的是services和netwotks兩個部分
如下是一份標準的docker-compose.yml配置文件
version: '2' services: App1: image: nginx ports: - "8080:80" networks: - "netName1" volumes: - /opt/conf/:/mnt App2: image: nginx ports: - "8081:80" networks: - "netName1" volumes: - /opt/conf/:/mnt App3: image: nginx ports: - "8082:80" networks: - "netName2" networks: netName1: driver: bridge netName2: driver: bridge
-- image
其中第一行是version
第二行services標籤就是定義服務,在services標籤下的第一個標籤就是定義服務的名稱,image則是指定服務的鏡像名稱或者鏡像ID,若是這個鏡像不存在,docker會嘗試去自動拉取這個鏡像
-- build
服務除了基於指定的鏡像,還能夠基於一份Dockerfile,在使用