Docker - 容器数据卷

news/2024/11/17 21:50:15/

Docker - 容器数据卷

什么是容器数据卷

等同于挂载,将容器内的目录地址指向于宿主机文件系统中

在这里插入图片描述

直接使用命令来挂载 -v

docker run -it -v 主机目录:容器内目录# 测试 docker run -it -v /root:/home centos /bin/bash
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -it -v /root:/home centos /bin/bash
[root@91fda5ea1cef /]# # 通过命令查看:docker inspect 容器id 是否挂载成功"Mounts": [{"Type": "bind","Source": "/root","Destination": "/home","Mode": "","RW": true,"Propagation": "rprivate"}]# 测试2 将容器停止后修改文件再次启动查看
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker exec -it 容器id /bin/bash

Mysql数据持久化

# 拉取mysql镜像
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker pull mysql:5.7# 运行容器,需要做数据挂载! # 安装启动mysql,需要配置密码的!
# 官方测试:docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag# 测试启动
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -p 3306:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql mysql:5.7# 开通3306端口
[root@iZ2zeg7mctvft5renx1qvbZ ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent# 重启docker
[root@iZ2zeg7mctvft5renx1qvbZ ~]# systemctl restart docker# 重启mysql容器
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker start 容器id

具名和匿名挂载

# 匿名挂载
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -P --name nginx01 -v /ect/nginx nginx
e388dde55561cd863b831946d4d4217eedd2d47853ad5bf795b26abd044c37e5
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker volume --helpUsage:  docker volume COMMANDManage volumesCommands:create      Create a volumeinspect     Display detailed information on one or more volumesls          List volumesprune       Remove unused local volumesrm          Remove one or more volumesRun 'docker volume COMMAND --help' for more information on a command.
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker volume ls
DRIVER    VOLUME NAME
local     503b749c6ec21d5c91a8c84b4459f6f900c2f71210943e2558c419b5738f9490
local     b99393a99583fadbd8a7a1438cf94541db1cfe41764f19740a6e823b71bf3ccd
local     f80209292f35958b4f5326d715843b3f11554774b6e616bf90665757125919c8# 具名挂载
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -P --name nginx -v juming-nginx:/etc/nginx nginx
b0af58763139d2ec61b984938f7a37b24f7fe1490af3e0be05f24499a8ab186f
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker volume ls
DRIVER    VOLUME NAME
local     juming-nginx
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker volume inspect juming-nginx
[{"CreatedAt": "2023-11-10T10:10:05+08:00","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data","Name": "juming-nginx","Options": null,"Scope": "local"}
]
[root@iZ2zeg7mctvft5renx1qvbZ _data]# pwd
/var/lib/docker/volumes/juming-nginx/_data
[root@iZ2zeg7mctvft5renx1qvbZ _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

如何确认是具名挂载和匿名挂载

-v 容器内路径	    	   # 匿名挂载
-v 卷名:容器内路径		     # 具名挂载
-v /宿主机路径:容器内路径	   # 指定路径挂载

拓展

# 通过 -v 容器路径:ro rw改变读写权限
ro readonly		# 只读
rw readwrite	# 可读可写# 一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了
docker run -d -P --name nginx -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx -v juming-nginx:/etc/nginx:rw nginx# ro只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作的!

数据卷容器

  1. 生成镜像
[root@iZ2zeg7mctvft5renx1qvbZ volume]# pwd
/root/volume
[root@iZ2zeg7mctvft5renx1qvbZ volume]# cat yilu
# 创建一个dockerfile文件,名字可以随机 建议 Dockerfile
# 文件中的内容 指令(大写)参数
FROM centosVOLUME ["volume01","volume02"]			# 匿名挂载CMD echo "-----end-----"CMD /bin/bash
[root@iZ2zeg7mctvft5renx1qvbZ volume]# docker build -f /root/volume/yilu -t yilu/centos:1.0 .
[+] Building 0.1s (5/5) FINISHED                                                                                                                          docker:default=> [internal] load build definition from yilu                                                                                                                      0.0s=> => transferring dockerfile: 116B                                                                                                                                0.0s=> [internal] load .dockerignore                                                                                                                                   0.0s=> => transferring context: 2B                                                                                                                                     0.0s=> [internal] load metadata for docker.io/library/centos:latest                                                                                                    0.0s=> [1/1] FROM docker.io/library/centos                                                                                                                             0.0s=> exporting to image                                                                                                                                              0.0s=> => exporting layers                                                                                                                                             0.0s=> => writing image sha256:5f6352c382306e2ddce0a4220b0d7c1ad60a96a2c5a34be60d6025b388eeb778                                                                        0.0s=> => naming to docker.io/yilu/centos:1.0                                                                                                                          0.0s
[root@iZ2zeg7mctvft5renx1qvbZ volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
yilu/centos           1.0       5f6352c38230   2 years ago     231MB# 这里的每个命令,就是镜像的一层
  1. 根据镜像生成容器
[root@iZ2zeg7mctvft5renx1qvbZ volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
yilu/centos           1.0       5f6352c38230   2 years ago     231MB
[root@iZ2zeg7mctvft5renx1qvbZ volume]# docker run -it 5f6352c38230 /bin/bash
[root@54e430545b09 /]# ls -l
total 0
lrwxrwxrwx   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root 360 Nov 10 03:01 dev
drwxr-xr-x   1 root root  66 Nov 10 03:01 etc
drwxr-xr-x   2 root root   6 Nov  3  2020 home
lrwxrwxrwx   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root   6 Sep 15  2021 lost+found
drwxr-xr-x   2 root root   6 Nov  3  2020 media
drwxr-xr-x   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x 132 root root   0 Nov 10 03:01 proc
dr-xr-x---   2 root root 162 Sep 15  2021 root
drwxr-xr-x  11 root root 163 Sep 15  2021 run
lrwxrwxrwx   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x  13 root root   0 Nov 10 03:01 sys
drwxrwxrwt   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x  12 root root 144 Sep 15  2021 usr
drwxr-xr-x  20 root root 262 Sep 15  2021 var
drwxr-xr-x   2 root root   6 Nov 10 03:01 volume01														# 挂载目录
drwxr-xr-x   2 root root   6 Nov 10 03:01 volume02														# 挂载目录# 查看容器信息
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker inspect 795ef6523dd3"Mounts": [{"Type": "volume","Name": "c8f9475c98d8969e43a9850da2a4f7f74283ceea41e2be081ae85b2298a46489","Source": "/var/lib/docker/volumes/c8f9475c98d8969e43a9850da2a4f7f74283ceea41e2be081ae85b2298a46489/_data","Destination": "volume01","Driver": "local","Mode": "","RW": true,"Propagation": ""},{"Type": "volume","Name": "a28b7a6f95e6061df744f4e39ac12a4689f644bd2eb3aff2377a59a5cd161101","Source": "/var/lib/docker/volumes/a28b7a6f95e6061df744f4e39ac12a4689f644bd2eb3aff2377a59a5cd161101/_data","Destination": "volume02","Driver": "local","Mode": "","RW": true,"Propagation": ""}],
  1. 容器之前的绑定

    将容器之前进行卷绑定

# docker run -it --name docker 镜像id
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -it --name docker 5f6352c38230
# docker run -it --name docker01 --volumes-from 容器id  镜像id
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -it --name docker01 --volumes-from 195c1fc93f87  5f6352c38230
  1. Mysql之间实现容器卷绑定
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -p 3306:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysql mysql:5.7
585993811f7e5d1983c6eeb125f79952e1871bd09c111293a88f7696afecdc75
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
585993811f7e   mysql:5.7   "docker-entrypoint.s…"   47 seconds ago   Up 46 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql# docker run -d -p 3307:3306 --name mysql01 --volumes-from 容器id -e MYSQL_ROOT_PASSWORD=root 镜像id
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -p 3307:3306 --name mysql01 --volumes-from 585993811f7e -e MYSQL_ROOT_PASSWORD=root 547b3c3c15a9
f235ad4d0e0cf4a8ac7245fc3362ca174731aefedacd07b036ad83e3649e5e0b

通过上面命令启动两个mysql后发现一个问题点:在第一个Mysql起来后,再拉起另一个mysql时,另一个mysql访问不到。后了一会后第二个mysql会被删除。

通过 docker logs 容器id 命令查看后

[root@iZ2zeg7mctvft5renx1qvbZ data]# docker logs 4eaa9a7c3c50
2023-11-10T05:40:15.936101Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2023-11-10T05:40:15.936125Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2023-11-10T05:40:16.936231Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2023-11-10T05:40:16.936257Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2023-11-10T05:40:17.936378Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2023-11-10T05:40:17.936415Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2023-11-10T05:40:18.936527Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
2023-11-10T05:40:18.936557Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2023-11-10T05:40:18.936563Z 0 [Note] InnoDB: Unable to open the first data file
2023-11-10T05:40:18.936576Z 0 [ERROR] InnoDB: Operating system error number 11 in a file operation.
2023-11-10T05:40:18.936589Z 0 [ERROR] InnoDB: Error number 11 means 'Resource temporarily unavailable'
2023-11-10T05:40:18.936593Z 0 [Note] InnoDB: Some operating system error numbers are described at http://dev.mysql.com/doc/refman/5.7/en/operating-system-error-codes.html
2023-11-10T05:40:18.936596Z 0 [ERROR] InnoDB: Cannot open datafile './ibdata1'
2023-11-10T05:40:18.936614Z 0 [ERROR] InnoDB: Could not open or create the system tablespace. If you tried to add new data files to the system tablespace, and it failed here, you should now edit innodb_data_file_path in my.cnf back to what it was, and remove the new ibdata files InnoDB created in this failed attempt. InnoDB only wrote those files full of zeros, but did not yet use them in any way. But be careful: do not remove old data files which contain your precious data!
2023-11-10T05:40:18.936621Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Cannot open a file
2023-11-10T05:40:19.537157Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2023-11-10T05:40:19.537186Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2023-11-10T05:40:19.537191Z 0 [ERROR] Failed to initialize builtin plugins.
2023-11-10T05:40:19.537194Z 0 [ERROR] Aborting

两个实例使用同一份数据卷,从错误来看innodb数据文件加了锁导致第二个实例无法正常读取。我对于共享数据出现了偏差,认为2个mysql容器共享一个数据卷可以实现2个数据库内容完全一致,但是MySQL并不属于存算分离架构做不到一份数据同时被多个实例使用。


http://www.ppmy.cn/news/1215737.html

相关文章

微软允许OEM对Win10不提供关闭Secure Boot

用户可能将无法在Windows 10电脑上安装其它操作系统了,微软不再要求OEM在UEFI 中提供的“关闭 Secure Boot”的选项。 微软最早是在Designed for Windows 8认证时要求OEM的产品必须支持UEFI Secure Boot。Secure Boot 被设计用来防止恶意程序悄悄潜入到引导进程。问…

EasyPOI实现excel文件导出

EasyPOI真的是一款非常好用的文件导出工具&#xff0c;相较于传统的一行一列的数据导出&#xff0c;这种以实体类绑定生成的方式真的非常方便&#xff0c;也希望大家能够了解、掌握其使用方法&#xff0c;下面就用一个实例来简单介绍一下EasyPOI的使用。 1.导入依赖 <!-- e…

Django——路由层

一. 路由匹配 1. 路由匹配注意事项 urlpatterns [url(r^admin/, admin.site.urls),# 首页url(r^$,views.home),# 路由匹配url(r^test/$,views.test),url(r^testadd/$,views.testadd),# 尾页(了解): 后期使用异常捕获处理, 这样的尾页让django的第二次在路径中斜杠APPEND_SL…

计算机网络期末复习-Part5

1、CRC计算 看例题&#xff1a;待发送序列为101110&#xff0c;生成多项式为X31&#xff0c;计算CRC校验码 先在待发送序列末尾添加与生成多项式次数相同的零&#xff0c;在上述例子中&#xff0c;生成多项式是X^3 1&#xff0c;所以需要添加3个零&#xff0c;待发送序列变成…

使用opencv实现图像的畸形矫正:仿射变换

1 仿射变换 1.1 什么是仿射变换 在图像处理中&#xff0c;经常需要对图像进行各种操作如平移、缩放、旋转、翻转等&#xff0c;这些都是图像的仿射变换。图像仿射变换又称为图像仿射映射&#xff0c;是指在几何中&#xff0c;一个向量空间进行一次线性变换并接上一个平移&…

结合大模型进行降本增效之——自动化测试

软件测试中&#xff0c;有哪些步骤能结合大模型的AIGC和数据分析能力&#xff1f; 生成测试用例 利用GPT-3.5 Turbo的自然语言生成能力&#xff0c;让它根据需求自动生成测试用例。例如&#xff0c;你可以向GPT-3.5 Turbo提供关于某个功能或者页面的描述&#xff0c;然后让它生…

C/C++交换输出 2021年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析

目录 C/C交换输出 一、题目要求 1、编程实现 2、输入输出 二、算法分析 三、程序编写 四、程序说明 五、运行结果 六、考点分析 C/C交换输出 2021年9月 C/C编程等级考试一级编程题 一、题目要求 1、编程实现 输入两个整数a,b&#xff0c;将它们交换输出 2、输入输…

Ps:通过显示大小了解图像的打印尺寸

在 Photoshop 中&#xff0c;如果想了解文档窗口中的图像打印出来之后的实质大小&#xff0c;只要知道两个数值即可。 第一个数值是图像分辨率&#xff08;也称“文档分辨率”&#xff09;的大小&#xff0c;可在Ps菜单&#xff1a;图像/图像大小 Image Size对话框中查询或设置…