Docker - 容器数据卷
什么是容器数据卷
等同于挂载,将容器内的目录地址指向于宿主机文件系统中
直接使用命令来挂载 -v
shell
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数据持久化
shell
# 拉取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
具名和匿名挂载
shell
# 匿名挂载
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -P --name nginx01 -v /ect/nginx nginx
e388dde55561cd863b831946d4d4217eedd2d47853ad5bf795b26abd044c37e5
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove unused local volumes
rm Remove one or more volumes
Run '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
如何确认是具名挂载和匿名挂载
shell
-v 容器内路径 # 匿名挂载
-v 卷名:容器内路径 # 具名挂载
-v /宿主机路径:容器内路径 # 指定路径挂载
拓展
shell
# 通过 -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就说明这个路径只能通过宿主机来操作,容器内部是无法操作的!
数据卷容器
- 生成镜像
shell
[root@iZ2zeg7mctvft5renx1qvbZ volume]# pwd
/root/volume
[root@iZ2zeg7mctvft5renx1qvbZ volume]# cat yilu
# 创建一个dockerfile文件,名字可以随机 建议 Dockerfile
# 文件中的内容 指令(大写)参数
FROM centos
VOLUME ["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
# 这里的每个命令,就是镜像的一层
- 根据镜像生成容器
shell
[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": ""
}
],
-
容器之前的绑定
将容器之前进行卷绑定
shel
# 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
- Mysql之间实现容器卷绑定
shell
[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 命令查看后
shell[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并不属于存算分离架构做不到一份数据同时被多个实例使用。