当前位置:   article > 正文

Docker Compose CLI(Compose命令行)选项概述和帮助

docker compose cli

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用。

Dockerfile 可以让用户管理一个单独的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等。

1、首先介绍几个术语

  • 服务(service):一个应用容器,实际上可以运行多个相同镜像的实例。
  • 项目(project):由一组关联的应用容器组成的一个完整业务单元。

可见,一个项目可以由多个服务(容器)关联而成,Compose 面向项目进行管理。

2、docker-compose命令

       对于 Compose 来说,大部分命令的对象既可以是项目本身,也可以指定为项目中的服务或者容器。如果没有特别的说明,命令对象将是项目,这意味着项目中所有的服务都会受到命令影响。

你可以通过在命令行运行docker-compose --help看到下面这些信息;如果需要看某一个具体的命令的使用方式,可以运行

docker-compse command --help,如我们查看up命令的使用格式 docker-compose up --help

  1. [root@iZbp13pwlxqwiu1xxb6szsZ ~]# docker-compose up --help
  2. Builds, (re)creates, starts, and attaches to containers for a service.
  3. Unless they are already running, this command also starts any linked services.
  4. The `docker-compose up` command aggregates the output of each container. When
  5. the command exits, all containers are stopped. Running `docker-compose up -d`
  6. starts the containers in the background and leaves them running.
  7. If there are existing containers for a service, and the service's configuration
  8. or image was changed after the container's creation, `docker-compose up` picks
  9. up the changes by stopping and recreating the containers (preserving mounted
  10. volumes). To prevent Compose from picking up changes, use the `--no-recreate`
  11. flag.
  12. If you want to force Compose to stop and recreate all containers, use the
  13. `--force-recreate` flag.
  14. Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
  15. Options:
  16. -d, --detach Detached mode: Run containers in the background,
  17. print new container names. Incompatible with
  18. --abort-on-container-exit.
  19. --no-color Produce monochrome output.
  20. --quiet-pull Pull without printing progress information
  21. --no-deps Don't start linked services.
  22. --force-recreate Recreate containers even if their configuration
  23. and image haven't changed.
  24. --always-recreate-deps Recreate dependent containers.
  25. Incompatible with --no-recreate.
  26. --no-recreate If containers already exist, don't recreate
  27. them. Incompatible with --force-recreate and -V.
  28. --no-build Don't build an image, even if it's missing.
  29. --no-start Don't start the services after creating them.
  30. --build Build images before starting containers.
  31. --abort-on-container-exit Stops all containers if any container was
  32. stopped. Incompatible with -d.
  33. -t, --timeout TIMEOUT Use this timeout in seconds for container
  34. shutdown when attached or when containers are
  35. already running. (default: 10)
  36. -V, --renew-anon-volumes Recreate anonymous volumes instead of retrieving
  37. data from the previous containers.
  38. --remove-orphans Remove containers for services not defined
  39. in the Compose file.
  40. --exit-code-from SERVICE Return the exit code of the selected service
  41. container. Implies --abort-on-container-exit.
  42. --scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
  43. `scale` setting in the Compose file if present.
  44. [root@iZbp13pwlxqwiu1xxb6szsZ ~]#

