如何将主机数据挂载到不同的容器

How to mount host data to different containers

我需要将主机目录/data挂载到6个容器h1,h2,h3,h4,h5,h6

/data是主机挂载的外接硬盘。 6个容器可以轻松打开和关闭。

这6个容器会进入各自的/data子目录,独立分析数据,在本地产生新的数据。所有子目录之间没有任何关系

相关问题是 here,但没有给出首选答案。

怎么做?下面是我现在的容器和镜像。

$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9bd9334a1e7 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h6 23679fe7252b ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h5 e2864e38e746 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h4 c8996a304638 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h3 9acd2a223d86 ubuntu "/usr/bin/bash" 19 hours ago Up 18 hours h2 5690b8c7b6da ubuntu "/usr/bin/bash" 2 days ago Up 12 hours h1

$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest f2a91732366c 2 months ago 1.85 kB docker.io/ubuntu 27 422dc563ca32 2 months ago 252 MB docker.io/ubuntu latest 422dc563ca32 2 months ago 252 MB

如果这些容器 已经 运行,您不能轻易地向它们添加 /data。
除了 docker cp.

但最佳做法仍然是:

  • 使用已经在其中的 /data 制作图像 (Dockerfile ADD)
  • 或使用现有图像并启动您的容器,但使用 -v(卷)选项。请参阅 Use volumes