docker

DescriptionThe base command for the Docker CLI.

描述

根据您的Docker系统配置,您可能需要在每个docker命令前加上sudo。为了避免在使用docker命令时需要使用sudo,您的系统管理员可以创建一个名为docker的Unix组,并将用户添加到该组中。

有关安装Docker或sudo配置的更多信息,请参阅适用于您操作系统的安装说明。

显示帮助文本

要列出任何命令的帮助,只需执行该命令,后跟--help选项。

$ docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Create and run a new container from an image

Options:
      --add-host value             Add a custom host-to-IP mapping (host:ip) (default [])
  -a, --attach value               Attach to STDIN, STDOUT or STDERR (default [])
<...>

环境变量

以下环境变量列表由docker命令行支持:

VariableDescription
DOCKER_API_VERSIONOverride the negotiated API version to use for debugging (e.g. 1.19)
DOCKER_CERT_PATHLocation of your authentication keys. This variable is used both by the docker CLI and the dockerd 守护进程
DOCKER_CONFIGThe location of your client configuration files.
DOCKER_CONTENT_TRUST_SERVERThe URL of the Notary server to use. Defaults to the same URL as the registry.
DOCKER_CONTENT_TRUSTWhen set Docker uses notary to sign and verify images. Equates to --disable-content-trust=false for build, create, pull, push, run.
DOCKER_CONTEXTName of the docker context to use (overrides DOCKER_HOST env var and default context set with docker context use)
DOCKER_CUSTOM_HEADERS(Experimental) Configure custom HTTP headers to be sent by the client. Headers must be provided as a comma-separated list of name=value pairs. This is the equivalent to the HttpHeaders field in the configuration file.
DOCKER_DEFAULT_PLATFORMDefault platform for commands that take the --platform flag.
DOCKER_HIDE_LEGACY_COMMANDSWhen set, Docker hides "legacy" top-level commands (such as docker rm, and docker pull) in docker help output, and only Management commands per object-type (e.g., docker container) are printed. This may become the default in a future release.
DOCKER_HOSTDaemon socket to connect to.
DOCKER_TLSEnable TLS for connections made by the docker CLI (equivalent of the --tls command-line option). Set to a non-empty value to enable TLS. Note that TLS is enabled automatically if any of the other TLS options are set.
DOCKER_TLS_VERIFYWhen set Docker uses TLS and verifies the remote. This variable is used both by the docker CLI and the dockerd 守护进程
BUILDKIT_PROGRESSSet type of progress output (auto, plain, tty, rawjson) when building with BuildKit 后端. Use plain to show container output (default auto).

因为Docker是使用Go开发的,你也可以使用Go运行时使用的任何环境变量。特别是,你可能会发现这些很有用:

VariableDescription
HTTP_PROXYProxy URL for HTTP requests unless overridden by NoProxy.
HTTPS_PROXYProxy URL for HTTPS requests unless overridden by NoProxy.
NO_PROXYComma-separated values specifying hosts that should be excluded from proxying.

查看 Go 规范 以了解这些变量的详细信息。

选项类型

单字符命令行选项可以组合使用,因此不需要输入 docker run -i -t --name test busybox sh,你可以写成 docker run -it --name test busybox sh

布尔值

布尔选项的形式为 -d=false。你在帮助文本中看到的值是默认值,如果你指定该标志,则会设置此默认值。如果你指定一个没有值的布尔标志,这将把标志设置为 true,无论默认值是什么。

例如,运行 docker run -d 会将值设置为 true,因此你的容器 在后台以“分离”模式运行。

默认值为 true 的选项(例如,docker build --rm=true)只能通过显式设置为 false 来更改为非默认值:

$ docker build --rm=false .

你可以在单个命令行中多次指定像 -a=[] 这样的选项,例如在这些命令中:

$ docker run -a stdin -a stdout -i -t ubuntu /bin/bash

$ docker run -a stdin -a stdout -a stderr ubuntu /bin/ls

有时,多个选项可能需要一个更复杂的值字符串,例如 -v

$ docker run -v /host:/container example/mysql

注意

不要同时使用-t-a stderr选项,因为pty实现的限制。在pty模式下,所有的stderr都会直接输出到stdout

