如果容器立即退出,探索 Docker 的图像文件? [英] Explore Docker's image files if container exits immediately?
问题描述
我有一个从 Docker 中心提取的 Docker 映像.
I have a Docker image that I pulled from Docker hub.
当我运行 docker run image_name
时,容器立即退出.
When I run docker run image_name
, container exits immediately.
我无权访问 Docker 映像的源代码,包括 Dockerfile.我只有一张从 hub.docker.com
中提取的图像.
I don't have access to the source code of the Docker image, including Dockerfile. All I have is an image I pulled from hub.docker.com
.
我需要调试/查看图像内部的内容(例如查看和探索图像的文件系统),而不是将其作为容器运行.
I need to debug/see what is inside the image (e.g. see and explore image's filesystem), without running it as a container.
有可能吗?
推荐答案
像这样从想要的镜像启动一个容器:
Start a container from desired image like this:
docker run -it --rm image_name bash
-i
即使没有附加 STDIN 也保持打开状态
-i
Keeps STDIN open even if not attached
-t
分配一个伪终端
--rm
退出后修剪停止的容器
--rm
prunes stopped container after exit
bash
执行容器中的特定命令.您可以执行任何有效的命令.
bash
executes the specific command in the container. You can execute any valid command.
示例 docker run -it --rm centos:7 pwd
输出 /
(根目录).
Example docker run -it --rm centos:7 pwd
outputs /
(root directory).
更新:在某些情况下,图像的入口点使用 bash/sh -c
格式上述命令(docker run -it --rm image_name bash
) 将不起作用,因为 bash
将被视为图像原始入口点的附加参数.
Update: In some cases, where image's entrypoint uses bash/sh -c
format above command(docker run -it --rm image_name bash
) will not work because bash
will be treated as additional argument to image's orignal entrypoint.
在这种情况下,您可以使用 --entrypoint
标志来获得相同的结果:
In such case you can use the --entrypoint
flag for achieving the same result:
docker run -it --entrypoint "/bin/bash" image_name
这篇关于如果容器立即退出,探索 Docker 的图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!