2018-12-10 Docker Compose 命令说明:
原文地址:https://docs.docker.com/compose/reference/
docker-compose build
Usage: build [options] [--build-arg key=val...] [SERVICE...]
Options:
--compress Compress the build context using gzip.
使用gzip压缩构建上下文。
--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.
总是尝试拉出图像的新版本
-m, --memory MEM Sets memory limit for the build container.
为构建容器设置内存限制。
--build-arg key=val Set build-time variables for services.
为服务设置构建时变量。
--parallel Build images in parallel.
并行构建镜像
Services are built once and then tagged, by default as project_service. For example, composetest_db. If the Compose file specifies an image name, the image is tagged with that name, substituting any variables beforehand. See variable substitution.
服务构建一次,然后标记为project_service,默认情况下标记为project_service。例如,composetest_db。如果组合文件指定了一个映像名称,则用该名称标记映像,并预先替换任何变量。参考变量替换。
变量替换参考地址:https://docs.docker.com/compose/compose-file/#variable-substitution
If you change a service’s Dockerfile or the contents of its build directory, run docker-compose build to rebuild it.
如果更改服务的Dockerfile或其构建目录的内容,请运行docker-compose构建来重新构建它。
docker-compose bundle
Usage: bundle [options]
Options:
--push-images Automatically push images for any services
which have a `build` option specified.
自动任何服务推送镜像,由“build”选项指定。
-o, --output PATH Path to write the bundle file to.
Defaults to "<project name>.dab".
设置写入的路径。默认为“<项目名称>.dab”。
Generate a Distributed Application Bundle (DAB) from the Compose file.
从compose文件生成分布式应用程序包(DAB)。
Images must have digests stored, which requires interaction with a Docker registry. If digests aren’t stored for all images, you can fetch them with docker-compose pull or docker-compose push. To push images automatically when bundling, pass --push-images. Only services with a build option specified have their images pushed.
镜像必须存储摘要,这需要与Docker注册表进行交互。如果没有为所有镜像存储摘要,可以使用docker- composition pull或docker- composition push获取它们。要在绑定时自动推送镜像,请传递 --push-images。只有指定了构建选项的服务才会推送它们的映像。
2018-12-13
docker-compose config
Usage: config [options]
Options:
--resolve-image-digests Pin image tags to digests.
将图像标记引到摘要。
-q, --quiet Only validate the configuration – do not print anything.
只验证配置—不要打印任何东西。
--services Print the service names, one per line.
打印服务名称,每行一个。
--volumes Print the volume names, one per line.
打印卷名,每行一个。
--hash="*" Print the service config hash, one per line.
Set "service1,service2" for a list of specified services
or use the wildcard symbol to display all services.
打印服务配置散列,每行一个。
为指定的服务列表设置“service1,service2”
或使用通配符显示所有服务。
Validate and view the Compose file.
验证和查看compose文件。
docker-compose create
Creates containers for a service.
为服务创建容器。
This command is deprecated. Use the `up` command with `--no-start` instead.
不推荐使用此命令。使用“up”命令,而不是“no-start”。
Usage: create [options] [SERVICE...]
Options:
--force-recreate Recreate containers even if their configuration and
image haven't changed. Incompatible with --no-recreate.
重新创建容器,即使它们的配置和
镜像没有改变形象。不兼容——no-recreate。
--no-recreate If containers already exist, don't recreate them.
Incompatible with --force-recreate.
如果容器已经存在,不要重新创建它们。
不兼容——force-recreate。
--no-build Don't build an image, even if it's missing.
不要建立一个镜像,即使它是缺失的。
--build Build images before creating containers.
在创建容器之前构建映像。
docker-compose down
Usage: down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
删除镜像。类型必须是:
“全部”:删除任何服务使用的所有镜像。
“本地”:只删除没有由“image”字段设置自定义标记的镜像。
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
删除在组合文件的“卷”部分声明的命名卷和附加到容器的匿名卷。
--remove-orphans Remove containers for services not defined in the
Compose file
删除组合文件中未定义的服务容器
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
设置关机时间,默认是十秒
Stops containers and removes containers, networks, volumes, and images created by up.
停止容器并删除由up创建的容器、网络、卷和映像。
By default, the only things removed are:
默认情况下,唯一被删除的东西是:
- Containers for services defined in the Compose file
- Networks defined in the networks section of the Compose file
Compose文件中定义的服务的容器
在Compose文件的网络部分中定义的网络
- The default network, if one is used
如果使用默认网络,删除的默认网络
Networks and volumes defined as external are never removed.
定义为外部的网络和卷永远不会被删除。
docker-compose events
Usage: events [options] [SERVICE...]
Options:
--json Output events as a stream of json objects
以json对象流的形式输出事件
Stream container events for every container in the project.
为项目中的每个容器事件流化。
With the --json flag, a json object is printed one per line with the format:
使用——json标志,json对象每行打印一个,格式为:
{
"time": "2015-11-20T18:01:03.615550",
"type": "container",
"action": "create",
"id": "213cf7...5fc39a",
"service": "web",
"attributes": {
"name": "application_web_1",
"image": "alpine:edge",
}
}
The events that can be received using this can be seen here.
可以在这里看到使用它可以接收到的事件。
https://docs.docker.com/engine/reference/commandline/events/#extended-description
docker-compose exec
Usage: exec [options] [-e KEY=VAL...] SERVICE COMMAND [ARGS...]
Options:
-d, --detach Detached mode: Run command in the background.
分离模式:在后台运行命令。
--privileged Give extended privileges to the process.
为进程赋予扩展特权。
-u, --user USER Run the command as this user.
以该用户运行该命令。
-T Disable pseudo-tty allocation. By default `docker-compose exec`
allocates a TTY.
禁用pseudo-tty分配。默认情况下,“docker-compose exec”分配一个TTY。
--index=index index of the container if there are multiple
instances of a service [default: 1]
如果服务有多个实例,容器的索引[默认值:1]
-e, --env KEY=VAL Set environment variables (can be used multiple times,
not supported in API < 1.25)
设置环境变量(可以多次使用,API < 1.25中不支持)
-w, --workdir DIR Path to workdir directory for this command.
指向此命令的workdir目录的路径。
docker-compose help
Usage: help COMMAND
Displays help and usage instructions for a command.
显示命令的帮助和使用说明。
docker-compose images
Usage: images [options] [SERVICE...]
Options:
-q Only display image IDs
仅显示镜像id
List images used by the created containers.
列出创建容器所使用的镜像。
docker-compose kill
Usage: kill [options] [SERVICE...]
Options:
-s SIGNAL SIGNAL to send to the container. Default signal is SIGKILL.
发送到容器的信号。默认信号是SIGKILL。
Forces running containers to stop by sending a SIGKILL signal. Optionally the signal can be passed, for example:
通过发送SIGKILL信号强制运行容器停止。可选择传递信号,例如
docker-compose kill -s SIGINT
docker-compose logs
Estimated reading time: 1 minute
Usage: logs [options] [SERVICE...]
Options:
--no-color Produce monochrome output.
产生单色输出。
-f, --follow Follow log output
跟踪日志输出
-t, --timestamps Show timestamps
显示时间戳
--tail="all" Number of lines to show from the end of the logs
for each container.
从每个容器的日志末尾显示的行数。
Displays log output from services.
显示来自服务的日志输出。
2018-12-14
docker-compose pause
Usage: pause [SERVICE...]
Pauses running containers of a service. They can be unpaused with docker-compose unpause.
暂停运行服务容器。它们可以使用docker-compose unpause来解除暂停。
docker-compose port
Usage: port [options] SERVICE PRIVATE_PORT
Options:
--protocol=proto tcp or udp [default: tcp]
--index=index index of the container if there are multiple
instances of a service [default: 1]
如果服务有多个实例,容器的索引[默认值:1]
Prints the public port for a port binding.
打印端口绑定的公共端口。
docker-compose ps
Usage: ps [options] [SERVICE...]
Options:
-q Only display IDs
只显示ID
Lists containers.
以列表方式显示容器
docker-compose pull
Usage: pull [options] [SERVICE...]
Options:
--ignore-pull-failures Pull what it can and ignores images with pull failures.
尽可能拉取,忽略镜像可能拉取失败
--parallel Deprecated, pull multiple images in parallel (enabled by default).
不推荐,拉取多个镜像并行(默认启用)。
--no-parallel Disable parallel pulling.
禁用并行拉取
-q, --quiet Pull without printing progress information
拉取时不打印进度信息。
--include-deps Also pull services declared as dependencies
也可以将拉取的服务声明为依赖。
Pulls an image associated with a service defined in a docker-compose.yml or docker-stack.yml file, but does not start containers based on those images.
获取与在docker- composition中定义的服务关联的映像。yml或docker-stack。但不会基于这些映像启动容器。
For example, suppose you have this docker-compose.yml file from the Quickstart: Compose and Rails sample.
例如,假设您有这个docker-compose。来自Quickstart的yml文件:组合和Rails示例。
version: '2'
services:
db:
image: postgres
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
If you run docker-compose pull ServiceName in the same directory as the docker-compose.yml file that defines the service, Docker pulls the associated image. For example, to call the postgres image configured as the db service in our example, you would run docker-compose pull db.
如果运行docker-compose,则将ServiceName拉到与docker-compose相同的目录中。定义服务的yml文件,Docker获取相关的映像。例如,要调用在我们的示例中配置为db服务的postgres映像,您需要运行docker-compose pull db。
$ docker-compose pull db
Pulling db (postgres:latest)...
latest: Pulling from library/postgres
cd0a524342ef: Pull complete
9c784d04dcb0: Pull complete
d99dddf7e662: Pull complete
e5bff71e3ce6: Pull complete
cb3e0a865488: Pull complete
31295d654cd5: Pull complete
fc930a4e09f5: Pull complete
8650cce8ef01: Pull complete
61949acd8e52: Pull complete
527a203588c0: Pull complete
26dec14ac775: Pull complete
0efc0ed5a9e5: Pull complete
40cd26695b38: Pull complete
Digest: sha256:fd6c0e2a9d053bebb294bb13765b3e01be7817bf77b01d58c2377ff27a4a46dc
Status: Downloaded newer image for postgres:latest
docker-compose push
Usage: push [options] [SERVICE...]
Options:
--ignore-push-failures Push what it can and ignores images with push failures.\
尽可能推送,不管推送是否失败.
Pushes images for services to their respective registry/repository.
将服务的映像推送到各自的注册表/存储库。
The following assumptions are made:
做出以下假设:
- You are pushing an image you have built locally
- You have access to the build key
你正在推送在本地创建的镜像
您可以访问构建密钥
示例:
version: '3'
services:
service1:
build: .
image: localhost:5000/yourimage # goes to local registry
service2:
build: .
image: youruser/yourimage # goes to youruser DockerHub registry
docker-compose restart
Usage: restart [options] [SERVICE...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds. (default: 10)
指定关闭时间,单位是秒
Restarts all stopped and running services.
重启所有关闭状态的服务
If you make changes to your docker-compose.yml configuration these changes are not reflected after running this command.
如果对docker-compose.yml 进行更改。这些更改在运行此命令后没有响应。
For example, changes to environment variables (which are added after a container is built, but before the container’s command is executed) are not updated after restarting.
例如,对环境变量的更改(在构建容器之后添加,但是在执行容器的命令之前添加)在重新启动之后不会更新。
docker-compose rm
Usage: rm [options] [SERVICE...]
Options:
-f, --force Don't ask to confirm removal
直接删就行, 不用和我确认。
-s, --stop Stop the containers, if required, before removing
如有需要,请在卸载前停止容器
-v Remove any anonymous volumes attached to containers
删除任何附加到容器的匿名卷
Removes stopped service containers.
移除所有已经停止的服务容器
By default, anonymous volumes attached to containers are not removed. You can override this with -v. To list all volumes, use docker volume ls.
默认情况下,附加到容器上的匿名卷不会被删除。你可以用-v覆盖它。要列出所有卷,请使用docker volume ls。
Any data which is not in a volume is lost.
任何不在卷中的数据都将丢失。
Running the command with no options also removes one-off containers created by docker-compose up or docker-compose run:
在没有选项的情况下运行该命令还会删除docker-compose up或docker-compose run创建的一次性容器:
$ docker-compose rm
Going to remove djangoquickstart_web_run_1
Are you sure? [yN] y
Removing djangoquickstart_web_run_1 ... done
docker-compose run
Usage:
run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...]
SERVICE [COMMAND] [ARGS...]
Options:
-d, --detach 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)
设置环境变量(可以多次使用)
-l, --label KEY=VAL Add or override a label (can be used multiple times)
添加或覆盖标签(可以多次使用)
-u, --user="" Run as specified username or uid
以指定的用户名或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
向host发布容器的端口
--service-ports Run command with the service's ports enabled and mapped
to the host.
在服务端口启用的情况下运行命令并映射到主机端口。
--use-aliases Use the service's network aliases in the network(s) the
container connects to.
在容器连接的网络中使用服务的网络别名。
-v, --volume=[] Bind mount a volume (default [])
绑定装入卷(默认[])
-T Disable pseudo-tty allocation. By default `docker-compose run`
allocates a TTY.
禁用pseudo-tty分配。默认情况下,“docker-compose run”分配一个TTY。
-w, --workdir="" Working directory inside the container
容器内的工作目录
Runs a one-time command against a service. For example, the following command starts the web service and runs bash as its command.
对服务运行一次性命令。例如,下面的命令启动web服务,并将bash作为其命令运行。
docker-compose run web bash
Commands you use with run start in new containers with configuration defined by that of the service, including volumes, links, and other details. However, there are two important differences.
使用run 命令启动一个新容器,这些容器的配置由服务定义,包括卷、链接和其他详细信息。然而,有两个重要的区别。
First, the command passed by run overrides the command defined in the service configuration. For example, if the web service configuration is started with bash, then docker-compose run web python app.py overrides it with python app.py.
首先,run传递的命令覆盖服务配置中定义的命令。例如,如果web服务配置是用bash启动的,那么docker-compose run web python app.py将用python app.py覆盖它。
The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag:
第二个区别是docker-compose run命令不创建服务配置中指定的任何端口。这可以防止端口与已经打开的端口发生冲突。如果您确实希望创建服务端口并将其映射到主机,请指定—service-ports标志:
docker-compose run --service-ports web python manage.py shell
Alternatively, manual port mapping can be specified with the --publish or -p options, just as when using docker run:
或者,可以使用——publish或-p选项指定手动端口映射,就像使用 docker run 时一样:
docker-compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell
If you start a service configured with links, the run command first checks to see if the linked service is running and starts the service if it is stopped. Once all the linked services are running, the run executes the command you passed it. For example, you could run:
如果启动配置有链接的服务,run命令首先检查链接的服务是否正在运行,如果服务停止就将他启动。一旦所有链接的服务都在运行,run将执行您传递给它的命令。例如,你可以执行:
docker-compose run db psql -h db -U docker
This opens an interactive PostgreSQL shell for the linked db container.
If you do not want the run command to start linked containers, use the --no-deps flag:
这将为链接的db容器打开一个交互式PostgreSQL shell。
docker-compose run --no-deps web python manage.py shell
If you want to remove the container after running while overriding the container’s restart policy, use the --rm flag:
如果您想在运行后删除容器,同时覆盖容器的重启策略,请使用—rm标志:
docker-compose run --rm web python manage.py db upgrade
This runs a database upgrade script, and removes the container when finished running, even if a restart policy is specified in the service configuration.
这将运行一个数据库升级脚本,并在完成运行时删除容器,即使在服务配置中指定了重启策略。
docker-compose scale
官方不推荐使用就不翻译了
Use the up command with the --scale flag instead.
替换方案是 docker compose up --scale
docker-compose start
Usage: start [SERVICE...]
Starts existing containers for a service.
启动服务的现有容器。
docker-compose stop
Estimated reading time: 1 minute
Usage: stop [options] [SERVICE...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds (default: 10).
设置关闭时间,默认是10秒
Stops running containers without removing them. They can be started again withdocker-compose start.
停止运行容器而不移除它们。可以使用docker-compose start 重新启动它们。
docker-compose top
Usage: top [SERVICE...]
Displays the running processes.
显示正在显示的进程。
$ docker-compose top
compose_service_a_1
PID USER TIME COMMAND----------------------------
4060 root 0:00 top
compose_service_b_1
PID USER TIME COMMAND----------------------------
4115 root 0:00 top
docker-compose unpause
Usage: unpause [SERVICE...]
Unpauses paused containers of a service.
暂停暂停服务的容器。
docker-compose up
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.
分离模式:在后台运行容器,打印新的容器名称。不兼容 abort-on-container-exit。
--no-color Produce monochrome output.
产生单色输出。
--quiet-pull Pull without printing progress information
pull不打印进度信息
--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。
--no-recreate If containers already exist, don't recreate
them. Incompatible with --force-recreate and -V.
如果容器已经存在,不要重新创建它们。与-force-和-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.
停止所有容器,如果任何容器被停止。不兼容- d。
-t, --timeout TIMEOUT Use this timeout in seconds for container
shutdown when attached or when containers are
already running. (default: 10)
当附加或容器已经运行时,使用此超时(以秒为单位)关闭容器。(默认值: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.
删除compose文件中未定义的服务容器。
--exit-code-from SERVICE Return the exit code of the selected service
container. Implies --abort-on-container-exit.
返回所选服务容器的退出代码。暗示? --abort-on-container-exit.
--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
`scale` setting in the Compose file if present.
将服务扩展到NUM实例。如果存在,重写组合文件中的“scale”设置。
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 (essentially running docker-compose logs -f). When the command exits, all containers are stopped. Running docker-compose up -d starts the containers in the background and leaves them running.
docker-compose up命令聚合每个容器的输出(本质上是运行docker-compose日志-f)。当命令退出时,所有容器都将停止。运行docker- composition -d在后台启动容器并让它们继续运行。
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.
如果存在服务的现有容器,并且在容器创建之后更改了服务的配置或映像,那么docker- composition up将通过停止和重新创建容器(保留已挂载的卷)来获取更改。要防止组合接收更改,请使用—no- rebuild标志
If you want to force Compose to stop and recreate all containers, use the --force-recreate flag.
如果希望强制组合停止并重新创建所有容器,请使用——force- creation标志。
If the process encounters an error, the exit code for this command is 1.
如果进程遇到错误,则此命令的退出代码为1。
If the process is interrupted using SIGINT (ctrl + C) or SIGTERM, the containers are stopped, and the exit code is 0.
如果使用SIGINT (ctrl + C)或SIGTERM中断进程,则容器将停止,退出代码为0。
If SIGINT or SIGTERM is sent again during this shutdown phase, the running containers are killed, and the exit code is 2.
如果在此关闭阶段再次发送SIGINT或SIGTERM,则停止运行的容器,退出代码为2。