字符串和整数

--name=""这样的选项需要一个字符串,并且它们只能指定一次。像-c=0这样的选项需要一个整数,并且它们只能指定一次。

配置文件

默认情况下,Docker命令行将其配置文件存储在您$HOME目录中名为.docker的目录中。

Docker 管理配置目录中的大多数文件,您不应修改它们。但是,您可以修改 config.json 文件以控制 docker 命令行为的某些方面。

你可以使用环境变量或命令行选项来修改docker命令的行为。你也可以在config.json中使用选项来修改一些相同的行为。如果设置了环境变量和--config标志,标志将优先于环境变量。命令行选项会覆盖环境变量,而环境变量会覆盖你在config.json文件中指定的属性。

更改 .docker 目录

要指定不同的目录,请使用DOCKER_CONFIG环境变量或--config命令行选项。如果两者都指定了,则--config选项会覆盖DOCKER_CONFIG环境变量。下面的示例使用位于~/testconfigs/目录中的config.json文件覆盖了docker ps命令。

$ docker --config ~/testconfigs/ ps

此标志仅适用于正在运行的任何命令。对于持久配置,您可以在您的 shell 中设置 DOCKER_CONFIG 环境变量(例如 ~/.profile~/.bashrc)。下面的示例将新目录设置为 HOME/newdir/.docker

$ echo export DOCKER_CONFIG=$HOME/newdir/.docker > ~/.profile

Docker CLI 配置文件 (config.json) 属性

使用Docker CLI配置来自定义docker CLI的设置。配置文件使用JSON格式,并包含以下属性:

默认情况下,配置文件存储在 ~/.docker/config.json 中。请参考 更改 .docker 目录 部分以使用 不同的位置。

警告

配置文件以及~/.docker配置目录中的其他文件可能包含敏感信息,例如代理的认证信息,或者根据您的凭证存储方式,可能包含镜像仓库的凭证。在与他人共享之前,请仔细检查配置文件的内容,并避免将文件提交到版本控制系统中。

自定义命令的默认输出格式

这些字段允许您自定义某些命令的默认输出格式,如果没有提供--format标志。

PropertyDescription
configFormatCustom default format for docker config ls output. See docker config ls for a list of supported formatting directives.
imagesFormatCustom default format for docker images / docker image ls output. See docker images for a list of supported formatting directives.
networksFormatCustom default format for docker network ls output. See docker network ls for a list of supported formatting directives.
nodesFormatCustom default format for docker node ls output. See docker node ls for a list of supported formatting directives.
pluginsFormatCustom default format for docker plugin ls output. See docker plugin ls for a list of supported formatting directives.
psFormatCustom default format for docker ps / docker container ps output. See docker ps for a list of supported formatting directives.
secretFormatCustom default format for docker secret ls output. See docker secret ls for a list of supported formatting directives.
serviceInspectFormatCustom default format for docker service inspect output. See docker service inspect for a list of supported formatting directives.
servicesFormatCustom default format for docker service ls output. See docker service ls for a list of supported formatting directives.
statsFormatCustom default format for docker stats output. See docker stats for a list of supported formatting directives.
tasksFormatCustom default format for docker stack ps output. See docker stack ps for a list of supported formatting directives.
volumesFormatCustom default format for docker volume ls output. See docker volume ls for a list of supported formatting directives.

自定义HTTP头

属性 HttpHeaders 指定了一组要包含在从 Docker 客户端发送到守护进程的所有消息中的头信息。Docker 不会尝试解释或理解这些头信息;它只是将它们放入消息中。Docker 不允许这些头信息更改它为自己设置的任何头信息。

或者,使用DOCKER_CUSTOM_HEADERS 环境变量, 该变量在v27.1及更高版本中可用。此环境变量是实验性的, 其确切行为可能会发生变化。

凭证存储选项

属性 credsStore 指定了一个外部二进制文件作为默认的凭证存储。当设置了此属性时,docker login 将尝试将凭证存储在由 docker-credential- 指定的二进制文件中,该文件在 $PATH 上可见。如果未设置此属性,凭证将存储在 CLI 配置文件的 auths 属性中。有关更多信息,请参阅 凭证存储 部分在 docker login 文档中

