docker容器使用存储卷进行数据持久化

1.将存储卷"test01"挂载到容器,若不存在则直接创建,默认权限为rw

[root@centos201 ~]# docker container run -v test01:/usr/share/nginx/html -d --name web01 nginx:1.20.1 
68f7609b7d72ba6e328605103cfb315b1a38aa2631ce69a576a228d1037300aa
[root@centos201 ~]# 




[17:22:31 root@centos201 ~]#docker container rm -f `docker container ps -qa`
b6b2e7abd637
426e7f87bfd2
[17:23:56 root@centos201 ~]#docker container run -v test01:/usr/share/nginx/html -d --name web01 nginx:1.16 
6152c57e20a701b49191d5f3e34f13ff0ef802a4d125067766629cd62f265e9d

[17:25:25 root@centos201 ~]#docker ps -l
CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS              PORTS     NAMES
6152c57e20a7   nginx:1.16   "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp    web01
[17:25:28 root@centos201 ~]#docker container inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" `docker container ps -lq`
172.17.0.2
[17:25:36 root@centos201 ~]#docker exec  -it web01 bash
root@6152c57e20a7:/# cd /usr/share/nginx/html/

root@6152c57e20a7:/usr/share/nginx/html# ls 
50x.html  index.html
root@6152c57e20a7:/usr/share/nginx/html# echo "<h1>haha</h1>" > index.html 
root@6152c57e20a7:/usr/share/nginx/html# 
root@6152c57e20a7:/usr/share/nginx/html# cat index.html 
<h1>haha</h1>
root@6152c57e20a7:/usr/share/nginx/html# 


[17:28:26 root@centos201 ~]#curl 172.17.0.2
<h1>haha</h1>
[17:28:41 root@centos201 ~]#

2.将"test01"存储卷以只读的方式挂载

[root@centos201 ~]# docker container run -v test01:/usr/share/nginx/html:ro -d --name web02 nginx:1.20.1  
9b51f5292893f6f07a1f36797191d99cc3b65fea965378ac3b0a99e27fc8ba88
[root@centos201 ~]# 

3.若存储卷"test02"不存在,则会自动创建。

[root@centos201 ~]# docker container run -v test02:/usr/share/nginx/html:rw -d --name web03 nginx:1.20.1  
4c68e3eec3b8a3feaa33977b3dd6ee1ff466807bffb60b7c513e6c431739c587
[root@centos201 ~]# 

4.将容器的"/usr/share/nginx/html"路径作为挂载点和本地的随机存储卷关联。

[root@centos201 ~]# docker container run -v /usr/share/nginx/html -d --name web04 nginx:1.20.1 

5.删除所有的存储卷

[root@centos201 ~]# docker volume rm `docker volume ls -q` 

6.删除所有的容器及匿名存储卷。

[root@centos201 ~]# docker container rm -fv `docker container ps -qa` 

7.将宿主机的路径以只读的方式挂载到容器的挂载点

[root@centos201 ~]# docker run -d -v /test/data:/usr/share/nginx/html:ro --name web01 nginx:1.20.1 

8.创建容器web03时使用和web01相同的存储卷。

[root@centos201 ~]# docker container run --volumes-from web01 -d --name web03 nginx:1.20.1
posted @ 2024-01-21 21:33  只为心情愉悦  阅读(40)  评论(0)    收藏  举报