Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用。
Dockerfile 可以让用户管理一个单独的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project
,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等。
可见,一个项目可以由多个服务(容器)关联而成,Compose 面向项目进行管理。
2、docker-compose命令
对于 Compose 来说,大部分命令的对象既可以是项目本身,也可以指定为项目中的服务或者容器。如果没有特别的说明,命令对象将是项目,这意味着项目中所有的服务都会受到命令影响。
你可以通过在命令行运行docker-compose --help看到下面这些信息
;如果需要看某一个具体的命令的使用方式,可以运行
docker-compse command --help,如我们查看up命令的使用格式 docker-compose up --help
[root@iZbp13pwlxqwiu1xxb6szsZ ~]# docker-compose up --help
Builds, (re)creates, starts, and attaches to containers for a service.
Unless they are already running, this command also starts any linked services.
The `docker-compose up` command aggregates the output of each container. When
the command exits, all containers are stopped. Running `docker-compose up -d`
starts the containers in the background and leaves them running.
If there are existing containers for a service, and the service's configuration
or image was changed after the container's creation, `docker-compose up` picks
up the changes by stopping and recreating the containers (preserving mounted
volumes). To prevent Compose from picking up changes, use the `--no-recreate`
flag.
If you want to force Compose to stop and recreate all containers, use the
`--force-recreate` flag.
Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
Options:
-d, --detach Detached mode: Run containers in the background,
print new container names. Incompatible with
--abort-on-container-exit.
--no-color Produce monochrome output.
--quiet-pull Pull without printing progress information
--no-deps Don't start linked services.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
--always-recreate-deps Recreate dependent containers.
Incompatible with --no-recreate.
--no-recreate If containers already exist, don't recreate
them. Incompatible with --force-recreate and -V.
--no-build Don't build an image, even if it's missing.
--no-start Don't start the services after creating them.
--build Build images before starting containers.
--abort-on-container-exit Stops all containers if any container was
stopped. Incompatible with -d.
-t, --timeout TIMEOUT Use this timeout in seconds for container
shutdown when attached or when containers are
already running. (default: 10)
-V, --renew-anon-volumes Recreate anonymous volumes instead of retrieving
data from the previous containers.
--remove-orphans Remove containers for services not defined
in the Compose file.
--exit-code-from SERVICE Return the exit code of the selected service
container. Implies --abort-on-container-exit.
--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
`scale` setting in the Compose file if present.
[root@iZbp13pwlxqwiu1xxb6szsZ ~]#
下面是docker-compose命令及使用
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
指定使用的 Compose 模板文件,默认为 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
验证 Compose 文件格式是否正确,若正确则显示配置信息,若格式错误显示错误原因。
create Create services为一个服务创建容器
This command is deprecated. Use the `up` command with `--no-start` instead.这个命令已被废弃,使用up --no-start命令来代替这个命令
down Stop and remove containers, networks, images, and volumes
此命令将会停止通过up命令所启动的容器并移除容器,网络,镜像,数据卷
(Stops containers and removes containers, networks, volumes, and images created by `up`.)
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 通过发送 SIGKILL 信号来强制停止服务容器。
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 推送服务依赖的镜像到 Docker 镜像仓库
restart Restart services 重启一个运行中的服务
rm Remove stopped containers
删除所有停止状态的服务容器。推荐先执行 docker-compose stop 命令来停止容器。
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 [-f
-f
to specify name and path of one or more Compose filesUse the -f
flag to specify the location of a Compose configuration file.
使用-f标志来指定Compose配置文件的位置。
You can supply multiple -f
configuration files. When you supply multiple files, Compose combines them into a single configuration. Compose builds the configuration in the order you supply the files. Subsequent files override and add to their predecessors.
您可以提供多个-f配置文件。 提供多个文件时,Compose会将它们组合为一个配置。 Compose按照您提供文件的顺序构建配置。 后续文件将覆盖并添加到其前身。
For example, consider this command line:
$ docker-compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db
The docker-compose.yml
file might specify a webapp
service.
docker-compose.yml文件指定了一个webapp服务。
webapp:
image: examples/web
ports:
- "8000:8000"
volumes:
- "/data"
If the docker-compose.admin.yml
also specifies this same service, any matching fields override the previous file. New values, add to the webapp
service configuration.
如果docker-compose.admin.yml也指定了相同的服务,则任何匹配的字段都会覆盖前一个文件。 新值添加到webapp服务配置中。
webapp:
build: .
environment:
- DEBUG=1
Use a -f
with -
(dash) as the filename to read the configuration from stdin
. When stdin
is used all paths in the configuration are relative to the current working directory.
使用带-(破折号)的 -f
作为文件名从stdin
中读取配置。 使用stdin
时,配置中的所有路径都相对于当前工作目录。
The -f
flag is optional. If you don’t provide this flag on the command line, Compose traverses the working directory and its parent directories looking for a docker-compose.yml
and a docker-compose.override.yml
file. You must supply at least the docker-compose.yml
file. If both files are present on the same directory level, Compose combines the two files into a single configuration.
-f标志是可选的。 如果您未在命令行上提供此标志,则Compose将遍历工作目录及其父目录,以查找docker-compose.yml和docker-compose.override.yml文件。 您必须至少提供docker-compose.yml文件。 如果两个文件都位于同一目录级别,则Compose会将两个文件合并为一个配置。
The configuration in the docker-compose.override.yml
file is applied over and in addition to the values in the docker-compose.yml
file.
除了docker-compose.yml文件中的值外,还将应用docker-compose.override.yml文件中的配置。
You can use the -f
flag to specify a path to a Compose file that is not located in the current directory, either from the command line or by setting up a COMPOSE_FILE environment variable in your shell or in an environment file.
您可以使用-f标志通过命令行或通过在Shell或环境文件中设置一个COMPOSE_FILE环境变量,来指定不在当前目录中的Compose文件的路径。
For an example of using the -f
option at the command line, suppose you are running the Compose Rails sample, and have a docker-compose.yml
file in a directory called sandbox/rails
. You can use a command like docker-compose pull to get the postgres image for the db
service from anywhere by using the -f
flag as follows: docker-compose -f ~/sandbox/rails/docker-compose.yml pull db
Here’s the full example:
$ docker-compose -f ~/sandbox/rails/docker-compose.yml pull db
Pulling db (postgres:latest)...
latest: Pulling from library/postgres
ef0380f84d05: Pull complete
50cf91dc1db8: Pull complete
d3add4cd115c: Pull complete
467830d8a616: Pull complete
089b9db7dc57: Pull complete
6fba0a36935c: Pull complete
81ef0e73c953: Pull complete
338a6c4894dc: Pull complete
15853f32f67c: Pull complete
044c83d92898: Pull complete
17301519f133: Pull complete
dcca70822752: Pull complete
cecf11b8ccf3: Pull complete
Digest: sha256:1364924c753d5ff7e2260cd34dc4ba05ebd40ee8193391220be0f9901d4e1651
Status: Downloaded newer image for postgres:latest
-p
to specify a project name 使用-p指定项目名称Each configuration has a project name. If you supply a -p
flag, you can specify a project name. If you don’t specify the flag, Compose uses the current directory name. See also the COMPOSE_PROJECT_NAME environment variable.
每个配置都有一个项目名称。 如果提供-p标志,则可以指定项目名称。 如果您未指定标志,则Compose使用当前目录名称。 另请参见COMPOSE_PROJECT_NAME环境变量。
You can set environment variables for various docker-compose
options, including the -f
and -p
flags.
您可以为各种docker-compose选项设置环境变量,包括-f和-p 标志。
For example, the COMPOSE_FILE environment variable relates to the -f
flag, and COMPOSE_PROJECT_NAME environment variable relates to the -p
flag.
例如,COMPOSE_FILE环境变量与-f标志相关,COMPOSE_PROJECT_NAME环境变量与-p标志相关。
Also, you can set some of these variables in an environment file.