一、Docker Compose 简介
Docker Compose是什么?
-
Docker Compose是一个能一次性定义和管理多个Docker容器的工具。
-
详细地说:
- Compose中定义和启动的每一个容器都相当于一个服务(service)
- Compose中能定义和启动多个服务,且它们之间通常具有协同关系
-
管理方式:
- 使用YAML文件来配置我们应用程序的服务。
- 使用单个命令(docker-compose up),就可以创建并启动配置文件中配置的所有服务。
Docker Compose 工作原理
Docker Compose安装
-
Docker for Mac与Docker for Windows自带docker-compose
-
Linux下需要单独安装:
- 第一步:sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose- ( u n a m e − s ) − (uname -s)- (uname−s)−(uname -m) -o /usr/local/bin/docker-compose
- 第二步:sudo chmod +x /usr/local/bin/docker-compose
终端中使用docker-compose --version查看安装的版本
这里示例安装版本是1.21.2,很可能您看到这里时,已经出现更新的版本,因此建议换成最新版本。查看最新版本。
Docker Compose CLI
-
利用docker-compose --help查看或者查看官方文档
-
对比后会发现:Docker Compose CLI的很多命令的功能和Docker Client CLI是相似的。最主要的区别就是前者能一次性运行管理多个容器,后者只能一次管理一个。
C:\Users\lenovo>docker-compose --version
docker-compose version 1.27.4, build 40524192
C:\Users\lenovo>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)
-c, --context NAME Specify a context 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 (DEPRECATED)
--env-file PATH Specify an alternate environment file
Commands:
build Build or rebuild services
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 version information and quit
C:\Users\lenovo>
二、了解 Docker Compose File
Docker Compose File版本
- Docker Compose File 有多个版本,基本是向后兼容的,但也有极个别配置项高版本中没有。
- 在docker-compose.yml一开始就需要利用version关键词标明当前file使用的版本
Docker Compose File TOP配置参数概览
Docker Compose File 顶级配置项:
- version:指定Docker Compose File版本号
- services:定义多个服务并配置启动参数
- volumes:声明或创建在多个服务中共同使用的数据卷对象
- networks:定义在多个服务中共同使用的网络对象
- configs:声明将在本服务中要使用的一些配置文件
- secrets:声明将在本服务中要使用的一些秘钥、密码文件
- x-***:自定义配置。主要用于复用相同的配置。