下面是docker-compose命令及使用 

  1. Define and run multi-container applications with Docker.
  2. Usage:
  3. docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  4. docker-compose -h|--help
  5. Options:
  6. -f, --file FILE Specify an alternate compose file
  7. (default: docker-compose.yml)
  8. 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定
  9. -p, --project-name NAME Specify an alternate project name
  10. (default: directory name)
  11. 指定项目名称,默认将使用所在目录名称作为项目名
  12. --verbose Show more output输出更多调试信息
  13. --log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  14. --no-ansi Do not print ANSI control characters
  15. -v, --version Print version and exit打印版本并退出
  16. -H, --host HOST Daemon socket to connect to
  17. --tls Use TLS; implied by --tlsverify
  18. --tlscacert CA_PATH Trust certs signed only by this CA
  19. --tlscert CLIENT_CERT_PATH Path to TLS certificate file
  20. --tlskey TLS_KEY_PATH Path to TLS key file
  21. --tlsverify Use TLS and verify the remote
  22. --skip-hostname-check Don't check the daemon's hostname against the
  23. name specified in the client certificate
  24. --project-directory PATH Specify an alternate working directory
  25. (default: the path of the Compose file)
  26. --compatibility If set, Compose will attempt to convert deploy
  27. keys in v3 files to their non-Swarm equivalent
  28. Commands:
  29. build Build or rebuild services构建或重新构建项目中的服务容器
  30. bundle Generate a Docker bundle from the Compose file
  31. config Validate and view the Compose file
  32. 验证 Compose 文件格式是否正确,若正确则显示配置信息,若格式错误显示错误原因。
  33. create Create services为一个服务创建容器
  34. This command is deprecated. Use the `up` command with `--no-start` instead.这个命令已被废弃,使用up --no-start命令来代替这个命令
  35. down Stop and remove containers, networks, images, and volumes
  36. 此命令将会停止通过up命令所启动的容器并移除容器,网络,镜像,数据卷
  37. (Stops containers and removes containers, networks, volumes, and images created by `up`.)
  38. events Receive real time events from containers
  39. exec Execute a command in a running container 在一个运行的容器中执行一个命令
  40. help Get help on a command
  41. images List images 列出被创建的容器所使用的镜像
  42. kill Kill containers 通过发送 SIGKILL 信号来强制停止服务容器。
  43. logs View output from containers 查看服务容器的输出
  44. pause Pause services 暂停一个服务容器
  45. port Print the public port for a port binding 打印某个容器端口所映射的公共端口
  46. ps List containers 列出项目中目前的所有容器
  47. pull Pull service images 拉取服务依赖的镜像
  48. push Push service images 推送服务依赖的镜像到 Docker 镜像仓库
  49. restart Restart services 重启一个运行中的服务
  50. rm Remove stopped containers
  51. 删除所有停止状态的服务容器。推荐先执行 docker-compose stop 命令来停止容器。
  52. run Run a one-off command
  53. scale Set number of containers for a service 设置指定服务运行的容器个数
  54. start Start services 启动一个已经存在的服务容器
  55. stop Stop services 停止已经处于运行状态的容器,但不删除它。
  56. top Display the running processes 查看各个服务容器内运行的进程
  57. unpause Unpause services 恢复处于暂停状态中的服务
  58. up Create and start containers
  59. 自动完成 构建镜像,重新创建/创建服务,启动服务,并关联服务相关容器的一系列操作。链接的服务都将会被自动启动,除非已经处于运行状态。
  60. version Show the Docker-Compose version information

你可以通过docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] 命令管理管理和管理多个在docker容器里的服务。

3、Use -f to specify name and path of one or more Compose files

Use the -f flag to specify the location of a Compose configuration file.

使用-f标志来指定Compose配置文件的位置。

3.1、Specifying multiple Compose files 指定多个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服务。

  1. webapp:
  2. image: examples/web
  3. ports:
  4. - "8000:8000"
  5. volumes:
  6. - "/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服务配置中。

  1. webapp:
  2. build: .
  3. environment:
  4. - 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文件中的配置。

 

3.2、Specifying a path to a single Compose file 指定单个Compose文件的路径

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:

  1. $ docker-compose -f ~/sandbox/rails/docker-compose.yml pull db
  2. Pulling db (postgres:latest)...
  3. latest: Pulling from library/postgres
  4. ef0380f84d05: Pull complete
  5. 50cf91dc1db8: Pull complete
  6. d3add4cd115c: Pull complete
  7. 467830d8a616: Pull complete
  8. 089b9db7dc57: Pull complete
  9. 6fba0a36935c: Pull complete
  10. 81ef0e73c953: Pull complete
  11. 338a6c4894dc: Pull complete
  12. 15853f32f67c: Pull complete
  13. 044c83d92898: Pull complete
  14. 17301519f133: Pull complete
  15. dcca70822752: Pull complete
  16. cecf11b8ccf3: Pull complete
  17. Digest: sha256:1364924c753d5ff7e2260cd34dc4ba05ebd40ee8193391220be0f9901d4e1651
  18. Status: Downloaded newer image for postgres:latest

4、Use -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环境变量。

5、Set up environment variables 设置环境变量

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.

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/74116
推荐阅读
相关标签
  

闽ICP备14008679号