项目作者: volmex

项目描述 :
Volmex is a docker volume driver that allows to execute arbitrary commands before a volume is mounted (pre-mount hook).
高级语言: Go
项目地址: git://github.com/volmex/volmex.git
创建时间: 2017-07-05T08:21:11Z
项目社区:https://github.com/volmex/volmex

开源协议:The Unlicense

下载


volmex

Volmex is a docker volume driver that allows to execute arbitrary commands before a volume is mounted (pre-mount hook).

Context

Using a swarm cluster, we found that there is no satisfying solution to setup and use multi-host persistent volumes.

As there are numerous protocols and implementations for multi-host storage solutions, e.g. rsync, syncthing, … we decided to not implement a new protocol but strive for a more abstract solution.

Hence, we created a Docker volume plugin (v1) that simply executes a user defined command whenever a volume is mounted.

General

Besides the ‘pre-mount hook’, volmex volumes are equivalent to named volumes in Docker (With a different storage base directory though - /var/local/volmex)

Usage (Evaluation/Testing)

  1. # get volmex
  2. $ wget https://github.com/volmex/volmex/releases/download/v0.9/volmex-0.9.tar
  3. $ tar xf volmex-0.9.1.tar
  4. # create dir where local volumes are stored
  5. $ sudo mkdir /var/local/volmex
  6. # start volmex driver (later you can move this to a systemd service file)
  7. $ sudo ./volmex-daemon
  8. # create a volume 'foo' using the volmex driver and specify a command or shell script that shall be executed pre-mount
  9. $ docker volume create \
  10. --driver volmex \
  11. --opt cmd="/usr/local/sbin/do-something" \
  12. foo

When the command is executed, the following variables are available in the command’s environment:

  • VOLMEX_NAME = foo
  • VOLMEX_MOUNTPOINT = /var/local/volmex/foo
  • VOLMEX_CMD = /usr/local/sbin/do-something

Install on systemd systems (Persistent)

  • Download and extract volmex
  1. # cd /tmp
  2. # wget https://github.com/volmex/volmex/releases/download/v0.9/volmex-0.9.tar
  3. # tar xf volmex-0.9.1.tar
  • Install volmex
  1. # mkdir -p /usr/lib/docker
  2. # install -D -m 744 volmex-daemon /usr/lib/docker/volmex-daemon
  3. # install -D -m 644 volmex.service /etc/systemd/system/volmex.service
  4. # mkdir -p /var/local/volmex
  5. # systemctl daemon-reload
  • Start/enable volmex
  1. # systemctl enable volmex
  2. # systemctl restart docker
  • Volmex should be started before Docker - which is configured in the service file. That’s why it’s sufficient to systemctl restart docker.

Usage

  • Create script that shall be executed on volume mount:
  1. ~ sudo vim /usr/local/sbin/do-something
  2. ...
  3. #!/usr/bin/env bash
  4. echo $VOLMEX_NAME
  5. echo sleeping for 10 seconds...
  6. sleep 10
  7. echo done
  8. ...
  9. ~ sudo chmod +x /usr/local/sbin/do-something
  • Create the volume using the volmex driver
  1. ~ docker volume create --driver volmex --opt cmd="/usr/local/sbin/do-something" foo
  • Add content
  1. ~ sudo mkdir -p /var/local/volmex/foo
  2. ~ sudo vim /var/local/volmex/foo/volmex-test
  3. ...
  4. volmex-test
  5. ...
  • Create a container which uses the volume
  1. ~ docker run --rm -ti --volume foo:/foo alpine sh
  2. / # cat /foo/volmex-test
  3. volmex-test
  4. / # exit
  • Check log output of the executed command
  1. ~ journalctl -u volmex | tail
  2. ...
  3. volmex-daemon[2115]: received 'Get' request for volume: foo
  4. volmex-daemon[2115]: received 'Mount' request for volume: foo
  5. volmex-daemon[2115]: executing volmex command: /usr/local/sbin/do-something
  6. volmex-daemon[2115]: foo
  7. volmex-daemon[2115]: sleeping for 10 seconds...
  8. volmex-daemon[2115]: done
  9. volmex-daemon[2115]: received 'Unmount' request for volume: foo
  10. ...