属性 credHelpers 指定了一组凭证助手,用于在存储和检索特定注册表的凭证时优先于 credsStoreauths。如果设置了此属性,则在存储或检索特定注册表的凭证时将使用二进制文件 docker-credential-。有关更多信息,请参阅 凭证助手 部分在 docker login 文档中

容器的自动代理配置

属性 proxies 指定了要在容器上自动设置的代理环境变量,并在 docker build 期间使用的容器上设置为 --build-arg。可以配置一组 "default" 代理,并将用于客户端连接的任何 Docker 守护程序,或者每个主机(Docker 守护程序)的配置,例如 https://docker-daemon1.example.com。可以为每个环境设置以下属性:

PropertyDescription
httpProxyDefault value of HTTP_PROXY and http_proxy for containers, and as --build-arg on docker build
httpsProxyDefault value of HTTPS_PROXY and https_proxy for containers, and as --build-arg on docker build
ftpProxyDefault value of FTP_PROXY and ftp_proxy for containers, and as --build-arg on docker build
noProxyDefault value of NO_PROXY and no_proxy for containers, and as --build-arg on docker build
allProxyDefault value of ALL_PROXY and all_proxy for containers, and as --build-arg on docker build

这些设置仅用于配置容器的代理设置,而不作为docker CLI或dockerd守护进程的代理设置。请参考环境变量HTTP/HTTPS代理部分来配置CLI和守护进程的代理设置。

警告

代理设置可能包含敏感信息(例如,如果代理需要身份验证)。环境变量以纯文本形式存储在容器的配置中,因此可以通过远程API进行检查,或者在使用docker commit时提交到镜像中。

从容器分离的默认按键序列

一旦附加到容器,用户可以使用CTRL-p CTRL-q键序列从中分离并保持其运行。这个分离键序列可以通过detachKeys属性进行自定义。为该属性指定一个值。的格式是一个逗号分隔的列表,可以是字母[a-Z],或者是ctrl-与以下任意一个组合:

  • a-z (单个小写字母字符)
  • @ (at符号)
  • [ (左括号)
  • \\ (两个反斜杠)
  • _ (下划线)
  • ^ (插入符号)

您的自定义设置适用于使用您的Docker客户端启动的所有容器。 用户可以基于每个容器覆盖您的自定义或默认键序列。为此,用户在使用docker attachdocker execdocker rundocker start命令时指定--detach-keys标志。

CLI 插件选项

属性 plugins 包含特定于 CLI 插件的设置。键是插件名称,而值是该插件特定的选项的进一步映射。

示例配置文件

以下是一个示例 config.json 文件,用于说明各种字段的格式:

{
  "HttpHeaders": {
    "MyHeader": "MyValue"
  },
  "psFormat": "table {{.ID}}\\t{{.Image}}\\t{{.Command}}\\t{{.Labels}}",
  "imagesFormat": "table {{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}",
  "pluginsFormat": "table {{.ID}}\t{{.Name}}\t{{.Enabled}}",
  "statsFormat": "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}",
  "servicesFormat": "table {{.ID}}\t{{.Name}}\t{{.Mode}}",
  "secretFormat": "table {{.ID}}\t{{.Name}}\t{{.CreatedAt}}\t{{.UpdatedAt}}",
  "configFormat": "table {{.ID}}\t{{.Name}}\t{{.CreatedAt}}\t{{.UpdatedAt}}",
  "serviceInspectFormat": "pretty",
  "nodesFormat": "table {{.ID}}\t{{.Hostname}}\t{{.Availability}}",
  "detachKeys": "ctrl-e,e",
  "credsStore": "secretservice",
  "credHelpers": {
    "awesomereg.example.org": "hip-star",
    "unicorn.example.com": "vcbait"
  },
  "plugins": {
    "plugin1": {
      "option": "value"
    },
    "plugin2": {
      "anotheroption": "anothervalue",
      "athirdoption": "athirdvalue"
    }
  },
  "proxies": {
    "default": {
      "httpProxy":  "http://user:pass@example.com:3128",
      "httpsProxy": "https://my-proxy.example.com:3129",
      "noProxy":    "intra.mycorp.example.com",
      "ftpProxy":   "http://user:pass@example.com:3128",
      "allProxy":   "socks://example.com:1234"
    },
    "https://manager1.mycorp.example.com:2377": {
      "httpProxy":  "http://user:pass@example.com:3128",
      "httpsProxy": "https://my-proxy.example.com:3129"
    }
  }
}

实验性功能

实验性功能提供对未来产品功能的早期访问。这些功能旨在用于测试和反馈,它们可能会在版本之间发生变化而不发出警告,或者可能会在未来的版本中被移除。

从 Docker 20.10 开始,实验性 CLI 功能默认启用,无需配置即可启用它们。

公证

如果使用您自己的公证服务器和自签名证书或内部证书颁发机构,您需要将证书放置在Docker配置目录中的tls//ca.crt

或者,您可以通过将其添加到系统的根证书颁发机构列表中来全局信任该证书。

选项

OptionDefaultDescription
--config/root/.dockerLocation of client config files
-c, --contextName of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with docker context use)
-D, --debugEnable debug mode
-H, --hostDaemon socket to connect to
-l, --log-levelinfoSet the logging level (debug, info, warn, error, fatal)
--tlsUse TLS; implied by --tlsverify
--tlscacert/root/.docker/ca.pemTrust certs signed only by this CA
--tlscert/root/.docker/cert.pemPath to TLS certificate file
--tlskey/root/.docker/key.pemPath to TLS key file
--tlsverifyUse TLS and verify the remote

