Docker三剑客之Compose-二

来源:互联网 发布:淘宝裸机是什么意思 编辑:程序博客网 时间:2024/06/04 18:37

title: Docker三剑客之Compose(二)——Compose命令详解
date: 2015-12-04 16:01:34
tags: docker

categories: Docker 三剑客

Docker Compose是一个编排多容器分布式部署的工具,提供命令集管理容器化应用的完整开发周期,包括服务构建,启动和停止。


首先看一下Compose命令的格式:

Usage:  docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]

其中options有如下选项:

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)  --x-networking            (EXPERIMENTAL) Use new Docker networking functionality.                            Requires Docker 1.9 or later.  --x-network-driver DRIVER (EXPERIMENTAL) Specify a network driver (default: "bridge").                            Requires Docker 1.9 or later.  --verbose                 Show more output  -v, --version             Print version and exit

首先运行docker-compose命令需要指定服务(service)名称,可以同时指定多个service,也可以不指定,当不指定service名称时,默认对配置中的所有service执行命令。

其中-f标识用于指定Compose的配置文件,可以指定多个,当没有使用-f标识时,默认在项目跟目录及其子目录下寻找docker-compose.ymldocker-compose.override.yml文件,至少需要存在docker-compose.yml文件。

当指定了多个文件时(包括没指定-f但同时存在docker-compose.ymldocker-compose.override.yml文件),Compose会将多个文件合并成一个配置文件,合并的结果与指定文件的顺序有关。合并有两种操作,或者添加,或者覆盖。具体的合并规则以后会单独用一篇文章介绍。

-p标识用于给项目指定一个名称,如过没有指定,默认使用项目根目录的名称作为项目名称。
-v显示版本号

Compose的Commands有如下几个,一一介绍:
* build
* kill
* logs
* pause & unpause
* port
* ps
* pull
* restart
* rm
* run
* scale
* start & stop
* up

build

Usage: build [options] [SERVICE...]Options:    --force-rm  Always remove intermediate containers.    --no-cache  Do not use cache when building the image.    --pull      Always attempt to pull a newer version of the image.

docker-compose build命令用来创建或重新创建服务使用的镜像,后面指定的是服务的名称,创建之后的镜像名为project_service,即项目名后跟服务名。比如项目名称为composeset,其中的一个服务名称为web,则docker-compose build web创建的镜像的名称为composeset_web。

docker build一样,执行此命令也需要Dockerfile文件。当修改了Dockerfile文件或它的上下文之后,可以运行docker-compose build重新创建镜像,此时无需指定服务名称。

kill

Usage: kill [options] [SERVICE...]Options:-s SIGNAL         SIGNAL to send to the container. Default signal is SIGKILL.

docker-compose kill命令用于通过向容器发送SIGKILL信号强行停止服务。
-s标识用于覆盖默认发送的信号

logs

Usage: logs [options] [SERVICE...]Options:--no-color  Produce monochrome output.

docker-compose logs命令用于展示service的日志
--no-color标识使日志显示为单色

pause & unpause

docker-compose pause暂停服务;
docker-compose unpause恢复被暂停的服务;

port

Usage: port [options] SERVICE PRIVATE_PORTOptions:--protocol=proto  tcp or udp [default:  tcp]--index=index     index of the container if there are multiple                  instances of a service [default: 1]

docker-conpose port命令用于查看服务中的端口被映射到了宿主机的哪个端口上,使用这条命令时必须通知指定服务名称和内部端口号,完整命令示例:

$ docker-compose port web 5000  #查看web服务中5000端口被映射到宿主机的哪个端口上0.0.0.0:5000

ps

docker-compose ps用于显示当前项目下的容器。注意,执行此命令时必须cd到项目的根目录下,否则提示如下错误:

ERROR:         Can't find a suitable configuration file in this directory or any parent. Are you in the right directory?        Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml

docker ps不同,docker-compose会显示停止后的容器(即状态为Exited的容器);

docker-compose ps只能查看当前项目的容器,如果要显示本机上所有的容器,请使用docker ps -a

pull

Usage: pull [options] [SERVICE...]Options:--ignore-pull-failures  Pull what it can and ignores images with pull failures.

