容器数据持久化
1、数据卷:将本地磁盘mount进入容器
-v /dest
-v src:dst [ro]|[rw]
2、数据卷容器:将外部容器分享给容器
--volumes-from
数据卷容器专门用来挂载数据卷的容器,以供其他容器引用和使用
3、数据卷管理命令
docker volume
一、数据卷:
1、给容器内部映射一个外部数据卷 -v src:dest
[root@docker1 nginx]# docker run -d --name web1 -v /opt/nginx:/usr/share/nginx/html -p 808:80 nginx:1.8
a288f924bb7d4d64b7e64794cbdc14592a1fc9c0b743404f16a880b0850fff0c
[root@docker1 nginx]# docker exec -it web1 bash
root@a288f924bb7d:/# ls /usr/share/nginx/html/
index.html
2、如果不指定本地数据卷位置,默认存储在/var/lib/docker/volumes下
[root@docker1 nginx]# docker run -d --name web2 -p 809:80 -v /usr/share/nginx/html nginx:1.8
978187c40573fb15ac547da84596847a71c48639ba21fa556b6d12149ca8265d
[root@docker1 nginx]#
[root@docker1 nginx]# cd /var/lib/docker/volumes/
[root@docker1 volumes]# ls e96f7bc2afc0b46d99fe59665d1f0b7ed9375c9492d115660ada00d82b68b649/_data/
client_temp fastcgi_temp proxy_temp scgi_temp uwsgi_temp
[root@docker1 volumes]# docker inspect web2
{
"Type": "volume",
"Name": "e96f7bc2afc0b46d99fe59665d1f0b7ed9375c9492d115660ada00d82b68b649",
"Source": "/var/lib/docker/volumes/e96f7bc2afc0b46d99fe59665d1f0b7ed9375c9492d115660ada00d82b68b649/_data",
"Destination": "/var/cache/nginx",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
将本地磁盘映射docker中
[root@docker ~]# mkdir -p /data/docker-volumes
[root@docker data]# docker run -d --name nginx-volume-test4 -v /data/docker-volumes/:/data nginx
eb3e5a6677a26e588f6bf48cdfc7394ffd31c93bf28f0d0c62f9c1dc711ec858
[root@docker data]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb3e5a6677a2 nginx "nginx -g 'daemon off" 18 seconds ago Up 17 seconds 80/tcp, 443/tcp nginx-volume-test4
[root@docker data]# docker inspect -f "{{ .State.Pid }}" nginx-volume-test4
11232
[root@docker data]# nsenter -t 11232 -m -u -i -n -p
root@eb3e5a6677a2:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@eb3e5a6677a2:/# ls data
file1 web6
root@eb3e5a6677a2:/#
二、数据卷容器
[root@docker1 ~]# mkdir /opt/dbdata
[root@docker1 ~]# docker create -v /opt/dbdata:/dbdata --name dbstore centos
4d68156146da097910f1424ea41d042929aba622e83eb736b613c184bcb4ad5d
[root@docker1 ~]# docker run -it --rm --volumes-from dbstore centos
[root@618f59f9d7e8 /]# df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 46G 3.6G 42G 8% /
overlay 46G 3.6G 42G 8% /
tmpfs 926M 0 926M 0% /dev
tmpfs 926M 0 926M 0% /sys/fs/cgroup
/dev/mapper/centos-root 46G 3.6G 42G 8% /dbdata
[root@618f59f9d7e8 /]# cd dbdata/
[root@618f59f9d7e8 dbdata]# ls
test
注意:但是容器内不能写文件,没有权限
三、docker volume
[root@docker1 ~]# docker volume
Usage: docker volume COMMAND
Manage volumes
Options:
--help Print usage
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
[root@docker1 ~]# docker volume ls
DRIVER VOLUME NAME
local 2a3f571ee6ae8b2ed9890a97faeb1e30d7ab1b0ad973bb0c6f4f4b0e3f544036
local 95b0346142cb123d97f49614bd21492b558660ea51e96672d7d3c52b6ec32f5f
local 980041d114c6c813b1855c9e809a7347951426e805fe7d79608132603c4f6563
local e07d69c288b08e60fcdfbd35a5c86a4c251de74659cd24748a4eaeb1291e710d
local e3a38c545ea23f64abddc3e901313e32a1d045acf433a06a98a16e60299b3169
local e96f7bc2afc0b46d99fe59665d1f0b7ed9375c9492d115660ada00d82b68b649
local e9d6633614ad9fef67881ab094a54f0258c40273ce0f694b6e6985ec312f3424
[root@docker1 ~]#
删除没有挂载的数据卷
[root@docker1 ~]# docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
e07d69c288b08e60fcdfbd35a5c86a4c251de74659cd24748a4eaeb1291e710d
980041d114c6c813b1855c9e809a7347951426e805fe7d79608132603c4f6563
e3a38c545ea23f64abddc3e901313e32a1d045acf433a06a98a16e60299b3169
Total reclaimed space: 0 B
[root@docker1 ~]# docker volume ls
DRIVER VOLUME NAME
local 2a3f571ee6ae8b2ed9890a97faeb1e30d7ab1b0ad973bb0c6f4f4b0e3f544036
local 95b0346142cb123d97f49614bd21492b558660ea51e96672d7d3c52b6ec32f5f
local e96f7bc2afc0b46d99fe59665d1f0b7ed9375c9492d115660ada00d82b68b649
local e9d6633614ad9fef67881ab094a54f0258c40273ce0f694b6e6985ec312f3424
查看一个数据卷的详细信息
[root@docker1 ~]# docker inspect e9d6633614ad9fef67881ab094a54f0258c40273ce0f694b6e6985ec312f3424
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/e9d6633614ad9fef67881ab094a54f0258c40273ce0f694b6e6985ec312f3424/_data",
"Name": "e9d6633614ad9fef67881ab094a54f0258c40273ce0f694b6e6985ec312f3424",
"Options": {},
"Scope": "local"
}
]
创建一个数据卷
[root@docker1 ~]# docker volume create docker-volume
docker-volume
[root@docker1 ~]# docker volume ls
DRIVER VOLUME NAME
local 2a3f571ee6ae8b2ed9890a97faeb1e30d7ab1b0ad973bb0c6f4f4b0e3f544036
local 95b0346142cb123d97f49614bd21492b558660ea51e96672d7d3c52b6ec32f5f
local docker-volume
[root@docker1 ~]# cd /var/lib/docker/volumes/
[root@docker1 volumes]# ls
2a3f571ee6ae8b2ed9890a97faeb1e30d7ab1b0ad973bb0c6f4f4b0e3f544036
95b0346142cb123d97f49614bd21492b558660ea51e96672d7d3c52b6ec32f5f
docker-volume