探索 Docker 容器的文件系统 [英] Exploring Docker container's file system

查看:36
本文介绍了探索 Docker 容器的文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到使用 docker 我需要了解容器内发生的事情或其中存在哪些文件.一个例子是从 docker 索引下载图像 - 您不知道图像包含什么,因此无法启动应用程序.

I've noticed with docker that I need to understand what's happening inside a container or what files exist in there. One example is downloading images from the docker index - you don't have a clue what the image contains so it's impossible to start the application.

最理想的情况是能够通过 ssh 进入它们或类似的东西.是否有工具可以做到这一点,或者我认为我应该能够做到这一点对 docker 的概念化是错误的.

What would be ideal is to be able to ssh into them or equivalent. Is there a tool to do this, or is my conceptualisation of docker wrong in thinking I should be able to do this.

推荐答案

这里有几个不同的方法...

Here are a couple different methods...

Docker 1.3 或更新版本支持命令 exec,其行为类似于 nsenter.此命令可以在已经运行的容器中运行新进程(容器必须已经运行了 PID 1 进程).您可以运行 /bin/bash 来探索容器状态:

Docker version 1.3 or newer supports the command exec that behave similar to nsenter. This command can run new process in already running container (container must have PID 1 process running already). You can run /bin/bash to explore container state:

docker exec -t -i mycontainer /bin/bash

查看 Docker 命令行文档

您可以通过这种方式评估容器文件系统:

You can evaluate container filesystem this way:

# find ID of your running container:
docker ps

# create image (snapshot) from container filesystem
docker commit 12345678904b5 mysnapshot

# explore this filesystem using bash (for example)
docker run -t -i mysnapshot /bin/bash

通过这种方式,您可以在精确的时刻评估正在运行的容器的文件系统.容器仍在运行,不包含未来的更改.

This way, you can evaluate filesystem of the running container in the precise time moment. Container is still running, no future changes are included.

您可以稍后使用(正在运行的容器的文件系统不受影响!)删除快照:

You can later delete snapshot using (filesystem of the running container is not affected!):

docker rmi mysnapshot

C) 使用 ssh

如果您需要持续访问,您可以将 sshd 安装到您的容器并运行 sshd 守护进程:

C) Use ssh

If you need continuous access, you can install sshd to your container and run the sshd daemon:

 docker run -d -p 22 mysnapshot /usr/sbin/sshd -D
 
 # you need to find out which port to connect:
 docker ps

这样,您就可以使用 ssh 运行您的应用程序(连接并执行您想要的操作).

This way, you can run your app using ssh (connect and execute what you want).

使用nsenter,见为什么不需要在 Docker 容器中运行 SSHd

简短的版本是:使用 nsenter,您可以将一个 shell 放入一个现有容器,即使该容器不运行 SSH 或任何类型特殊用途的守护进程

这篇关于探索 Docker 容器的文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