部署一個應用可能須要用到多個Docker容器,好比MySQL,Redis,Nginx等,單獨的去管理每一個容器可能會比較麻煩。Docker Compose能夠經過一個yml文件來統一管理這些容器,能夠極大簡化咱們的應用部署過程。這裏以部署WordPress爲例,介紹如何使用Docker Compose去統一管理多個Docker容器。mysql
先來看看不用Docker Compose時部署WordPress的步驟。redis
Docker拉取MySQL鏡像:sql
docker pull mysql:5.7.25
建立MySQL容器:docker
docker run -d --name mysql -v mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpres s -p 3306:3306 mysql:5.7.25
Docker拉取WordPress鏡像:vim
docker pull wordpress
建立WordPress容器:網絡
docker run -d -p 8080:80 --link mysql -e WORDPRESS_DB_HOST=mysql:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=123456 -e WORDPRESS_DB_NAME=wordpress wordpress
查看是否成功:app
接下來看一下如何使用Docker Compose部署WordPress。在此以前,咱們須要先刪除上面建立的兩個容器,而後安裝Docker Compose,安裝教程:https://docs.docker.com/compose/install/。socket
查看是否安裝成功:wordpress
編寫一個docker-compose.yml:ui
vim docker-compose.yml
內容以下:
version: '3'
services:
wordpress:
image: wordpress
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
networks:
- my-bridge
mysql:
image: mysql:5.7.25
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wordpress
volumes:
- mysql-data:/var/lib/mysql
networks:
- my-bridge
volumes:
mysql-data:
networks:
my-bridge:
driver: bridge
而後執行命令docker-compose up -d運行:
查看WordPress是否部署成功:
上面例子中咱們經過一個docker-compose.yml文件,運行docker-compose up命令一次性建立了兩個容器:
docker-compose.yml通常包含三個部分:services、networks和volumes。
一個service表明一個container,能夠從Docker Hub的image建立,也能夠經過本地的dockerfile建立的image來建立。
service相似於docker run,docker run支持的選項 service也能夠用。
舉兩個例子:
基於鏡像:
services:
db:
image: mysql:5.7.25
volumes:
- mysql-data:/var/lib/mysql
networks:
- my-bridge
相似於命令:
docker run -d --network my-bridge -v mysql-data:/var/lib/mysql mysql:5.7.25
基於dockerfile:
services:
worker:
build: ./worker
links:
- db
- redis
networks:
- my-bridge
基於當前目錄下worker/文件夾裏的dockerfile構建container。
一個networks的例子:
networks:
my-bridge:
driver: bridge
等價於:
docker network create -d bridge my-bridge
一個volumes的例子:
volumes:
mysql-data:
等價於
docker volume create mysql-data
如今回頭看看剛剛的yml文件:
version: 3表示基於第三版本docker-compose.yml語法構建,而後建立了兩個容器wordpress和mysql,一個volume——mysql-data,一個network,名稱爲my-bridge,採用bridge網絡模式。
在wordpress的定義中,使用image指定了基於wordpress鏡像構建,ports進行端口映射,environment配置了wordpress鏡像支持的環境變量,指定網絡爲my-bridge。
mysql的定義和wordpress相似,再也不贅述。
更多docker-compose.yml支持的命令能夠參考:https://www.jianshu.com/p/2217cfed29d7。
看看docker-compose支持哪些命令:
[root@mrbird vagrant]# 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 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
查看運行中的容器:
docker-compose ps
中止:
docker-compose stop
啓動:
docker-compose start
中止並刪除(刪除建立的network,volume,container):
docker-compose down