docker基础学习之三:容器存储

持久化数据指的是数据的生命周期和容器的不一致,容器删除后数据还在。
非持久化数据指的是它的生命周期和容器一致,容器删除后数据也没了。
docker容器中持久化数据一般采用两种存储方式
volume
bind mount

一、volume进行容器挂载数据卷
1、创建容器
root@herrychen:~# 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 删除一个或多个数据卷

root@herrychen:~# docker volume ls
DRIVER VOLUME NAME
local 2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539

3、查看volume的详细信息,volume把本地的一个自动生成的目录挂载给容器的目录。
root@herrychen:~# docker inspect b961a
在这里插入图片描述

4、查看本地的那个自动生成的目录文件内容
在这里插入图片描述

root@herrychen:~# 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、进入容器内修改主页内容
root@herrychen:~#
root@herrychen:~# docker exec -it b961a bash
root@b961ae49dacd:/usr/local/apache2# cd htdocs/
root@b961ae49dacd:/usr/local/apache2/htdocs# ls
index.html
root@b961ae49dacd:/usr/local/apache2/htdocs# echo “update the index” > index.html
root@b961ae49dacd:/usr/local/apache2/htdocs# exit
exit

root@herrychen:~# curl 127.0.0.1:8080
update the index

进入容器修改主页已经成功了。

6、本地目录里的index.html也被修改了。进行了同步更新。
root@herrychen:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
update the index

7、强制删除容器
root@herrychen:~# docker rm -f b961ae49dacd
b961ae49dacd

8、本地自动生成的目录文件还在。
root@herrychen:/var/lib/docker/volumes/2a4455e2f9dcf03645d08c51a82206ef241cb3eb3e24891e82183f7f11ee5539/_data# cat index.html
update the index

二、bind mount进行容器挂载数据卷
1、创建文件
root@herrychen:~# mkdir htdocs
root@herrychen:~# ls
dockerfile htdocs

2、创建容器,:ro参数表示只读
root@herrychen:~# docker run --name httpd01 -d -p 8081:80 -v /root/htdocs/:/usr/local/apache2/htdocs:ro httpd
4b61ce6450e3a5efa2a2760e29ed0b99d2a01d5b223a8248c5345fc5d34c3479

3、查看主页内容,是空的,
在这里插入图片描述

4、容器里主页内容也是空的,说明没有文件,文件被/root/htdocs/给覆盖了。
同时进入容器修改文件内容也报错,文件是只读的。
root@herrychen:~# docker exec -it httpd01 bash
root@4b61ce6450e3:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@4b61ce6450e3:/usr/local/apache2# cd htdocs
root@4b61ce6450e3:/usr/local/apache2/htdocs# ls
root@4b61ce6450e3:/usr/local/apache2/htdocs# echo “bind mount” > index.html
bash: index.html: Read-only file system

3、只能通过对宿主机目录文件的修改进行同步。
root@herrychen:~# cd htdocs/
root@herrychen:~/htdocs# ls
root@herrychen:~/htdocs# vim index.html
bind mount
root@herrychen:~/htdocs# cat index.html
bind mount

root@herrychen:~/htdocs# curl 127.0.0.1:8081
bind mount

4、不带:ro表示数据卷可读写,进入容器修改文件内容也可以了。
说明bind mount不但可以把宿主机的目录挂载给容器,同时可以限制对容器内的目录的读写权限。

root@herrychen:~#docker run --name httpd02 -d -p 8082:80 -v /root/htdocs/:/usr/local/apache2/htdocs httpd
3ews1ce6450e3a5efa2a2760e291d0b99d2a01d5b223a8248c5345fc5d34c3567

root@herrychen:~#docker exec -it httpd2 bash
root@3ews1ce6450e:/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、创建数据卷
root@herrychen:~# docker volume create mynewvolume
mynewvolume
root@herrychen:~# docker volume ls
DRIVER VOLUME NAME
local mynewvolume

2、创建一个不需要运行的容器,因为只需要对其他容器提供一个挂载点。
root@herrychen:~# docker create --name vc -v mynewvolume:/usr/local/apache2/htdocs httpd
ac4d6026e0317902faa65e4d31ba697371622075eb5feea7ad8d42503d566121

3、创建两个业务容器,
–volumes-from是挂载数据卷,将httpd3容器、httpd4容器的数据卷挂载到vc容器中
root@herrychen:~# docker run --name http3 -d -p 1003:80 --volumes-from vc httpd
74db47fb9876571ba8b483e13eec21eb59d9e2a9dc330a23178b2433a6cb61b9

root@herrychen:~# docker run --name http4 -d -p 1004:80 --volumes-from vc httpd
60ab284ad4705900e358456b17ee571deb998733e43c7f7953673d756ae85b78

root@herrychen:~# 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容器修改主页文件
root@herrychen:~#docker exec -it http3 bash
#cd htdocs/
#ls
index.html
#echo “new data” > index.html
#exit

root@herrychen:~#curl 127.0.0.1:1003
new data

5、看到httpd4容器也更新了,
root@herrychen:~#curl 127.0.0.1:1004
new data

可见volume container是做容器共享数据存储非常好的一种模式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值