docker 数据卷操作

1、创建数据卷

root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
[root@warship ~]# docker run -itd -v /etc/nginx --name nginx0 nginx:latest
59d0cdd4196be35b7dd0050722f7862cf2d7b3b385130ee4b2e4b93f8b6097c4
[root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
[root@warship ~]# docker run -itd -v etc_nginx:/etc/nginx --name nginx1 nginx:latest
5523b5062bd4b961c78ba52294c3d8566e1538453daba86e01414c4861491c9c
[root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
local               etc_nginx
[root@warship ~]# docker run -itd --mount source=etc-nginx,target=/etc/nginx --name nginx3 nginx:latest
025b88cd6ffca9a544142fab3340c5dd072265da0a0c32e91362d4080ba9a561
[root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
local               etc-nginx
local               etc_nginx

2、挂载本地目录
必须写绝对路径

[root@warship ~]# docker run -itd  -v ./nginx:/etc/nginx  --name nginx4 nginx:latest
docker: Error response from daemon: create ./nginx: "./nginx" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
[root@warship ~]# docker run -itd  -v /root/data:/data  --name nginx4 nginx:latest
f7b03266c886098459adbbe9b2af2c5332527e38c4984bdc9d6f3cf293cb026f
[root@warship ~]# docker run -itd --name nginx5 --mount type=bind,source=/root/data/,target=/data,consistency=consistent nginx:latest
c9a8de286db5b6a68a1babb46f66788f41c6c0c948be1ff380337702a618adeb

3、挂载本地文件

 [root@warship ~]#docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash

4、用convoy引擎创建数据卷
首先要安装convoy驱动并启动

[root@warship ~]# docker run -d -P \
>   --volume-driver=convoy \
>   -v my-named-volume:/webapp \
>   --name web training/webapp python app.py
bdaac95e9bf7d7cdfd18eeb100d32c40b38fa3e82c4d471c7ec2d773d3841e6c

[root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
local               etc-nginx
local               etc_nginx
convoy              my-named-volume
[root@warship ~]# docker volume create  -d convoy --opt o=size=20GB my-named-volume
my-named-volume
[root@warship ~]# docker volume ls
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
local               etc-nginx
local               etc_nginx
convoy              my-named-volume
[root@warship ~]# docker run -d -P \
>   -v my-named-volume:/webapp \
>   --name web training/webapp python app.py
e762537888a5b17da8f9062d34ce7037a34c1f70d661d855199deb959246f9ed

5、容器间挂载数据卷

[root@warship ~]# docker create -v public_dbdata:/dbdata --name dbstore training/postgres /bin/true
d7d7571bdb536004d56b71693f57c2f2e2802daa3ee41409c06ef2ed7de2c669
[root@warship ~]# docker volume ls 
DRIVER              VOLUME NAME
local               9e8db252080defa18ff3a9fb5f061f3503d90f17d74dab97e7252e3d01e23644
local               e91765a9d15a7bfd3717316f80458fba5a07ebeae58901bea05c5a1ce269779a
local               etc-nginx
local               etc_nginx
convoy              my-named-volume
local               public_dbdata
[root@warship ~]# docker run -d --volumes-from dbstore --name db1 training/postgres
3c7bea3615e01c04a3c8f7ba936282c867a529d5a547ed90adec9d0eae82b024

你可能感兴趣的:(Docker)