(转)Docker volume plugin - enabled create local volume on docker host
原文地址:https://hub.docker.com/r/cwspear/docker-local-persist-volume-plugin/
Short Description
Full Description
Local Persist Volume Plugin for Docker
Create named local volumes that persist in the location(s) you want!
Rationale
In Docker 1.9, they added support for creating standalone named Volumes. Now with Docker 1.10 and Docker Compose 1.6's new syntax, you can create named volumes through Docker Compose.
This is great for creating standalone volumes and easily connecting them to different directories in different containers as a way to share data between multiple containers. On a much larger scale, it also allows for the use of Docker Volume Plugins to do cool things like Flocker is doing (help run stateful containers across multiple hosts).
Even if something like Flocker is overkill for your needs, it can still be useful to have persistent data on your host. I'm a strong advocate for "Docker for small projects" and not just huge, scaling behemoths and microservices. I wrote this out of a need on projects I'm currently working on and have in production.
This local-persist
approach gives you the same benefits of standalone Volumes that docker volume create ...
normally affords, while also allowing you to create Volumes that persist, thus giving those stateful containers their state. Read below how to install and use, then read more about the benefits of this approach.
Installing & Running
To create a Docker Plugin, one must create a Unix socket that Docker will look for when you use the plugin and then it listens for commands from Docker and runs the corresponding code when necessary.
Running the code in this project with create the said socket, listening for commands from Docker to create the necessary Volumes.
According to the Docker Plugin API Docs:
Plugins can run inside or outside containers. Currently running them outside containers is recommended.
It doesn't really say why one way is recommended over the other, but I provide binaries and instructions to run outside of container, as well as an image and instructions to run it inside a container.
Running Outside a Container
Quick Way
I provide an install
script that will download the proper binary, set up an Systemd service to start when Docker does and enable it.
curl -fsSL https://raw.githubusercontent.com/CWSpear/local-persist/master/scripts/install.sh | sudo bash
This needs be to run on the Docker host. i.e. running that on a Mac won't work (and it will print a message saying as much and exit).
This has been tested on Ubuntu 15.10, and is known not to work on CoreOS (yet). If you need to use Upstart instead of Systemd, you can pass the --upstart
flag to the install script, but it isn't as tested, so it may not work:
curl -fsSL https://raw.githubusercontent.com/CWSpear/local-persist/master/scripts/install.sh | sudo bash -s -- --upstart
Follow the same process to update to the latest version.
Manual Way
If you're uncomfortable running a script you downloaded off the internet with sudo
, you can extract any of the steps out of the install.sh
script and run them manually. However you want to do it, the main steps are:
- Download the appropriate binary from the Releases page for your OS and architecture.
- Rename the downloaded file
docker-volume-local-persist
- Place it in
/usr/bin
(you can put it somewhere else, but be sure your Systemd (or similar) config reflects the change). - Make sure the file is executable (
chmod +x /usr/bin/docker-volume-local-persist
) - It's enough to just run it at this point (type
docker-volume-local-persist
and hit enter) to test, etc, and if that's all you're trying to do, you're done. But if you want it to start with Docker, proceed to step 6. - Download systemd.service
- Rename the service file to
docker-volume-local-persist.service
- Move it to
/etc/systemd/system/
- run
sudo systemctl daemon-reload
to reload the config - run
sudo systemctl enable docker-volume-local-persist
to enable the service (it will start after Docker does) - run
sudo systemctl start docker-volume-local-persist
to start it now. Safe to run if it's already started
Running from Within a Container
I maintain an image on Docker Hub to run this plugin from a container:
docker run -d \
-v /run/docker/plugins/:/run/docker/plugins/ \
-v /path/to/store/json/for/restart/:/var/lib/docker/plugin-data/ \
-v /path/to/where/you/want/data/volume/:/path/to/where/you/want/data/volume/ \
cwspear/docker-local-persist-volume-plugin
The -v /run/docker/plugins/:/run/docker/plugins/
part will make sure the sock
file gets created at the right place. You also need to add one or more volumes to places you want to mount your volumes later at.
For example, if I am going to persist my MySQL data for a container I'm going to build later at /data/mysql/
, I would add a -v /data/mysql/:/data/mysql/
to the command above (or even -v /data/:/data/
). You can add more than one location in this manner.
Lastly, the -v /path/to/store/json/for/restart/:/var/lib/docker/plugin-data/
part is so that the plugin can create a json
file to know what volumes existed in case of a system restart, etc.
When the container is destroyed, etc, it will look at a file it created in /var/lib/docker/plugin-data/
to recreate any volumes that had previously existed, so you want that JSON file to persist on the host.
Usage: Creating Volumes
Then to use, you can create a volume with this plugin (this example will be for a shared folder for images):
docker volume create -d local-persist -o mountpoint=/data/images --name=images
Then if you create a container, you can connect it to this Volume:
docker run -d -v images:/path/to/images/on/one/ one
docker run -d -v images:/path/to/images/on/two/ two
# etc
Also, see docker-compose.example.yml for an example to do something like this with Docker Compose (needs Compose 1.6+ which needs Engine 1.10+).
Benefits
This has a few advantages over the (default) local
driver that comes with Docker, because our data will not be deleted when the Volume is removed. The local
driver deletes all data when it's removed. With the local-persist
driver, if you remove the driver, and then recreate it later with the same command above, any volume that was added to that volume will still be there.
You may have noticed that you could do this with data-only containers, too. And that's true, and using that technique has a few advantages, one thing it (specifically as a limitation of volumes-from
) does not allow, is mounting that shared volume to a different path inside your containers. Trying to recreate the above example, each container would have to store images in the same directory in their containers, instead of separate ones which local-persist
allows.
Also, using local-persist
instead of data-only containers, docker ps -a
won't have extra dead entries, and docker volume ls
will have more descriptive output (because volumes have names).
(转)Docker volume plugin - enabled create local volume on docker host的更多相关文章
- [Docker] Create a Volume
We can create volumn to keep the data, even we stop the container and restart again, the data won't ...
- centos7下安装docker(13.1docker存储--data volume)
我们现在知道docker 有两种存储方式:storage driver和data volume stroage driver这种存储方式主要是存储那些无状态的数据,是镜像层和容器层组成的,而data ...
- kubernetes支持local volume
目录 local volume 创建一个storage class 静态创建PV 使用local volume PV 动态创建PV local volume kubernetes从1.10版本开始支持 ...
- k8s local volume 和host path volume的区别
k8s提供多种volume接口,其中local 和host path是容易混淆的两个接口.下面这篇文章解释了两者的区别: https://groups.google.com/forum/#!topic ...
- Docker(三)-Docker中Image、Container与Volume的迁移
Image 镜像的迁移,适用于离线环境. 一般离线环境,都会自建Docker Registry. 无论 官方的 ,还是最近流行的 Harbor ,都是不错的选择. 但是,这个世界上就是有些环境,或者说 ...
- 详解Docker中Image、Container与 Volume 的迁移
开源Linux 长按二维码加关注~ 上一篇:Linux Used内存到底哪里去了? 已经部署的容器化服务,也不是不需要维护的.而且,由于生产环境往往有这样那样的严格要求,往往需要些非常规操作.Imag ...
- Docker源码分析(四):Docker Daemon之NewDaemon实现
1. 前言 Docker的生态系统日趋完善,开发者群体也在日趋庞大,这让业界对Docker持续抱有极其乐观的态度.如今,对于广大开发者而言,使用Docker这项技术已然不是门槛,享受Docker带来的 ...
- [置顶]
Docker学习总结(7)——云端基于Docker的微服务与持续交付实践
本文根据[2016 全球运维大会•深圳站]现场演讲嘉宾分享内容整理而成 讲师简介 易立 毕业于北京大学,获得学士学位和硕士学位:目前负责阿里云容器技术相关的产品的研发工作. 加入阿里之前,曾在IBM中 ...
- Docker进阶:容器卷、DockerFile、Docker网络原理
1.Docker镜像 1.1镜像是什么 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和机遇运行环境开发的软件. 包含一个软件的所有内容.蒋所有的应用和环境,直接打包为docker镜像,直 ...
随机推荐
- 201621123006 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 多态.重载.继承.覆盖.super.抽象类 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. ...
- IGMP 因特网组管理协议
IGMP(Internet Group Management Protocol)作为因特网组管理协议,用于多播. 与ICMP一样, IGMP也被当作 IP 层的一部分,IGMP报文也通过IP数 ...
- Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解决)
在一个Spring项目中,新建了一个外部属性文件db.properties,在xml文件中利用${}来引用db.properties文件里面的属性. beans-properties.xml: < ...
- (转)Mahout Kmeans Clustering 学习
一.Mahout命令使用 合成控制的数据集 synthetic_control.data 可以从 此处下载,总共由600行X60列double型的数据组成, 意思是有600个元组,每个元组是一个时间序 ...
- CentOS常用命令汇总
将新创建的数据分配某个用户访问 grant all privileges on zhouzdb.* to 'zhouz'@'%' identified by '1234'; flush privile ...
- I.MX6 U-Boot mkconfig hacking
/**************************************************************************** * I.MX6 U-Boot mkconfi ...
- 【剑指offer】10矩阵覆盖
原创博文,转载请注明出处! 0.简介 # 本文是牛客网<剑指offer>刷题笔记,笔记索引链接 1.题目 # 用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地 ...
- SEO中H1标签的用法- 1
在网上找了很多关于H1标签对SEO意义的资料,不可否认H1对SEO具有重大的意义,但是具体情况每个人有每个人的见解吧.这里主要根据网上搜索的资料,以及自己的一些经验整理出来的,但是本人毕竟不是专业SE ...
- iOS8扩展插件开发配置 [转载]
一.iOS8扩展插件概述 WWDC14除了发布了OS X v10.10和switf外,iOS8.0也开始变得更加开放了.说到开放,当然要数应用扩展(App Extension)了.顾名思义,应用扩展允 ...
- 线性回归 Linear regression(2)线性回归梯度下降中学习率的讨论
这篇博客针对的AndrewNg在公开课中未讲到的,线性回归梯度下降的学习率进行讨论,并且结合例子讨论梯度下降初值的问题. 线性回归梯度下降中的学习率 上一篇博客中我们推导了线性回归,并且用梯度下降来求 ...