如何将Docker容器中的目录挂载到主机? [英] How to mount a directory in a Docker container to the host?

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

问题描述

假设我有一个使用此简单Dockerfile的应用程序:

Assume that i have an application with this simple Dockerfile:

//...
RUN configure.sh --logmyfiles /var/lib/myapp
ENTRYPOINT ["starter.sh"]
CMD ["run"]
EXPOSE 8080
VOLUME ["/var/lib/myapp"]

然后我从中运行一个容器:

And I run a container from that:

sudo docker run -d --name myapp -p 8080:8080 myapp:latest

因此它可以正常工作并将一些日志存储在Docker容器的 / var / lib / myapp 中。

So it works properly and stores some logs in /var/lib/myapp of docker container.

我的问题

我也需要将这些日志文件也自动保存在主机中,因此如何安装 / var / lib / myapp 从容器到主机服务器中的 / var / lib / myapp (不删除当前容器) )?

I need these log files to automatically saved in host too, So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server (without removing current container) ?

编辑

我也看到了Docker-Mount Direc从容器到主机,但这并不能解决我的问题,我需要一种将文件从Docker备份到主机的方法。

I also see Docker - Mount Directory From Container to Host, but it doesn't solve my problem i need a way to backup my files from docker to host.

推荐答案

首先,有关 Docker卷的一些信息。卷挂载仅在容器创建时发生。这意味着启动容器后便无法更改卷安装。同样,卷安装是单向的:从主机到容器,而不是相反。当您在容器中指定作为卷装入的主机目录时(例如: docker run -d --name = foo -v / path / on / host:/ path / on / container ubuntu ),它是一个常规ole Linux mount --bind ,这意味着主机目录将暂时覆盖容器目录。实际上,目标目录上没有任何内容被删除或覆盖,但是由于容器的性质,这实际上意味着在容器的整个生命周期中都将覆盖它。

First, a little information about Docker volumes. Volume mounts occur only at container creation time. That means you cannot change volume mounts after you've started the container. Also, volume mounts are one-way only: From the host to the container, and not vice-versa. When you specify a host directory mounted as a volume in your container (for example something like: docker run -d --name="foo" -v "/path/on/host:/path/on/container" ubuntu), it is a "regular ole" linux mount --bind, which means that the host directory will temporarily "override" the container directory. Nothing is actually deleted or overwritten on the destination directory, but because of the nature of containers, that effectively means it will be overridden for the lifetime of the container.

您剩下两个选择(也许三个)。您可以将主机目录装载到容器中,然后在启动脚本中复制这些文件(或者如果您将cron放入您的容器中,您可以使用cron定期将这些文件复制到该主机目录卷装载中。)

So, you're left with two options (maybe three). You could mount a host directory into your container and then copy those files in your startup script (or if you bring cron into your container, you could use a cron to periodically copy those files to that host directory volume mount).

使用 docker cp 将文件从容器移动到主机。现在,这有点麻烦,而且绝对不是您应该在基础结构自动化中使用的东西。但是确实确实可以很好地达到该目的。一次性或调试是一个很好的选择。

You could also use docker cp to move files from your container to your host. Now that is kinda hacky and definitely not something you should use in your infrastructure automation. But it does work very well for that exact purpose. One-off or debugging is a great situation for that.

您也可以设置网络传输,但这确实涉及您的工作。但是,如果要定期对日志文件(或其他文件)执行此操作,则可以考虑使用 rsyslog 之类的工具将这些文件移出容器。

You could also possibly set up a network transfer, but that's pretty involved for what you're doing. However, if you want to do this regularly for your log files (or whatever), you could look into using something like rsyslog to move those files off your container.

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

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