docker debug

描述进入任何容器或镜像的 shell。替代使用 `docker exec` 进行调试的方法。
用法debug [OPTIONS] {CONTAINER|IMAGE}

描述

注意

Docker Debug 需要 专业版、团队版或企业版订阅。您必须登录才能使用此命令。

Docker Debug 是一个 CLI 命令,它通过保持镜像小巧且安全来帮助您遵循最佳实践。使用 Docker Debug,您可以在镜像仅包含运行应用程序的最低限度必要组件时对其进行调试。它允许您创建和使用精简的镜像或容器,这些镜像或容器通常难以调试,因为所有工具都已被移除。例如,虽然典型的调试方法如 `docker exec -it my-app bash` 可能无法在精简容器上工作,但 `docker debug` 将可以正常工作。

使用 `docker debug`,您可以进入任何容器或镜像的调试 shell,即使它们不包含 shell。您无需修改镜像即可使用 Docker Debug。但是,使用 Docker Debug 仍然不会修改您的镜像。Docker Debug 自带一个易于自定义的工具箱。该工具箱预装了许多标准的 Linux 工具,例如 `vim`、`nano`、`htop` 和 `curl`。使用内置的 `install` 命令可以添加 https://search.nixos.org/packages 上提供的其他工具。Docker Debug 支持 `bash`、`fish` 和 `zsh`。默认情况下,它会尝试自动检测您的 shell。

自定义内置工具

  • install [tool1] [tool2]:从 https://search.nixos.org/packages 添加 Nix 包,参见 示例
  • uninstall [tool1] [tool2]:卸载 Nix 包。
  • entrypoint:打印、检查或运行入口点,参见 示例
  • builtins:显示自定义内置工具。

注意

对于镜像和已停止的容器,退出 shell 时所有更改都会被丢弃。在任何情况下,更改都不会影响实际的镜像或容器。访问正在运行或已暂停的容器时,所有文件系统更改都会直接对容器可见。`/nix` 目录永远不会对实际的镜像或容器可见。

选项

选项默认值描述
--shellauto选择一个 shell。支持:`bash`、`fish`、`zsh`、`auto`。
-c, --command评估指定的命令,而不是启动交互式会话,参见 示例
--host要连接到的 Daemon docker socket。例如:`ssh://root@example.org`、`unix:///some/path/docker.sock`,参见 示例

示例

调试没有 shell 的容器(精简容器)

`hello-world` 镜像非常简单,只包含 `/hello` 二进制文件。它是精简镜像的一个很好的例子。没有其他工具和 shell。

从 `hello-world` 镜像运行容器

$ docker run --name my-app hello-world

容器会立即退出。要进入内部的调试 shell,请运行

$ docker debug my-app

调试 shell 允许您检查文件系统

docker > ls
dev  etc  hello  nix  proc  sys

文件 `/hello` 是运行容器时执行的二进制文件。您可以通过直接运行它来确认这一点

docker > /hello

运行二进制文件后,它会产生相同的输出。

调试(精简)镜像

您可以通过运行以下命令直接调试镜像:

$ docker debug hello-world
...
docker > ls
dev  etc  hello  nix  proc  sys

您甚至不需要拉取镜像,因为 `docker debug` 会像 `docker run` 命令一样自动执行此操作。

修改正在运行的容器的文件

Docker debug 允许您修改任何正在运行的容器中的文件。工具箱预装了 `vim` 和 `nano`。

运行 nginx 容器并更改默认的 `index.html`

$ docker run -d --name web-app -p 8080:80 nginx
d3d6074d0ea901c96cac8e49e6dad21359616bef3dc0623b3c2dfa536c31dfdb

要确认 nginx 正在运行,请打开浏览器并导航到 http://localhost:8080。您应该会看到 nginx 的默认页面。现在,使用 vim 更改它

vim /usr/share/nginx/html/index.html

将标题更改为“欢迎使用我的应用程序!”并保存文件。现在,重新加载浏览器中的页面,您应该会看到更新后的页面。

使用 `install` 命令管理您的工具箱

内置的 `install` 命令允许您从 https://search.nixos.org/packages 向工具箱添加任何工具。请记住,添加工具永远不会修改实际的镜像或容器。工具只会被添加到您的工具箱中。运行 `docker debug`,然后安装 `nmap`

