Containers are ephemeral. Containers live entirely in memory, so even if the container is set up with automatic restart, the new container will not have access to the data created inside of the old container. To save persistent data of Docker containers we need to create volumes that live outside of the ephemeral containers. Don’t map volumes that store important information to the host. Use platform storage, like Amazon EBS, GCE Persistent Disk or Azure Disk Volume to store all your important data. Set up an automatic backup process to create volume snapshots regularly, so even if the host goes away, your data is safe.
Docker Swarm, use drivers to connect to external resources. One of the most versatile is REX-Ray.
We will add volume to MongoDB running in a Docker Swarm.
Install the REX-Ray plugin on the Docker host
docker plugin install rexray/ebs EBS_ACCESSKEY=aws_access_key EBS_SECRETKEY=aws_secret_key
Create a 1 GB Docker volume with Rex-Ray and gp2 EBS backing.
docker volume create –driver rexray/ebs –opt size=1 –opt volumetype=gp2 –name ebs_volume
Launch a new MongoDB service and map the volume to the MongoDB data directory
docker service create –network my_overlay_network –replicas 1 –mount type=volume,src=ebs_volume,target=/data/db –name mongodb mongo