dockerd
daemon
Usage: dockerd [OPTIONS]
A self-sufficient runtime for containers.
Options:
--add-runtime runtime Register an additional OCI compatible runtime (default [])
--authorization-plugin list Authorization plugins to load
--bip string IPv4 address for the default bridge
--bip6 string IPv6 address for the default bridge
-b, --bridge string Attach containers to a network bridge
--cdi-spec-dir list CDI specification directories to use
--cgroup-parent string Set parent cgroup for all containers
--config-file string Daemon configuration file (default "/etc/docker/daemon.json")
--containerd string containerd grpc address
--containerd-namespace string Containerd namespace to use (default "moby")
--containerd-plugins-namespace string Containerd namespace to use for plugins (default "plugins.moby")
--cpu-rt-period int Limit the CPU real-time period in microseconds for the
parent cgroup for all containers (not supported with cgroups v2)
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds for the
parent cgroup for all containers (not supported with cgroups v2)
--cri-containerd start containerd with cri
--data-root string Root directory of persistent Docker state (default "/var/lib/docker")
-D, --debug Enable debug mode
--default-address-pool pool-options Default address pools for node specific local networks
--default-cgroupns-mode string Default mode for containers cgroup namespace ("host" | "private") (default "private")
--default-gateway ip Default gateway IPv4 address for the default bridge network
--default-gateway-v6 ip Default gateway IPv6 address for the default bridge network
--default-ipc-mode string Default mode for containers ipc ("shareable" | "private") (default "private")
--default-network-opt mapmap Default network options (default map[])
--default-runtime string Default OCI runtime for containers (default "runc")
--default-shm-size bytes Default shm size for containers (default 64MiB)
--default-ulimit ulimit Default ulimits for containers (default [])
--dns list DNS server to use
--dns-opt list DNS options to use
--dns-search list DNS search domains to use
--exec-opt list Runtime execution options
--exec-root string Root directory for execution state files (default "/var/run/docker")
--experimental Enable experimental features
--feature map Enable feature in the daemon
--fixed-cidr string IPv4 subnet for the default bridge network
--fixed-cidr-v6 string IPv6 subnet for the default bridge network
-G, --group string Group for the unix socket (default "docker")
--help Print usage
-H, --host list Daemon socket(s) to connect to
--host-gateway-ip list IP addresses that the special 'host-gateway' string in --add-host resolves to.
Defaults to the IP addresses of the default bridge
--http-proxy string HTTP proxy URL to use for outgoing traffic
--https-proxy string HTTPS proxy URL to use for outgoing traffic
--icc Enable inter-container communication for the default bridge network (default true)
--init Run an init in the container to forward signals and reap processes
--init-path string Path to the docker-init binary
--insecure-registry list Enable insecure registry communication
--ip ip Host IP for port publishing from the default bridge network (default 0.0.0.0)
--ip-forward Enable IP forwarding in system configuration (default true)
--ip-forward-no-drop Do not set the filter-FORWARD policy to DROP when enabling IP forwarding
--ip-masq Enable IP masquerading for the default bridge network (default true)
--ip6tables Enable addition of ip6tables rules (default true)
--iptables Enable addition of iptables rules (default true)
--ipv6 Enable IPv6 networking for the default bridge network
--label list Set key=value labels to the daemon
--live-restore Enable live restore of docker when containers are still running
--log-driver string Default driver for container logs (default "json-file")
--log-format string Set the logging format ("text"|"json") (default "text")
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--log-opt map Default log driver options for containers (default map[])
--max-concurrent-downloads int Set the max concurrent downloads (default 3)
--max-concurrent-uploads int Set the max concurrent uploads (default 5)
--max-download-attempts int Set the max download attempts for each pull (default 5)
--metrics-addr string Set default address and port to serve the metrics api on
--mtu int Set the MTU for the default "bridge" network (default 1500)
--network-control-plane-mtu int Network Control plane MTU (default 1500)
--no-new-privileges Set no-new-privileges by default for new containers
--no-proxy string Comma-separated list of hosts or IP addresses for which the proxy is skipped
--node-generic-resource list Advertise user-defined resource
-p, --pidfile string Path to use for daemon PID file (default "/var/run/docker.pid")
--raw-logs Full timestamps without ANSI coloring
--registry-mirror list Preferred registry mirror
--rootless Enable rootless mode; typically used with RootlessKit
--seccomp-profile string Path to seccomp profile. Set to "unconfined" to disable the default seccomp profile (default "builtin")
--selinux-enabled Enable selinux support
--shutdown-timeout int Set the default shutdown timeout (default 15)
-s, --storage-driver string Storage driver to use
--storage-opt list Storage driver options
--swarm-default-advertise-addr string Set default address or interface for swarm advertised address
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "~/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "~/.docker/cert.pem")
--tlskey string Path to TLS key file (default "~/.docker/key.pem")
--tlsverify Use TLS and verify the remote
--userland-proxy Use userland proxy for loopback traffic (default true)
--userland-proxy-path string Path to the userland proxy binary
--userns-remap string User/Group setting for user namespaces
--validate Validate daemon configuration and exit
-v, --version Print version information and quit
带 [] 的选项可以指定多次。
描述
dockerd
是管理容器的持久进程。Docker 为 daemon 和 client 使用不同的二进制文件。要运行 daemon,请键入 dockerd
。
要以调试输出运行 daemon,请使用 dockerd --debug
或将 "debug": true
添加到daemon.json 文件中。
注意
启用实验性功能
通过使用
--experimental
标志启动dockerd
或将"experimental": true
添加到daemon.json
文件中来启用实验性功能。
环境变量
以下列表中的环境变量受 dockerd
daemon 支持。其中一些环境变量同时受 Docker Daemon 和 docker
CLI 支持。请参阅环境变量,了解 docker
CLI 支持的环境变量。
变量 | 描述 |
---|---|
DOCKER_CERT_PATH | 你的身份验证密钥的位置。此变量同时受 docker CLI 和 dockerd daemon 使用。 |
DOCKER_DRIVER | 要使用的存储驱动。 |
DOCKER_RAMDISK | 如果设置,这将禁用 pivot_root 。 |
DOCKER_TLS_VERIFY | 设置后,Docker 将使用 TLS 并验证远程端。此变量同时受 docker CLI 和 dockerd daemon 使用。 |
DOCKER_TMPDIR | daemon 创建的临时文件的位置。 |
HTTP_PROXY | 用于 HTTP 请求的代理 URL,除非被 NoProxy 覆盖。详情请参阅Go 规范。 |
HTTPS_PROXY | 用于 HTTPS 请求的代理 URL,除非被 NoProxy 覆盖。详情请参阅Go 规范。 |
MOBY_DISABLE_PIGZ | 即使安装了 unpigz ,也禁用其在拉取镜像时并行解压层的功能。 |
NO_PROXY | 指定应从代理中排除的主机的逗号分隔值。详情请参阅Go 规范。 |
示例
代理配置
注意
如果你正在运行 Docker Desktop,请参阅Docker Desktop 手册。
如果你位于 HTTP 代理服务器后面(例如在企业设置中),你可能需要配置 Docker daemon 以便其使用代理服务器执行拉取和推送镜像等操作。daemon 可以通过以下三种方式配置:
- 使用环境变量(
HTTP_PROXY
、HTTPS_PROXY
和NO_PROXY
)。 - 在daemon 配置文件中使用
http-proxy
、https-proxy
和no-proxy
字段(Docker Engine 23.0 或更高版本)。 - 使用
--http-proxy
、--https-proxy
和--no-proxy
命令行选项。(Docker Engine 23.0 或更高版本)。
命令行选项和配置文件选项优先于环境变量。请参阅使用 systemd 控制和配置 Docker,了解如何在在使用 systemd
的主机上设置这些环境变量。
Daemon socket 选项
Docker daemon 可以通过三种不同类型的 Socket 监听 Docker Engine API 请求:unix
、tcp
和 fd
。
默认情况下,在 /var/run/docker.sock
创建一个 unix
域套接字(或 IPC 套接字),这需要 root
权限或 docker
组成员身份。
如果需要远程访问 Docker daemon,需要启用 tcp Socket。使用 TCP 套接字时,Docker daemon 默认提供未加密且未经身份验证的直接访问。你应该使用内置的 HTTPS 加密套接字或在其前面放置一个安全的 Web 代理来保护 daemon。你可以使用 -H tcp://0.0.0.0:2375
在所有网络接口上监听端口 2375
,或者使用其 IP 地址在特定网络接口上监听:-H tcp://192.168.59.103:2375
。通常,端口 2375
用于未加密通信,端口 2376
用于与 daemon 的加密通信。
注意
如果你正在使用 HTTPS 加密套接字,请记住仅支持 TLS 版本 1.0 及更高版本。出于安全原因,不支持 SSLv3 及以下协议。
在基于 systemd 的系统上,你可以通过 systemd 套接字激活与 daemon 通信,使用 dockerd -H fd://
。使用 fd://
适用于大多数设置,但你也可以指定单个套接字:dockerd -H fd://3
。如果找不到指定的套接字激活文件,daemon 将退出。你可以在Docker 源代码树中找到使用 systemd 套接字激活与 Docker 和 systemd 的示例。
你可以使用多个 -H
选项配置 Docker daemon 同时监听多个套接字
下面的示例运行了监听默认 Unix 套接字以及此主机上 2 个特定 IP 地址的 daemon
$ sudo dockerd -H unix:///var/run/docker.sock -H tcp://192.168.59.106 -H tcp://10.10.10.2
Docker client 遵守 DOCKER_HOST
环境变量来为 client 设置 -H
标志。请使用以下命令中的一个
$ docker -H tcp://0.0.0.0:2375 ps
$ export DOCKER_HOST="tcp://0.0.0.0:2375"
$ docker ps
将 DOCKER_TLS_VERIFY
环境变量设置为非空字符串的任何值等同于设置 --tlsverify
标志。以下是等效的
$ docker --tlsverify ps
# or
$ export DOCKER_TLS_VERIFY=1
$ docker ps
Docker client 遵守 HTTP_PROXY
、HTTPS_PROXY
和 NO_PROXY
环境变量(或它们的小写版本)。HTTPS_PROXY
优先于 HTTP_PROXY
。
Docker client 支持通过 SSH 连接到远程 daemon
$ docker -H ssh://me@example.com:22/var/run/docker.sock ps
$ docker -H ssh://me@example.com:22 ps
$ docker -H ssh://me@example.com ps
$ docker -H ssh://example.com ps
要使用 SSH 连接,你需要设置 ssh
,使其能够通过公钥认证连接到远程主机。不支持密码认证。如果你的密钥受密码保护,你需要设置 ssh-agent
。
将 Docker 绑定到另一个主机/端口或 Unix 套接字
警告
将默认的
docker
daemon 绑定更改为 TCP 端口或 Unixdocker
用户组会引入安全风险,因为它可能允许非 root 用户在主机上获得 root 访问权限。请确保你控制对docker
的访问。如果绑定到 TCP 端口,任何有权访问该端口的人都拥有完整的 Docker 访问权限;因此,在开放网络上不建议这样做。
使用 -H
可以让 Docker daemon 监听特定的 IP 和端口。默认情况下,它监听 unix:///var/run/docker.sock
,仅允许 root 用户进行本地连接。你可以将其设置为 0.0.0.0:2375
或特定的主机 IP,以允许所有人访问,但由于有人可能会获得运行 daemon 的主机的 root 访问权限,因此不建议这样做。
类似地,Docker client 可以使用 -H
连接到自定义端口。在 Linux 上,Docker client 默认连接到 unix:///var/run/docker.sock
,在 Windows 上默认连接到 tcp://127.0.0.1:2376
。
-H
接受以下格式的主机和端口分配
tcp://[host]:[port][path] or unix://path
例如
tcp://
-> 当 TLS 加密开启时,连接到127.0.0.1
的端口2376
;当通信为明文时,连接到端口2375
的 TCP 连接。tcp://host:2375
-> host:2375 上的 TCP 连接tcp://host:2375/path
-> host:2375 上的 TCP 连接,并将 path 前置到所有请求unix://path/to/socket
-> 位于path/to/socket
的 Unix 套接字
当 -H
为空时,其默认值与未传入 -H
时相同。
-H
也接受 TCP 绑定的简写形式:host:
或 host:port
或 :port
以 daemon 模式运行 Docker
$ sudo <path to>/dockerd -H 0.0.0.0:5555 &
下载一个 ubuntu
镜像
$ docker -H :5555 pull ubuntu
你可以使用多个 -H
,例如,如果你想同时监听 TCP 和 Unix 套接字
$ sudo dockerd -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock &
# Download an ubuntu image, use default Unix socket
$ docker pull ubuntu
# OR use the TCP port
$ docker -H tcp://127.0.0.1:2375 pull ubuntu
Daemon 存储驱动
在 Linux 上,Docker daemon 支持多种不同的镜像层存储驱动:overlay2
、fuse-overlayfs
、btrfs
和 zfs
。
overlay2
是所有当前支持的 Linux 发行版的首选存储驱动,并且是默认选择的驱动。除非用户有充分的理由偏好其他存储驱动,否则应使用 overlay2
。
你可以在选择存储驱动中找到更多关于存储驱动以及如何选择驱动的信息。
在 Windows 上,Docker daemon 仅支持 windowsfilter
存储驱动。
每个存储驱动的选项
可以使用 --storage-opt
标志指定的选项来配置特定的存储驱动。zfs
的选项以 zfs
开头,btrfs
的选项以 btrfs
开头。
ZFS 选项
zfs.fsname
指定 daemon 应使用哪个 ZFS 文件系统来创建其数据集。默认情况下,使用 /var/lib/docker
中的 ZFS 文件系统。
示例
$ sudo dockerd -s zfs --storage-opt zfs.fsname=zroot/docker
Btrfs 选项
btrfs.min_space
指定创建用于容器的子卷时使用的最小大小。如果用户在使用 --storage-opt size 选项创建或运行容器时为 btrfs 使用了磁盘配额,Docker 应确保 size 不能小于 btrfs.min_space。
示例
$ sudo dockerd -s btrfs --storage-opt btrfs.min_space=10G
Overlay2 选项
overlay2.size
设置容器的默认最大大小。仅当后端文件系统为 xfs
并以 pquota
挂载选项挂载时才支持此选项。在这些条件下,用户可以传递任何小于后端文件系统大小的值。
示例
$ sudo dockerd -s overlay2 --storage-opt overlay2.size=1G
Windowsfilter 选项
size
指定创建用于容器的沙箱时使用的大小。默认为 20G。
示例
C:\> dockerd --storage-opt size=40G
运行时选项
Docker daemon 依赖于 OCI 兼容的运行时(通过 containerd daemon 调用),以此作为与 Linux 内核 namespaces、cgroups 和 SELinux 的接口。
配置容器运行时
默认情况下,Docker daemon 使用 runc 作为容器运行时。您可以配置 daemon 以添加其他运行时。
安装在 PATH
上的 containerd shim 可以直接使用,无需编辑 daemon 的配置。例如,如果您将 Kata Containers shim (containerd-shim-kata-v2
) 安装在 PATH
上,则可以使用 docker run
选择该运行时,而无需编辑 daemon 的配置。
$ docker run --runtime io.containerd.kata.v2
未实现 containerd shim 的容器运行时,或者安装在 PATH
外部的 containerd shim,必须通过配置文件或使用 --add-runtime
命令行标志向 daemon 注册。
有关如何使用其他容器运行时的示例,请参阅 替代容器运行时
使用 daemon.json
配置运行时
要使用 daemon 的配置文件注册和配置容器运行时,请将运行时作为条目添加到 runtimes
下:
{
"runtimes": {
"<runtime>": {}
}
}
条目的键(上例中的 <runtime>
)代表运行时的名称。这就是您在使用 docker run --runtime <runtime>
运行容器时引用的名称。
运行时条目包含一个对象,用于指定运行时的配置。该对象的属性取决于您要注册的运行时类型。
如果运行时实现了自己的 containerd shim,则该对象应包含一个
runtimeType
字段和一个可选的options
字段。{ "runtimes": { "<runtime>": { "runtimeType": "<name-or-path>", "options": {} } } }
请参阅 配置 shim。
如果运行时被设计为 runc 的直接替代品(drop-in replacement),则该对象包含一个
path
字段和一个可选的runtimeArgs
字段。{ "runtimes": { "<runtime>": { "path": "/path/to/bin", "runtimeArgs": ["...args"] } } }
请参阅 配置 runc 直接替代品。
在配置文件中更改运行时配置后,必须重新加载或重启 daemon 才能使更改生效。
$ sudo systemctl reload dockerd
配置 containerd shim
如果您要注册的运行时实现了 containerd shim,或者您要注册的运行时使用了 runc shim,请使用以下格式的运行时条目:
{
"runtimes": {
"<runtime>": {
"runtimeType": "<name-or-path>",
"options": {}
}
}
}
runtimeType
指的是:
containerd shim 的完全限定名。
shim 的完全限定名与在 containerd 的 CRI 配置中注册运行时使用的
runtime_type
相同。例如,io.containerd.runsc.v1
。containerd shim 可执行文件的路径。
如果您将 containerd shim 可执行文件安装在
PATH
外部,此选项非常有用。
options
是可选的。它允许您指定要用于 shim 的运行时配置。您可以在 options
中指定的配置参数取决于您注册的运行时。对于大多数 shim,支持的配置选项是 TypeUrl
和 ConfigPath
。例如:
{
"runtimes": {
"gvisor": {
"runtimeType": "io.containerd.runsc.v1",
"options": {
"TypeUrl": "io.containerd.runsc.v1.options",
"ConfigPath": "/etc/containerd/runsc.toml"
}
}
}
}
您可以使用相同的 runtimeType
配置多个运行时。例如:
{
"runtimes": {
"gvisor-foo": {
"runtimeType": "io.containerd.runsc.v1",
"options": {
"TypeUrl": "io.containerd.runsc.v1.options",
"ConfigPath": "/etc/containerd/runsc-foo.toml"
}
},
"gvisor-bar": {
"runtimeType": "io.containerd.runsc.v1",
"options": {
"TypeUrl": "io.containerd.runsc.v1.options",
"ConfigPath": "/etc/containerd/runsc-bar.toml"
}
}
}
}
与 "runtimeType": "io.containerd.runc.v2"
一起使用时,options
字段会采用一组特殊的配置参数。有关 runc 参数的更多信息,请参阅 CRI 插件配置指南 中的 runc 配置部分。
配置 runc 直接替代品
如果您要注册的运行时可以作为 runc 的直接替代品,则可以使用 daemon 配置文件或 dockerd
CLI 的 --add-runtime
标志来注册该运行时。
使用配置文件时,条目使用以下格式:
{
"runtimes": {
"<runtime>": {
"path": "/path/to/binary",
"runtimeArgs": ["...args"]
}
}
}
其中 path
是运行时可执行文件的绝对路径,或者是安装在 PATH
中的可执行文件的名称。
{
"runtimes": {
"runc": {
"path": "runc"
}
}
}
runtimeArgs
允许您选择性地向运行时传递附加参数。使用此格式的条目使用 containerd runc shim 来调用自定义运行时可执行文件。
使用 --add-runtime
CLI 标志时,请使用以下格式:
$ sudo dockerd --add-runtime <runtime>=<path>
不支持通过命令行定义运行时参数。
有关 runc 直接替代品的配置示例,请参阅 替代容器运行时 > youki
配置默认容器运行时
您可以指定完全限定的 containerd 运行时 shim 的名称,或已注册运行时的名称。您可以使用 daemon 配置文件或 dockerd
CLI 的 --default-runtime
标志来指定默认运行时。
使用配置文件时,条目使用以下格式:
{
"default-runtime": "io.containerd.runsc.v1"
}
使用 --default-runtime
CLI 标志时,请使用以下格式:
$ dockerd --default-runtime io.containerd.runsc.v1
独立运行 containerd
默认情况下,Docker daemon 会自动启动 containerd
。如果您想控制 containerd
的启动,请手动启动 containerd
并使用 --containerd
标志传递 containerd
socket 的路径。例如:
$ sudo dockerd --containerd /run/containerd/containerd.sock
配置 cgroup 驱动程序
您可以使用 --exec-opt native.cgroupdriver
CLI 标志配置运行时应如何管理容器 cgroups。
您只能指定 cgroupfs
或 systemd
。如果您指定 systemd
但其不可用,系统将报错退出。如果您省略 native.cgroupdriver
选项,则在 cgroup v1 主机上使用 cgroupfs
,在 systemd 可用的 cgroup v2 主机上使用 systemd
。
此示例将 cgroupdriver
设置为 systemd
:
$ sudo dockerd --exec-opt native.cgroupdriver=systemd
此选项的设置适用于 daemon 启动的所有容器。
配置容器隔离技术 (Windows)
对于 Windows 容器,您可以使用 --exec-opt isolation
标志指定要使用的默认容器隔离技术。
以下示例将 hyperv
设置为默认隔离技术:
> dockerd --exec-opt isolation=hyperv
如果在 daemon 启动时未指定隔离值,则在 Windows 客户端上默认为 hyperv
,在 Windows 服务器上默认为 process
。
Daemon DNS 选项
要为所有 Docker 容器设置 DNS 服务器,请使用:
$ sudo dockerd --dns 8.8.8.8
要为所有 Docker 容器设置 DNS 搜索域,请使用:
$ sudo dockerd --dns-search example.com
不安全的注册表
在本节中,“registry” 指的是私有仓库(registry),myregistry:5000
是私有仓库的占位符示例。
Docker 将私有仓库视为安全或不安全。安全的仓库使用 TLS,并且其 CA 证书的副本放置在 Docker 主机上的 /etc/docker/certs.d/myregistry:5000/ca.crt
。不安全的仓库要么不使用 TLS(即,侦听纯文本 HTTP),要么使用 Docker daemon 不知道的 CA 证书的 TLS。后一种情况可能发生在 /etc/docker/certs.d/myregistry:5000/
下找不到证书,或者证书验证失败(即,错误的 CA)时。
默认情况下,Docker 假设所有仓库都是安全的,除了本地仓库。如果 Docker 假设仓库是安全的,则无法与不安全的仓库通信。为了与不安全的仓库通信,Docker daemon 需要使用以下两种形式之一的 --insecure-registry
:
--insecure-registry myregistry:5000
告诉 Docker daemon 应将 myregistry:5000 视为不安全。--insecure-registry 10.1.0.0/16
告诉 Docker daemon,所有解析为 CIDR 语法描述的子网内的 IP 地址的仓库都应被视为不安全。
此标志可以多次使用,以允许将多个仓库标记为不安全。
如果未将不安全的仓库标记为不安全,docker pull
、docker push
和 docker search
将导致错误消息,提示用户要么保护(使安全)该仓库,要么如上所述向 Docker daemon 传递 --insecure-registry
标志。
IP 地址落在 127.0.0.0/8 范围内的本地仓库,从 Docker 1.3.2 开始会自动标记为不安全。不建议依赖此行为,因为它将来可能会改变。
启用 --insecure-registry
,即允许未加密和/或不受信任的通信,在运行本地仓库时可能有用。但是,由于其使用会产生安全漏洞,因此应仅为测试目的启用它。为了增强安全性,用户应将其 CA 添加到系统的可信 CA 列表,而不是启用 --insecure-registry
。
传统仓库
不再支持仅支持传统 v1 协议的仓库上的操作。具体来说,daemon 不会尝试推送到、拉取或登录到 v1 仓库。例外是 search
,它仍然可以在 v1 仓库上执行。
在 HTTPS_PROXY 后运行 Docker daemon
在使用了 HTTPS
代理的 LAN 内运行时,代理的证书会替换 Docker Hub 的证书。这些证书必须添加到您的 Docker 主机配置中:
- 为您的发行版安装
ca-certificates
软件包。 - 向您的网络管理员索取代理的 CA 证书,并将其附加到
/etc/pki/tls/certs/ca-bundle.crt
。 - 然后使用
HTTPS_PROXY=http://username:password@proxy:port/ dockerd
启动 Docker daemon。username:
和password@
是可选的 - 仅在您的代理配置为需要身份验证时才需要。
这仅将代理和身份验证添加到 Docker daemon 的请求中。要在构建镜像和运行容器时使用代理,请参阅 配置 Docker 使用代理服务器。
默认 ulimit
设置
--default-ulimit
标志允许您为所有容器设置要使用的默认 ulimit
选项。它接受与 docker run
的 --ulimit
相同的选项。如果未设置这些默认值,则 ulimit
设置会从 Docker daemon 继承。传递给 docker run
的任何 --ulimit
选项都会覆盖 daemon 的默认设置。
使用 ulimit
标志设置 nproc
时要小心,因为 nproc
被 Linux 设计用于设置用户而非容器可用的最大进程数。有关详细信息,请参阅 docker run
参考。
访问授权
Docker 的访问授权可以通过您的组织可以购买或自己构建的授权插件进行扩展。在启动 Docker daemon
时,您可以使用 --authorization-plugin=PLUGIN_ID
选项安装一个或多个授权插件。
$ sudo dockerd --authorization-plugin=plugin1 --authorization-plugin=plugin2,...
PLUGIN_ID
的值是插件的名称或其规范文件的路径。插件的实现决定了您是指定名称还是路径。请咨询您的 Docker 管理员以获取有关可用插件的信息。
安装插件后,通过命令行或 Docker 的 Engine API 发送到 daemon
的请求将由插件允许或拒绝。如果您安装了多个插件,则每个插件必须按顺序允许该请求才能完成。
有关如何创建授权插件的信息,请参阅 授权插件 部分。
Daemon 用户命名空间选项
Linux 内核的 用户命名空间支持通过使进程(以及因此容器)拥有一个独立的用户和组 ID 范围,该范围位于主机系统使用的传统用户和组范围之外,从而提供了额外的安全性。最重要的安全改进之一是,默认情况下,以 root
用户身份运行的容器进程在容器内部具有预期的管理权限(带有一些限制),但在主机上实际上映射到一个无特权的 uid
。
有关如何使用此功能以及其限制的详细信息,请参阅 使用用户命名空间隔离容器。
配置主机网关 IP
Docker daemon 支持 docker run
和 docker build
命令的 --add-host
标志的特殊值 host-gateway
。此值解析为主机上的地址,以便容器可以连接到在主机上运行的服务。
默认情况下,host-gateway
解析为默认网桥的 IPv4 地址,如果存在 IPv6 地址,则解析为其 IPv6 地址。
您可以使用 dockerd 命令行界面的 --host-gateway-ip
标志或 daemon 配置文件中的 host-gateway-ip
键将其配置为解析为不同的 IP 地址。
要在命令行上提供 IPv4 和 IPv6 地址,请使用两个 --host-gateway-ip
选项。
要在 daemon 配置文件中提供地址,请使用带有 JSON 数组的 "host-gateway-ips"
键,如下所示。为了兼容旧版本的 daemon,也可以在选项 "host-gateway-ip"
中将单个 IP 地址指定为 JSON 字符串。
$ cat > /etc/docker/daemon.json
{ "host-gateway-ips": ["192.0.2.1", "2001:db8::1111"]}
$ sudo systemctl restart docker
$ docker run -it --add-host host.docker.internal:host-gateway \
busybox ping host.docker.internal
PING host.docker.internal (192.0.2.1): 56 data bytes
$ docker run -it --add-host host.docker.internal:host-gateway \
busybox ping -6 host.docker.internal
PING host.docker.internal (2001:db8::1111): 56 data bytes
启用 CDI 设备
注意
这是一项实验性功能,因此不代表稳定的 API。
此功能默认未启用。要启用此功能,请在
daemon.json
配置文件中将features.cdi
设置为true
。
容器设备接口 (CDI) 是一种标准化机制,用于容器运行时创建能够与第三方设备交互的容器。
如果请求的设备规范在 daemon 的文件系统上可用,Docker daemon 支持使用 CDI 设备运行容器。
默认的规范目录是:
/etc/cdi/
用于静态 CDI 规范/var/run/cdi
用于生成的 CDI 规范
或者,您可以使用 daemon.json
配置文件中的 cdi-spec-dirs
选项,或 dockerd
CLI 的 --cdi-spec-dir
标志来设置 CDI 规范的自定义位置。
{
"features": {
"cdi": true
},
"cdi-spec-dirs": ["/etc/cdi/", "/var/run/cdi"]
}
为 daemon 启用 CDI 后,您可以使用 docker info
命令查看已配置的 CDI 规范目录。
Daemon 日志格式
--log-format
选项或 daemon 配置文件中的 "log-format" 选项允许您设置 daemon 生成的日志的格式。日志格式只能通过 --log-format
命令行选项或配置文件中的 "log-format" 字段进行配置;同时使用命令行选项和配置文件中的 "log-format" 字段将导致错误。如果未设置此选项,则默认为 "text"。
以下示例通过 --log-format
命令行选项配置 daemon 使用 json
格式的日志:
$ dockerd --log-format=json
# ...
{"level":"info","msg":"API listen on /var/run/docker.sock","time":"2024-09-16T11:06:08.558145428Z"}
以下示例显示了设置了 "log-format" 的 daemon.json
配置文件:
{
"log-format": "json"
}
杂项选项
IP 伪装(IP masquerading)使用地址转换,允许没有公共 IP 的容器与互联网上的其他机器通信。这可能会干扰某些网络拓扑,可以使用 --ip-masq=false
禁用。
Docker 支持对 Docker 数据目录 (/var/lib/docker
) 和对 /var/lib/docker/tmp
使用软链接(soft links)。DOCKER_TMPDIR
和数据目录可以这样设置:
$ export DOCKER_TMPDIR=/mnt/disk2/tmp
$ sudo -E dockerd --data-root /var/lib/docker -H unix://
默认 cgroup 父级
--cgroup-parent
选项允许您为容器设置默认的 cgroup 父级。如果未设置此选项,则对于 cgroupfs 驱动程序,默认为 /docker
,对于 systemd cgroup 驱动程序,默认为 system.slice
。
如果 cgroup 带有前导斜杠 (/
),则 cgroup 将在根 cgroup 下创建,否则 cgroup 将在 daemon cgroup 下创建。
假设 daemon 在 cgroup daemoncgroup
中运行,则 --cgroup-parent=/foobar
会在 /sys/fs/cgroup/memory/foobar
中创建一个 cgroup,而使用 --cgroup-parent=foobar
则会在 /sys/fs/cgroup/memory/daemoncgroup/foobar
中创建一个 cgroup。
systemd cgroup 驱动程序对于 --cgroup-parent
有不同的规则。systemd 通过 slice 表示层次结构,slice 的名称编码了在树中的位置。因此,systemd cgroup 的 --cgroup-parent
应该是 slice 名称。名称可以是由短划线分隔的一系列名称组成,描述了从根 slice 到该 slice 的路径。例如,--cgroup-parent=user-a-b.slice
意味着容器的内存 cgroup 在 /sys/fs/cgroup/memory/user.slice/user-a.slice/user-a-b.slice/docker-<id>.scope
中创建。
此设置也可以按容器设置,使用 docker create
和 docker run
上的 --cgroup-parent
选项,并且优先级高于 daemon 上的 --cgroup-parent
选项。
Daemon 指标
--metrics-addr
选项接受一个 TCP 地址来提供指标 API 服务。此功能仍处于实验阶段,因此 daemon 必须在实验模式下运行才能使此功能正常工作。
要在 localhost:9323
上提供指标 API 服务,您可以指定 --metrics-addr 127.0.0.1:9323
,允许您在 127.0.0.1:9323/metrics
的 API 上发出请求以接收 prometheus 格式的指标。
Port 9323
is the default port associated with Docker metrics,以避免与其他的 Prometheus 导出器和服务冲突。
如果您正在运行 Prometheus 服务器,可以将此地址添加到您的抓取配置中,以便 Prometheus 收集 Docker 指标。有关更多信息,请参阅 使用 Prometheus 收集 Docker 指标。
节点通用资源
--node-generic-resources
选项接受一个键值对列表 (key=value
),允许您在 Swarm 集群中宣告用户定义的资源。
当前预期的用例是宣告 NVIDIA GPU,以便请求 NVIDIA-GPU=[0-16]
的服务可以调度到具有足够 GPU 来运行任务的节点上。
用法示例
{
"node-generic-resources": [
"NVIDIA-GPU=UUID1",
"NVIDIA-GPU=UUID2"
]
}
在 daemon 中启用功能 (--feature)
--feature
选项允许您启用或禁用 daemon 中的某个功能。此选项与 daemon.json 配置文件中的 "features" 字段对应。功能只能通过 --feature
命令行选项或配置文件中的 "features" 字段进行配置;同时使用命令行选项和配置文件中的 "features" 字段将导致错误。可以多次指定 --feature
选项来配置多个功能。--feature
选项接受一个名称和一个可选的布尔值。省略值时,默认为 true
。
以下示例使用启用了 cdi
和 containerd-snapshotter
功能的 daemon 运行。为 cdi
选项提供了值:
$ dockerd --feature cdi=true --feature containerd-snapshotter
以下示例是使用 daemon.json
配置文件的等效方法:
{
"features": {
"cdi": true,
"containerd-snapshotter": true
}
}
Daemon 配置文件
--config-file
选项允许您以 JSON 格式设置 daemon 的任何配置选项。此文件使用与标志相同的名称作为键,但允许多个条目的标志除外,对于这些标志,它使用标志名称的复数形式,例如,labels
对应于 label
标志。
配置文件中设置的选项不得与使用标志设置的选项冲突。如果文件和标志之间存在重复的选项,无论其值如何,Docker daemon 都将启动失败。这是有意为之的,可以避免在配置重新加载时静默忽略更改。例如,如果您在配置文件中设置了 daemon 标签,同时通过 --label
标志设置了 daemon 标签,则 daemon 将启动失败。文件中不存在的选项在 daemon 启动时将被忽略。
--validate
选项允许您在不启动 Docker daemon 的情况下验证配置文件。对于无效的配置文件,将返回非零退出代码。
$ dockerd --validate --config-file=/tmp/valid-config.json
configuration OK
$ echo $?
0
$ dockerd --validate --config-file /tmp/invalid-config.json
unable to configure the Docker daemon with file /tmp/invalid-config.json: the following directives don't match any configuration option: unknown-option
$ echo $?
1
在 Linux 上
在 Linux 上,配置文件的默认位置是 /etc/docker/daemon.json
。使用 --config-file
标志指定非默认位置。
以下是 Linux 上允许的配置选项的完整示例:
{
"authorization-plugins": [],
"bip": "",
"bip6": "",
"bridge": "",
"builder": {
"gc": {
"enabled": true,
"defaultKeepStorage": "10GB",
"policy": [
{ "keepStorage": "10GB", "filter": ["unused-for=2200h"] },
{ "keepStorage": "50GB", "filter": ["unused-for=3300h"] },
{ "keepStorage": "100GB", "all": true }
]
}
},
"cgroup-parent": "",
"containerd": "/run/containerd/containerd.sock",
"containerd-namespace": "docker",
"containerd-plugins-namespace": "docker-plugins",
"data-root": "",
"debug": true,
"default-address-pools": [
{
"base": "172.30.0.0/16",
"size": 24
},
{
"base": "172.31.0.0/16",
"size": 24
}
],
"default-cgroupns-mode": "private",
"default-gateway": "",
"default-gateway-v6": "",
"default-network-opts": {},
"default-runtime": "runc",
"default-shm-size": "64M",
"default-ulimits": {
"nofile": {
"Hard": 64000,
"Name": "nofile",
"Soft": 64000
}
},
"dns": [],
"dns-opts": [],
"dns-search": [],
"exec-opts": [],
"exec-root": "",
"experimental": false,
"features": {
"cdi": true,
"containerd-snapshotter": true
},
"fixed-cidr": "",
"fixed-cidr-v6": "",
"group": "",
"host-gateway-ip": "",
"hosts": [],
"proxies": {
"http-proxy": "http://proxy.example.com:80",
"https-proxy": "https://proxy.example.com:443",
"no-proxy": "*.test.example.com,.example.org"
},
"icc": false,
"init": false,
"init-path": "/usr/libexec/docker-init",
"insecure-registries": [],
"ip": "0.0.0.0",
"ip-forward": false,
"ip-masq": false,
"iptables": false,
"ip6tables": false,
"ipv6": false,
"labels": [],
"live-restore": true,
"log-driver": "json-file",
"log-format": "text",
"log-level": "",
"log-opts": {
"cache-disabled": "false",
"cache-max-file": "5",
"cache-max-size": "20m",
"cache-compress": "true",
"env": "os,customer",
"labels": "somelabel",
"max-file": "5",
"max-size": "10m"
},
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
"max-download-attempts": 5,
"mtu": 0,
"no-new-privileges": false,
"node-generic-resources": [
"NVIDIA-GPU=UUID1",
"NVIDIA-GPU=UUID2"
],
"pidfile": "",
"raw-logs": false,
"registry-mirrors": [],
"runtimes": {
"cc-runtime": {
"path": "/usr/bin/cc-runtime"
},
"custom": {
"path": "/usr/local/bin/my-runc-replacement",
"runtimeArgs": [
"--debug"
]
}
},
"seccomp-profile": "",
"selinux-enabled": false,
"shutdown-timeout": 15,
"storage-driver": "",
"storage-opts": [],
"swarm-default-advertise-addr": "",
"tls": true,
"tlscacert": "",
"tlscert": "",
"tlskey": "",
"tlsverify": true,
"userland-proxy": false,
"userland-proxy-path": "/usr/libexec/docker-proxy",
"userns-remap": ""
}
注意
您不能在
daemon.json
中设置已在 daemon 启动时作为标志设置的选项。在使用 systemd 启动 Docker daemon 的系统上,-H
已经设置,因此您不能在daemon.json
中使用hosts
键来添加侦听地址。有关如何使用 systemd drop-in 文件配置 daemon 的示例,请参阅 自定义 Docker daemon 选项 for an example on how to configure the daemon using systemd drop-in files.
在 Windows 上
在 Windows 上,配置文件的默认位置是 %programdata%\docker\config\daemon.json
。使用 --config-file
标志指定非默认位置。
以下是 Windows 上允许的配置选项的完整示例:
{
"authorization-plugins": [],
"bridge": "",
"containerd": "\\\\.\\pipe\\containerd-containerd",
"containerd-namespace": "docker",
"containerd-plugins-namespace": "docker-plugins",
"data-root": "",
"debug": true,
"default-network-opts": {},
"default-runtime": "",
"default-ulimits": {},
"dns": [],
"dns-opts": [],
"dns-search": [],
"exec-opts": [],
"experimental": false,
"features": {},
"fixed-cidr": "",
"group": "",
"host-gateway-ip": "",
"hosts": [],
"insecure-registries": [],
"labels": [],
"log-driver": "",
"log-format": "text",
"log-level": "",
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
"max-download-attempts": 5,
"mtu": 0,
"pidfile": "",
"raw-logs": false,
"registry-mirrors": [],
"shutdown-timeout": 15,
"storage-driver": "",
"storage-opts": [],
"swarm-default-advertise-addr": "",
"tlscacert": "",
"tlscert": "",
"tlskey": "",
"tlsverify": true
}
default-runtime
选项默认未设置,此时 dockerd 会自动检测运行时。此检测基于 containerd
标志是否已设置。
接受的值
com.docker.hcsshim.v1
- 这是 Docker 自添加 Windows 支持以来一直使用的内置运行时,并使用 Windows 中的 v1 HCS API。io.containerd.runhcs.v1
- 这使用 containerd 的runhcs
shim 来运行容器,并使用 Windows 中的 v2 HCS API。
功能选项
daemon.json
中的可选字段 features
允许您启用或禁用特定的 daemon 功能。
{
"features": {
"some-feature": true,
"some-disabled-feature-enabled-by-default": false
}
}
功能选项列表包括:
containerd-snapshotter
:设置为true
时,daemon 使用 containerd 快照器(snapshotters)而不是传统的存储驱动程序来存储镜像和容器数据。有关更多信息,请参阅 containerd 存储。windows-dns-proxy
:设置为true
时,daemon 的内部 DNS 解析器会将请求转发到外部服务器。没有此设置,在容器中运行的大多数应用程序仍能使用容器本身配置的辅助 DNS 服务器,但nslookup
将无法解析外部名称。当前默认为false
,在未来版本中将更改为true
。此选项仅在 Windows 上允许。警告
windows-dns-proxy
功能标志将在未来版本中移除。
配置重新加载行为
某些选项可以在 daemon 运行时重新配置,而无需重启进程。daemon 在 Linux 中使用 SIGHUP
信号进行重新加载,在 Windows 中使用键为 Global\docker-daemon-config-$PID
的全局事件。您可以修改配置文件中的选项,但 daemon 仍然会检查与指定 CLI 标志的冲突设置。如果存在冲突,daemon 将无法重新配置自身,但不会停止执行。
当前支持的可以重新配置的选项列表如下:
选项 | 描述 |
---|---|
debug | 切换 daemon 的 debug 模式。 |
labels | 将 daemon 标签替换为一组新的标签。 |
live-restore | 切换 live restore。 |
max-concurrent-downloads | 配置每次 pull 的最大并发下载数。 |
max-concurrent-uploads | 配置每次 push 的最大并发上传数。 |
max-download-attempts | 配置每次 pull 的最大下载尝试次数。 |
default-runtime | 配置在创建容器时未指定运行时的情况下使用的运行时。 |
runtimes | 配置可用于运行容器的可用 OCI 运行时列表。 |
authorization-plugin | 指定要使用的授权插件。 |
insecure-registries | 指定 daemon 应视为不安全的仓库列表。 |
registry-mirrors | 指定仓库镜像列表。 |
shutdown-timeout | 配置 daemon 现有的配置超时,并使用新的超时设置关闭所有容器。 |
features | 启用或禁用特定功能。 |
运行多个 daemon
注意
在单个主机上运行多个 daemon 被认为是实验性的。您可能会遇到未解决的问题,并且在某些情况下可能无法按预期工作。
本节介绍如何在单个主机上运行多个 Docker daemon。要运行多个 daemon,您必须配置每个 daemon,使其不会与同一主机上的其他 daemon 冲突。您可以通过将这些选项作为标志提供,或使用 daemon 配置文件来设置这些选项。
以下 daemon 选项必须为每个 daemon 配置:
-b, --bridge= Attach containers to a network bridge
--exec-root=/var/run/docker Root of the Docker execdriver
--data-root=/var/lib/docker Root of persisted Docker data
-p, --pidfile=/var/run/docker.pid Path to use for daemon PID file
-H, --host=[] Daemon socket(s) to connect to
--iptables=true Enable addition of iptables rules
--config-file=/etc/docker/daemon.json Daemon configuration file
--tlscacert="~/.docker/ca.pem" Trust certs signed only by this CA
--tlscert="~/.docker/cert.pem" Path to TLS certificate file
--tlskey="~/.docker/key.pem" Path to TLS key file
当您的 daemon 对这些标志使用不同的值时,您可以在同一主机上运行它们而不会出现任何问题。重要的是您理解这些选项的含义并正确使用它们。
-b, --bridge=
标志默认为docker0
作为默认网桥网络。它在您安装 Docker 时自动创建。如果您不使用默认设置,则必须手动创建和配置网桥,或将其设置为 'none':--bridge=none
。--exec-root
是存储容器状态的路径。默认值为/var/run/docker
。在此处指定正在运行的 daemon 的路径。--data-root
是存储持久化数据(如镜像、卷和集群状态)的路径。默认值为/var/lib/docker
。为避免与其他 daemon 发生冲突,请为每个 daemon 分别设置此参数。-p, --pidfile=/var/run/docker.pid
是存储 daemon 进程 ID 的路径。在此处指定 PID 文件的路径。--host=[]
指定 Docker daemon 侦听客户端连接的位置。如果未指定,默认为/var/run/docker.sock
。--iptables=false
阻止 Docker daemon 添加 iptables 规则。如果多个 daemon 管理 iptables 规则,它们可能会覆盖由其他 daemon 设置的规则。请注意,禁用此选项需要您手动添加 iptables 规则以暴露容器端口。如果您阻止 Docker 添加 iptables 规则,即使您将--ip-masq
设置为true
,Docker 也不会添加 IP 伪装规则。在没有 IP 伪装规则的情况下,当使用默认网桥以外的网络时,Docker 容器无法连接到外部主机或互联网。--config-file=/etc/docker/daemon.json
是配置文件存储的路径。你可以使用它来代替守护进程标志。为每个守护进程指定路径。--tls*
Docker 守护进程支持--tlsverify
模式,该模式强制使用加密和认证的远程连接。--tls*
选项允许为单独的守护进程使用特定的证书。
不带网络的独立“引导(bootstrap)”Docker 守护进程实例的示例脚本
$ sudo dockerd \
-H unix:///var/run/docker-bootstrap.sock \
-p /var/run/docker-bootstrap.pid \
--iptables=false \
--ip-masq=false \
--bridge=none \
--data-root=/var/lib/docker-bootstrap \
--exec-root=/var/run/docker-bootstrap
默认网络选项
daemon.json
配置文件中的 default-network-opts
键,以及等效的 --default-network-opt
CLI 标志,允许你为新建网络的驱动程序网络驱动程序选项指定默认值。
以下示例展示了如何使用 daemon.json
文件配置 bridge
驱动程序的选项。
{
"default-network-opts": {
"bridge": {
"com.docker.network.bridge.host_binding_ipv4": "127.0.0.1",
"com.docker.network.driver.mtu": "1234"
}
}
}
此示例使用 bridge
网络驱动程序。有关可用驱动程序选项的概述,请参阅bridge 网络驱动程序页面。
更改配置并重启守护进程后,你创建的新网络将使用这些选项配置作为默认值。
$ docker network create mynet
$ docker network inspect mynet --format "{{json .Options}}"
{"com.docker.network.bridge.host_binding_ipv4":"127.0.0.1","com.docker.network.driver.mtu":"1234"}
请注意,更改此守护进程配置不会影响已有的网络。
使用 --default-network-opt
CLI 标志对于测试和调试非常有用,但对于持久的守护进程配置,你应该首选使用 daemon.json
文件。CLI 标志期望的格式为 driver=opt=value
,例如
$ sudo dockerd \
--default-network-opt bridge=com.docker.network.bridge.host_binding_ipv4=127.0.0.1 \
--default-network-opt bridge=com.docker.network.driver.mtu=1234