持久化数据指的是数据的生命周期和容器的不一致,容器删除后数据还在。
非持久化数据指的是它的生命周期和容器一致,容器删除后数据也没了。
docker容器中持久化数据一般采用两种存储方式
volume
bind mount
一、volume进行容器挂载数据卷
1、创建容器
[email protected]:~# docker run -d -p 8080:80 -v /usr/local/apache2/htdocs httpd
Unable to find image ‘httpd:latest’ locally
latest: Pulling from library/httpd
54fec2fa59d0: Already exists
8219e18ac429: Pull complete
3ae1b816f5e1: Pull complete
a5aa59ad8b5e: Pull complete
4f6febfae8db: Pull complete
Digest: sha256:c9e4386ebcdf0583204e7a54d7a827577b5ff98b932c498e9ee603f7050db1c1
Status: Downloaded newer image for httpd:latest
b961ae49dacd8d4a0eac47d82def35fe584a6bdeb52499ef65edcc4fc69b0a10
2、docker volume用法
Commands:
create Create a volume 创建一个数据卷
inspect Display detailed information on one or more volumes 打印一个或多个数据卷的详细信息
ls List volumes 列出所有数据卷
prune Remove all unused local volumes 删除所有未使用的数据卷
rm Remove one or more volumes 删除一个或多个数据卷
[email protected]:~# docker volume ls
DRIVER VOLUME NAME
local 2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539
3、查看volume的详细信息,volume把本地的一个自动生成的目录挂载给容器的目录。
[email protected]errychen:~# docker inspect b961a
4、查看本地的那个自动生成的目录文件内容
[email protected]:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b961ae49dacd httpd “httpd-foreground” 19 minutes ago Up 19 minutes 0.0.0.0:8080->80/tcp laughing_panini
acbc44e0b486 registry “/entrypoint.sh /etc…” 13 hours ago Up 13 hours 0.0.0.0:1000->5000/tcp affectionate_burnell
5、进入容器内修改主页内容
[email protected]:~#
[email protected]:~# docker exec -it b961a bash
[email protected]:/usr/local/apache2# cd htdocs/
[email protected]:/usr/local/apache2/htdocs# ls
index.html
[email protected]:/usr/local/apache2/htdocs# echo “update the index” > index.html
[email protected]:/usr/local/apache2/htdocs# exit
exit
[email protected]:~# curl 127.0.0.1:8080
update the index
进入容器修改主页已经成功了。
6、本地目录里的index.html也被修改了。进行了同步更新。
[email protected]:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
update the index
7、强制删除容器
[email protected]:~# docker rm -f b961ae49dacd
b961ae49dacd
8、本地自动生成的目录文件还在。
[email protected]:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
update the index
二、bind mount进行容器挂载数据卷
1、创建文件
[email protected]:~# mkdir htdocs
[email protected]:~# ls
dockerfile htdocs
2、创建容器,:ro参数表示只读
[email protected]:~# docker run --name httpd01 -d -p 8081:80 -v /root/htdocs/:/usr/local/apache2/htdocs:ro httpd
4b61ce6450e3a5efa2a2760e29ed0b99d2a01d5b223a8248c5345fc5d34c3479
3、查看主页内容,是空的,
4、容器里主页内容也是空的,说明没有文件,文件被/root/htdocs/给覆盖了。
同时进入容器修改文件内容也报错,文件是只读的。
[email protected]:~# docker exec -it httpd01 bash
[email protected]:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
[email protected]:/usr/local/apache2# cd htdocs
[email protected]:/usr/local/apache2/htdocs# ls
[email protected]:/usr/local/apache2/htdocs# echo “bind mount” > index.html
bash: index.html: Read-only file system
3、只能通过对宿主机目录文件的修改进行同步。
[email protected]:~# cd htdocs/
[email protected]:~/htdocs# ls
[email protected]:~/htdocs# vim index.html
bind mount
[email protected]:~/htdocs# cat index.html
bind mount
[email protected]:~/htdocs# curl 127.0.0.1:8081
bind mount
4、不带:ro表示数据卷可读写,进入容器修改文件内容也可以了。
说明bind mount不但可以把宿主机的目录挂载给容器,同时可以限制对容器内的目录的读写权限。
[email protected]:~#docker run --name httpd02 -d -p 8082:80 -v /root/htdocs/:/usr/local/apache2/htdocs httpd
3ews1ce6450e3a5efa2a2760e291d0b99d2a01d5b223a8248c5345fc5d34c3567
[email protected]:~#docker exec -it httpd2 bash
[email protected]:/usr/local/apache2#cd htdocs/
#ls
index.html
#cat index.html
bind mount
#echo “i can change” > index.html
i can change
三、volume container的使用
1、创建数据卷
[email protected]:~# docker volume create mynewvolume
mynewvolume
[email protected]:~# docker volume ls
DRIVER VOLUME NAME
local mynewvolume
2、创建一个不需要运行的容器,因为只需要对其他容器提供一个挂载点。
[email protected]:~# docker create --name vc -v mynewvolume:/usr/local/apache2/htdocs httpd
ac4d6026e0317902faa65e4d31ba697371622075eb5feea7ad8d42503d566121
3、创建两个业务容器,
–volumes-from是挂载数据卷,将httpd3容器、httpd4容器的数据卷挂载到vc容器中
[email protected]:~# docker run --name http3 -d -p 1003:80 --volumes-from vc httpd
74db47fb9876571ba8b483e13eec21eb59d9e2a9dc330a23178b2433a6cb61b9
[email protected]:~# docker run --name http4 -d -p 1004:80 --volumes-from vc httpd
60ab284ad4705900e358456b17ee571deb998733e43c7f7953673d756ae85b78
[email protected]:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60ab284ad470 httpd “httpd-foreground” 23 seconds ago Up 21 seconds 0.0.0.0:1004->80/tcp http4
74db47fb9876 httpd “httpd-foreground” 54 seconds ago Up 52 seconds 0.0.0.0:1003->80/tcp http3
ac4d6026e031 httpd “httpd-foreground” 5 minutes ago Created vc
4、进入httpd3容器修改主页文件
[email protected]:~#docker exec -it http3 bash
#cd htdocs/
#ls
index.html
#echo “new data” > index.html
#exit
[email protected]:~#curl 127.0.0.1:1003
new data
5、看到httpd4容器也更新了,
[email protected]:~#curl 127.0.0.1:1004
new data
可见volume container是做容器共享数据存储非常好的一种模式。