是否可以在未运行的容器中获得 bash 访问权限?

Ven*_* Ch 7 bash containers docker

我能够成功运行 Flask API 容器。但在应用程序执行期间,它会失败并因某种原因停止容器。

我检查了容器日志,发现出现了一些文件丢失错误。现在我想通过访问/bin/bash已停止的容器来调试丢失的文件。但它会抛出一个错误,指出容器未运行。

docker exec -it 容器 /bin/bash

是否有任何解决方法可以访问bashSTOPPED 容器?

Kri*_*sia 7

您可以尝试使用该docker commit命令。

\n

来自文档

\n
\n

将容器\xe2\x80\x99s 文件更改或设置提交到新映像中可能很有用。这允许您通过运行交互式 shell 来调试容器,\n或将工作数据集导出到另一台服务器。

\n
\n

资源示例:

\n
\n

我们可以使用commit\n命令将容器转换为Docker镜像。我们需要知道的只是已停止容器的名称或标识符。\n (您可以使用 docker ps -a 获取所有已停止容器的列表)。

\n
\n
docker ps -a\nCONTAINER ID   IMAGE   COMMAND     CREATED         STATUS                    NAMES\n0dfd54557799   ubuntu  "/bin/bash" 25 seconds ago  Exited (1) 4 seconds ago  peaceful_feynman\n
Run Code Online (Sandbox Code Playgroud)\n
\n

有了已停止容器的标识符0dfd54557799,我们就可以创建一个新的 Docker 映像。生成的映像将具有与之前停止的容器相同的\n状态。此时,我们使用 docker run 并覆盖原始入口点以进入容器。

\n
\n
# Commit the stopped image\ndocker commit 0dfd54557799 debug/ubuntu\n\n# now we have a new image\ndocker images list\nREPOSITORY    TAG     IMAGE ID       CREATED         SIZE  \ndebug/ubuntu  <none>  cc9db32dcc2d   2 seconds ago   64.3MB\n\n\n# create a new container from the "broken" image\ndocker run -it --rm --entrypoint sh debug/ubuntu\n# inside of the container we can inspect - for example, the file system\n$ ls /app\nApp.dll\nApp.pdb\nApp.deps.json\n# CTRL+D to exit the container\n\n# delete the container and the image\ndocker image rm debug/ubuntu\n
Run Code Online (Sandbox Code Playgroud)\n

  • `docker commit` 似乎很有趣。我将尝试这个解决方法。谢谢! (2认同)

小智 0

你不能,因为这个容器已经死了,并且被关闭的虚拟机也死了。您可以使用docker logs命令检查日志。

docker container ls -aq

docker logs <name_of_your_dead_container>
Run Code Online (Sandbox Code Playgroud)