$ docker debug nginx
...
docker > install nmap
Tip: You can install any package available at: https://search.nixos.org/packages.
installing 'nmap-7.93'
these 2 paths will be fetched (5.58 MiB download, 26.27 MiB unpacked):
/nix/store/brqjf4i23fagizaq2gn4d6z0f406d0kg-lua-5.3.6
/nix/store/xqd17rhgmn6pg85a3g18yqxpcya6d06r-nmap-7.93
copying path '/nix/store/brqjf4i23fagizaq2gn4d6z0f406d0kg-lua-5.3.6' from 'https://cache.nixos.org'...
copying path '/nix/store/xqd17rhgmn6pg85a3g18yqxpcya6d06r-nmap-7.93' from 'https://cache.nixos.org'...
building '/nix/store/k8xw5wwarh8dc1dvh5zx8rlwamxfsk3d-user-environment.drv'...

docker > nmap --version
Nmap version 7.93 ( https://nmap.org )
Platform: x86_64-unknown-linux-gnu
Compiled with: liblua-5.3.6 openssl-3.0.11 libssh2-1.11.0 nmap-libz-1.2.12 libpcre-8.45 libpcap-1.10.4 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select

您可以通过进入不同镜像的调试 shell 来确认 `nmap` 现在是您工具箱的一部分

$ docker debug hello-world
...
docker > nmap --version

Nmap version 7.93 ( https://nmap.org )
Platform: x86_64-unknown-linux-gnu
Compiled with: liblua-5.3.6 openssl-3.0.11 libssh2-1.11.0 nmap-libz-1.2.12 libpcre-8.45 libpcap-1.10.4 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select

docker > exit

`nmap` 仍然存在。

了解容器的默认启动命令(入口点)

Docker Debug 带有一个内置工具 `entrypoint`。进入 `hello-world` 镜像并确认入口点是 `/hello`

$ docker debug hello-world
...
docker > entrypoint --print
/hello

`entrypoint` 命令会评估底层镜像的 `ENTRYPOINT` 和 `CMD` 语句,并允许您打印、检查或运行生成的入口点。但是,很难理解来自 理解 CMD 和 ENTRYPOINT 如何交互 的所有极端情况。在这种情况下,`entrypoint` 可以提供帮助。

使用 `entrypoint` 来调查从 Nginx 镜像运行容器时实际发生了什么

$ docker debug nginx
...
docker > entrypoint
Understand how ENTRYPOINT/CMD work and if they are set correctly.
From CMD in Dockerfile:
 ['nginx', '-g', 'daemon off;']

From ENTRYPOINT in Dockerfile:
 ['/docker-entrypoint.sh']

By default, any container from this image will be started with following   command:

/docker-entrypoint.sh nginx -g daemon off;

path: /docker-entrypoint.sh
args: nginx -g daemon off;
cwd:
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Lint results:
 PASS: '/docker-entrypoint.sh' found
 PASS: no mixing of shell and exec form
 PASS: no double use of shell form

Docs:
- https://docs.dockerd.com.cn/reference/dockerfile/#cmd
- https://docs.dockerd.com.cn/reference/dockerfile/#entrypoint
- https://docs.dockerd.com.cn/reference/dockerfile/#understand-how-cmd-and-entrypoint-interact

输出告诉您,在 nginx 镜像启动时,将使用参数 `nginx -g daemon off;` 执行脚本 `/docker-entrypoint.sh`。您可以使用 `--run` 选项测试入口点

$ docker debug nginx
...
docker > entrypoint --run
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/01/19 17:34:39 [notice] 50#50: using the "epoll" event method
2024/01/19 17:34:39 [notice] 50#50: nginx/1.25.3
2024/01/19 17:34:39 [notice] 50#50: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/01/19 17:34:39 [notice] 50#50: OS: Linux 5.15.133.1-microsoft-standard-WSL2
2024/01/19 17:34:39 [notice] 50#50: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/01/19 17:34:39 [notice] 50#50: start worker processes
2024/01/19 17:34:39 [notice] 50#50: start worker process 77
...

这会在您的调试 shell 中启动 nginx,而无需实际运行容器。您可以按 `Ctrl`+`C` 关闭 nginx。

直接运行命令(例如,用于脚本编写)

使用 `--command` 选项直接评估命令,而不是启动交互式会话。例如,这类似于 `bash -c "arg1 arg2 ..."`。以下示例在 nginx 镜像中运行 `cat` 命令,而无需启动交互式会话。

$ docker debug --command "cat /usr/share/nginx/html/index.html" nginx

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="https://nginx.ac.cn/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

使用 --host 选项进行远程调试

以下示例演示如何使用 `--host` 选项。第一个示例使用 SSH 以 `root` 用户身份连接到 `example.org` 上的远程 Docker 实例,并进入 `my-container` 容器的 shell。

$ docker debug --host ssh://root@example.org my-container

以下示例连接到不同的本地 Docker Engine,并进入 `my-container` 容器的 shell。

$ docker debug --host=unix:///some/path/docker.sock my-container