有没有办法删除 docker 容器内的主机文件?

Is there a way to delete files of host inside a docker container?

我是 docker 卷的新手,我的用例是下一个:

我在同一个主机中有两个不同的容器 运行,并且都需要从中获取 read/write 文件。据我所知,我应该使用 docker 卷,但在我尝试之前,我想确保我可以从容器内部删除主机文件系统的文件(例如使用 golang 应用程序)

也许,您应该使用 docker 卷。它可以在主机和容器之间共享目录。比如你想read/write/mnt里的文件,你可以把/mnt挂载到容器里。

docker run -it -v /mnt:/mnt ubuntu:latest touch /mnt/hello.log

现在,/mnt/hello.log 已创建。您可以编辑文件 /mnt/hello。登录您的主机文件系统。

然后,

docker run -it -v /mnt:/mnt ubuntu:latest rm /mnt/hello.log

执行上述命令后,文件/mnt/hello.log将从容器中删除。

其实可以在golang中删除文件,像这样:

os.Remove("/mnt/hello.log")