调试没有 shell 的容器
Debugging containers that have no shell

原始链接: https://docs.docker.com/reference/cli/docker/debug

## Docker 调试:总结 Docker 调试是一个 CLI 命令(适用于 Docker Desktop 4.49+),专为调试容器和镜像而设计,特别是缺乏标准调试工具(如 shell)的“精简”镜像。它提供了一个调试 shell,*无需* 修改原始镜像或容器——更改隔离在一个可定制的工具箱中。 主要功能包括: * **访问任何容器/镜像:** 即使镜像中没有 shell 也能工作。 * **可定制的工具箱:** 预装了 `vim`、`nano`、`htop` 和 `curl` 等工具,并且可以通过 `install`(使用来自 [https://search.nixos.org/packages](https://search.nixos.org/packages) 的软件包)添加更多工具。 * **非破坏性:** 更改在退出时丢弃(对于镜像/已停止的容器),或直接对容器可见(对于正在运行/暂停的容器),绝不会影响基础镜像。 * **内置工具:** 包括 `entrypoint` 用于检查容器启动命令,以及 `--command` 选项用于直接运行脚本。 * **远程调试:** 支持通过 SSH 或替代套接字路径连接到远程 Docker 实例。 Docker 调试通过提供一个灵活安全的检查和修改环境,简化了调试工作流程,尤其适用于最小镜像。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 调试没有 shell 的容器 (docker.com) 6 分,来自 pploug 1 小时前 | 隐藏 | 过去 | 收藏 | 2 评论 pella 10 分钟前 | 下一个 [–] 仅适用于“Docker Desktop 4.49 及更高版本”回复 pploug 1 小时前 | 上一个 [–] Docker Debug 之前是一个付费功能,现在自 v4.49 起免费提供回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
DescriptionGet a shell into any container or image. An alternative to debugging with `docker exec`.
Usagedebug [OPTIONS] {CONTAINER|IMAGE}
Requires: Docker Desktop 4.49 and later. For Docker Desktop versions 4.48.0 and earlier, you must have a Pro, Team, or Business subscription

Docker Debug is a CLI command that helps you follow best practices by keeping your images small and secure. With Docker Debug, you can debug your images while they contain the bare minimum to run your application. It does this by letting you create and work with slim images or containers that are often difficult to debug because all tools have been removed. For example, while typical debug approaches like docker exec -it my-app bash may not work on a slim container, docker debug will work.

With docker debug you can get a debug shell into any container or image, even if they don't contain a shell. You don't need to modify the image to use Docker Debug. However, using Docker Debug still won't modify your image. Docker Debug brings its own toolbox that you can easily customize. The toolbox comes with many standard Linux tools pre-installed, such as vim, nano, htop, and curl. Use the builtin install command to add additional tools available on https://search.nixos.org/packages. Docker Debug supports bash, fish, and zsh. By default it tries to auto-detect your shell.

Custom builtin tools:

  • install [tool1] [tool2]: Add Nix packages from: https://search.nixos.org/packages, see example.
  • uninstall [tool1] [tool2]: Uninstall Nix packages.
  • entrypoint: Print, lint, or run the entrypoint, see example.
  • builtins: Show custom builtin tools.

For images and stopped containers, all changes are discarded when leaving the shell. At no point, do changes affect the actual image or container. When accessing running or paused containers, all filesystem changes are directly visible to the container. The /nix directory is never visible to the actual image or container.

OptionDefaultDescription
--shellautoSelect a shell. Supported: bash, fish, zsh, auto.
-c, --commandEvaluate the specified commands instead of starting an interactive session, see example.
--hostDaemon docker socket to connect to. E.g.: ssh://[email protected], unix:///some/path/docker.sock, see example.

Debugging containers that have no shell (slim containers)

The hello-world image is very simple and only contains the /hello binary. It's a good example of a slim image. There are no other tools and no shell.

Run a container from the hello-world image:

The container exits immediately. To get a debug shell inside, run:

The debug shell allows you to inspect the filesystem:

The file /hello is the binary that was executed when running the container. You can confirm this by running it directly:

After running the binary, it produces the same output.

Debugging (slim) images

You can debug images directly by running:

You don't even need to pull the image as docker debug will do this automatically like the docker run command.

Modifying files of a running container

Docker debug lets you modify files in any running container. The toolbox comes with vim and nano pre-installed.

Run an nginx container and change the default index.html:

To confirm nginx is running, open a browser and navigate to http://localhost:8080. You should see the default nginx page. Now, change it using vim:

Change the title to "Welcome to my app!" and save the file. Now, reload the page in the browser and you should see the updated page.

Managing your toolbox using the install command

The builtin install command lets you add any tool from https://search.nixos.org/packages to the toolbox. Keep in mind adding a tool never modifies the actual image or container. Tools get added to only your toolbox. Run docker debug and then install nmap:

You can confirm nmap is now part of your toolbox by getting a debug shell into a different image:

nmap is still there.

Understanding the default startup command of a container (entry points)

Docker Debug comes with a builtin tool, entrypoint. Enter the hello-world image and confirm the entrypoint is /hello:

The entrypoint command evaluates the ENTRYPOINT and CMD statement of the underlying image and lets you print, lint, or run the resulting entrypoint. However, it can be difficult to understand all the corner cases from Understand how CMD and ENTRYPOINT interact. In these situations, entrypoint can help.

Use entrypoint to investigate what actually happens when you run a container from the Nginx image:

The output tells you that on startup of the nginx image, a script /docker-entrypoint.sh is executed with the arguments nginx -g daemon off;. You can test the entrypoint by using the --run option:

This starts nginx in your debug shell without having to actually run a container. You can shutdown nginx by pressing Ctrl+C.

Running commands directly (e.g., for scripting)

Use the --command option to evaluate a command directly instead of starting an interactive session. For example, this is similar to bash -c "arg1 arg2 ...". The following example runs the cat command in the nginx image without starting an interactive session.

Remote debugging using the --host option

The following examples shows how to use the --host option. The first example uses SSH to connect to a remote Docker instance at example.org as the root user, and get a shell into the my-container container.

The following example connects to a different local Docker Engine, and gets a shell into the my-container container.

联系我们 contact @ memedata.com