简体   繁体   English

如何将本地文件共享到docker容器中使用docker-compose进行swarm

[英]How to share local files to docker container In swarm with docker-compose

here is my docker compose file 这是我的docker撰写文件

version: '2'
services:
 demoui:
  image: demoimage
  ports:
   - "80:8000"
  volumes:
   - ./democonfig/config.js:/usr/local/tomcat/webapps/demo-ui/config.js
   - ./logs/demo-ui:/usr/local/tomcat/logs
  restart: unless-stopped

This docker compose file works when I was in single node. 当我在单个节点中时,此docker compose文件可以正常工作。 After moving to docker swarm . 移动到码头群后。 It is not working. 它不起作用。 It throws the following error 它会引发以下错误

ERROR: for demoui  Error response from daemon: rpc error: code = 2 desc = "oci runtime error: could not synchronise with container process: not a directory"
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "compose/cli/main.py", line 63, in main
AttributeError: 'ProjectError' object has no attribute 'msg'
docker-compose returned -1

So the questions are 所以问题是

  1. How to share file to swarm cluster ? 如何将文件共享到swarm集群?

  2. Or need to copy all file into image and run it? 或者需要将所有文件复制到图像中并运行它?

  3. Please share some documentation of docker volume with swarm. 请与swarm分享一些docker volume的文档。

The error you're getting is because the source files/dirs for your volumes do not exist on the swarm nodes you are launching on. 您得到的错误是因为您正在启动的群集节点上不存在卷的源文件/目录。 Docker (and docker-compose ) has no way to copy those files over to the other hosts in the swarm. Docker(和docker-compose )无法将这些文件复制到swarm中的其他主机。

The source files/dirs need to be present on all of the swarm nodes that you want to share the configuration files with. 源文件/目录需要存在于您要与之共享配置文件的所有群集节点上。 You're also using a context-dependent path in your compose file, which isn't going to be consistent across all the nodes. 您还在撰写文件中使用了与上下文相关的路径,该路径在所有节点中都不一致。 This should instead be an absolute path (ie /opt/config rather than ./config or ~/config ). 这应该是一个绝对路径(即/opt/config而不是./config~/config )。

As a quick fix, you will need to copy the config files to each node in the same location (ie /opt or some other directory) and modify your compose file to point to this consistent location, eg 作为快速修复,您需要将配置文件复制到同一位置(即/ opt或其他目录)中的每个节点,并修改您的撰写文件以指向此一致位置,例如

  volumes:
   - /opt/config/config.js:/usr/local/tomcat/webapps/demo-ui/config.js
   - /opt/logs/demo-ui:/usr/local/tomcat/logs

Long-term, you can either sync these files manually, use a tool like lsyncd to keep them in sync automatically across nodes, place them on an NFS volume and mount that on each node, or use something like glusterfs . 从长远来看,您可以手动同步这些文件,使用lsyncd之类的工具使它们在节点之间自动保持同步,将它们放在NFS卷上并将其挂载到每个节点上,或者使用像glusterfs这样的东西。

Just keep in mind for your logging dir, you will likely want to ensure your log files aren't clobbering each other so you may want to keep them local or ensure the log file names are unique to each node if using shared storage. 请记住您的日志记录目录,您可能希望确保您的日志文件不会互相破坏,因此您可能希望将它们保持在本地或确保日志文件名称对于每个节点都是唯一的(如果使用共享存储)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM