如何从主机编辑Docker容器文件? [英] How to edit Docker container files from the host?

查看:265
本文介绍了如何从主机编辑Docker容器文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我发现了一种将主机文件公开到容器(-v选项)的方法,我想做的是相反的:

Now that I found a way to expose host files to the container (-v option) I would like to do kind of the opposite:

我如何编辑来自运行容器的主机编辑器的文件?

How can I edit files from a running container with a host editor?

sshfs可能会做这个工作,但由于运行容器已经是某种主机目录,我想知道是否有便携式(在aufs,btrfs和设备映射器之间)的方法?

sshfs could probably do the job but since a running container is already some kind of host directory I wonder if there is a portable (between aufs, btrfs and device mapper) way to do that?

推荐答案

虽然可能,其他答案解释如何你应该避免在联盟文件系统中编辑文件,如果可以的话。

Whilst it is possible, and the other answers explain how, you should avoid editing files in the Union File System if you can.

你对卷的定义是不正确的 - 它更多的是绕过联盟文件系统而不是暴露主机上的文件。例如,如果我这样做:

Your definition of volumes isn't quite right - it's more about bypassing the Union File System than exposing files on the host. For example, if I do:

$ docker run --name="test" -v /volume-test debian echo "test"

目录 / volume-test 在容器内部不会是联盟文件系统的一部分,而是存在于主机上的某个地方。我没有指定主机上的哪里,因为我可能不在乎 - 我不会暴露主机文件,只需创建一个在容器和主机之间可共享的目录。您可以在主机上找到它的完整位置:

The directory /volume-test inside the container will not be part of the Union File System and instead will exist somewhere on the host. I haven't specified where on the host, as I may not care - I'm not exposing host files, just creating a directory that is shareable between containers and the host. You can find out exactly where it is on the host with:

$ docker inspect -f "{{.Volumes}}" test
map[/volume_test:/var/lib/docker/vfs/dir/b7fff1922e25f0df949e650dfa885dbc304d9d213f703250cf5857446d104895]

如果您真的需要对文件进行快速编辑以测试某些内容,请使用 docker exec 在容器中获取shell并直接进行编辑,或使用 docker cp 将文件复制出来,在主机上编辑并复制。

If you really need to just make a quick edit to a file to test something, either use docker exec to get a shell in the container and edit directly, or use docker cp to copy the file out, edit on the host and copy back in.

这篇关于如何从主机编辑Docker容器文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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