示例

指定守护进程主机 (-H, --host)

你可以使用-H--host标志来指定在调用docker命令时使用的套接字。你可以使用以下协议:

SchemeDescriptionExample
unix://[<path>]Unix socket (Linux only)unix:///var/run/docker.sock
tcp://[<IP or host>[:port]]TCP connectiontcp://174.17.0.1:2376
ssh://[username@]<IP or host>[:port]SSH connectionssh://user@192.168.64.5
npipe://[<name>]Named pipe (Windows only)npipe:////./pipe/docker_engine

如果不指定-H标志,并且没有使用自定义的 context, 命令将使用以下默认套接字:

  • unix:///var/run/docker.sock 在 macOS 和 Linux 上
  • npipe:////./pipe/docker_engine 在 Windows 上

为了在不每次命令都指定-H标志的情况下实现类似效果,你也可以 创建一个上下文, 或者使用 DOCKER_HOST环境变量

有关-H标志的更多信息,请参阅 守护进程套接字选项

使用TCP套接字

以下示例展示了如何通过TCP调用docker ps,连接到IP地址为174.17.0.1的远程守护进程,该守护进程正在监听端口2376

$ docker -H tcp://174.17.0.1:2376 ps

注意

按照惯例,Docker守护进程使用端口2376进行安全的TLS连接,使用端口2375进行不安全的非TLS连接。

使用SSH套接字

当你使用SSH在远程守护进程上调用命令时,请求会被转发到SSH主机上的/var/run/docker.sock Unix套接字。

$ docker -H ssh://user@192.168.64.5 ps

您可以选择性地通过在SSH地址的末尾附加路径组件来指定套接字的位置。

$ docker -H ssh://user@192.168.64.5/var/run/docker.sock ps

子命令

CommandDescription
docker build (legacy builder)Build an image from a Dockerfile
docker builderManage builds
docker buildxDocker Buildx
docker checkpointManage checkpoints
docker composeDocker Compose
docker configManage Swarm configs
docker containerManage containers
docker contextManage contexts
docker debugGet a shell into any container or image. An alternative to debugging with `docker exec`.
docker desktop (Beta)Docker Desktop
docker imageManage images
docker initCreates Docker-related starter files for your project
docker inspectReturn low-level information on Docker objects
docker loginAuthenticate to a registry
docker logoutLog out from a registry
docker manifestManage Docker image manifests and manifest lists
docker networkManage networks
docker nodeManage Swarm nodes
docker pluginManage plugins
docker scoutCommand line tool for Docker Scout
docker searchSearch Docker Hub for images
docker secretManage Swarm secrets
docker serviceManage Swarm services
docker stackManage Swarm stacks
docker swarmManage Swarm
docker systemManage Docker
docker trustManage trust on Docker images
docker versionShow the Docker version information
docker volumeManage volumes