Docker: 在本地使用 swarm 绑定挂载

Docker: using a bind mount locally with swarm

Docker新人

我有一个 django 网站的简单图像,其中为应用程序目录定义了一个卷。 我可以将此卷绑定到我使用此命令进行开发的实际文件夹:

docker container run --rm -p 8000:8000 --mount type=bind,src=$(pwd)/wordcount-project,target=/usr/src/app/wordcount-project wordcount-django

这很有效。 现在我试着把那个简单的例子推到一个群里。请注意,我已经为可用的图像设置了一个本地注册表。 所以要开始我的服务,我会这样做:

docker service create -p 8000:8000 --mount type=bind,source=$(pwd)/wordcount-project,target=/usr/src/app/wordcount-project 127.0.0.1:5000/wordcount-django

它会在一些尝试后工作,但这只是因为它 运行 在本地节点(实际文件夹所在的位置)而不是远程节点(没有 wordcount-project 文件夹)。

知道如何解决这个问题,以便所有节点都可以访问该文件夹,并且仍然可以在本地访问以进行开发吗?

谢谢!

不推荐在 docker swarn 中使用 bind-mount,你可以阅读 in the doc。特别是:

Important: Bind mounts can be useful but they can also cause problems. In most cases, it is recommended that you architect your application such that mounting paths from the host is unnecessary.

但是,如果您仍然想使用绑定挂载,那么您有两种可能:

  1. 确保您的文件夹存在于所有节点上。这里的主要问题是您每次都必须在每个节点上更新它。
  2. 使用共享文件系统(例如sshfs)并将其挂载到每个节点的目录中。然而,既然你有一个共享文件系统,那么你可以只使用一个 docker 数据卷并更改驱动程序。

您可以找到一些关于更改卷数据驱动程序的文档here