Install & Use Docker Volume for Service, Swarm, & Stack in Linux CentOS 7

From Gejoreuy
Jump to navigation Jump to search

Sources

https://docs.docker.com/compose/compose-file/#volumes-for-services-swarms-and-stack-files
https://github.com/vieux/docker-volume-sshfs
https://github.com/vieux/docker-volume-sshfs/issues/48
https://portainer.readthedocs.io/en/latest/deployment.html
https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container?rq=1

Purpose

When working with services, swarms, and stack, keep in mind that the tasks (containers) backing a service can be deployed on any node in a swarm, and this may be a different node each time the service is updated.
In the absence of having named volumes with specified sources, Docker creates an anonymous volume for each task backing a service. Anonymous volumes do not persist after the associated containers are removed.
If we want our data to persist, use a named volume and a volume driver that is multi-host aware, so that the data is accessible from any node.

Preparation

Make sure we have installed docker and docker-compose in the server. Please refer how to install it in below links.

Make sure each node in swarm already connected with ssh key to the volume server. So we no need password to be configured in docker volume.

Do the update to the server.

[root@volume ~]# yum update

Install Plugin

Make sure you use root user in installing the plugin. If not, just change the ssh directory path.

[root@volume ~]# docker plugin install vieux/sshfs sshkey.source=/root/.ssh/

Create Docker Volume

[root@volume ~]# docker volume create --driver vieux/sshfs -o sshcmd=root@dh-storage:/root/storage sshvolume

Configure Docker Compose File

Below is the example how docker-compose.yml file configured with docker volume.

version: "3"
services:

  service1:
    image: image-example
    ports:
      - "80:8081"
    volumes:
      - "sshfsdata:/mount/it/here:ro"

volumes:
  sshfsdata:
    driver: vieux/sshfs:latest
    driver_opts:
      sshcmd: "username@server:/location/on/the/server"
      allow_other: ""