docker-compose pull用于;拉取服务依赖的镜像;

restart

docker-compose restart用于重启某个服务的所有容器,后跟服务名。

只有正在运行的服务才能重启,停止的服务不能使用restart命令。

rm

Usage: rm [options] [SERVICE...]Options:-f, --force   Don't ask to confirm removal-v            Remove volumes associated with containers

docker-compose rm删除停止的服务(容器)
-f表示强制删除
-v标识表示删除与容器相关的卷(volumes)

run

Usage: run [options] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]Options:    --allow-insecure-ssl  Deprecated - no effect.    -d                    Detached mode: Run container in the background, print                          new container name.    --name NAME           Assign a name to the container    --entrypoint CMD      Override the entrypoint of the image.    -e KEY=VAL            Set an environment variable (can be used multiple times)    -u, --user=""         Run as specified username or uid    --no-deps             Don't start linked services.    --rm                  Remove container after run. Ignored in detached mode.    -p, --publish=[]      Publish a container's port(s) to the host    --service-ports       Run command with the service's ports enabled and mapped                          to the host.    -T                    Disable pseudo-tty allocation. By default `docker-compose run`                          allocates a TTY.

docker-compose run命令用于在服务中运行一个一次性的命令。使用这个命令会新建一个容器,其配置和service的配置一样,也就是说新建的容器和service启动的容器有相同的volumes,links等。仅管如此,还是有两点不一样:
- run指定的命令会覆盖service配置中指定的命令
- run命令启动的容器不会创建任何在service配置中指定的端口,这避免了端口的冲突。如果你确实想创建端口并映射到宿主机上,可以使用--service-ports,例如:
bash
$ docker-compose run --service-ports web python manage.py shell

此外,端口的映射也可以改变,通过-p(--publish)标识。例如:
bash
$ docker-compose run -d -p 7001:8000 web python manage.py runserver 0.0.0.0:8000

上面的命令创建一个新的容器,其配置与web service一样,且将其8000端口映射到宿主机的7001端口上。

使用docker-compose run启动一个容器时,如果service中有--link指定的其他服务没有运行,会先运行这些服务,--link依赖的服务都运行成功后,再执行指定的命令。如果你不想启动这些依赖的容器,可以使用--no-deps标识。

$ docker-compose run --no-deps web python manage.py shell

此外:
-e用来添加环境变量;
--rm指定在run命令之后删除容器,如果指定了-d则忽略--rm标识;
-d指定后台运行;
--name指定容器的名字;

scale

docker-compose scale指定某一个服务启动的容器的个数,其参数格式为[service=num],例如:

$ docker-compose scale web=2 worker=3

这条命令可以使某项服务启动多个容器,但当容器有到主机的端口映射时,因为所有容器都指向一个宿主机的端口,所以只能启动一个容器,其他的会失败。

start & stop

docker-compose start命令启动运行某个服务的所有容器;
docker-compose stop命令停止运行一个服务的所有容器;

up

Usage: up [options] [SERVICE...]Options:    --allow-insecure-ssl   Deprecated - no effect.    -d                     Detached mode: Run containers in the background,                           print new container names.    --no-color             Produce monochrome output.    --no-deps              Don't start linked services.    --force-recreate       Recreate containers even if their configuration and                           image haven't changed. Incompatible with --no-recreate.    --no-recreate          If containers already exist, don't recreate them.                           Incompatible with --force-recreate.    --no-build             Don't build an image, even if it's missing    -t, --timeout TIMEOUT  Use this timeout in seconds for container shutdown                           when attached or when containers are already                           running. (default: 10)

docker-compose up创建并运行作为服务的容器,并将其输入输出重定向到控制台(attach),并将所有容器的输出合并到一起。命令退出后,所有的容器都会停止。

如果--link依赖的容器没有运行则运行依赖的容器;
-d标识指定容器后台运行;
如果已经存在服务的容器,且容器创建后服务的配置有变化,就重新创建容器。如果没有变化,默认不会重新创建容器;
--force-recreate标识指定即使服务配置没有变化,也重新创建容器;
--no-recreate标识表示如果服务的容器已经存在,不要重新创建它们;

s

0 0
原创粉丝点击