简体   繁体   中英

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

here is my docker compose file

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. 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 ?

  2. Or need to copy all file into image and run it?

  3. Please share some documentation of docker volume with swarm.

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.

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 ).

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

  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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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