编写dockerfile挂载卷、数据容器卷

编写dockerfile挂载卷

编写dockerfile文件

bash 复制代码
[root@wq docker-test-volume]# vim dockerfile1
[root@wq docker-test-volume]# cat dockerfile1
FROM centos

VOLUME ["volume01","volume02"]

CMD echo "------end------"
CMD /bin/bash
[root@wq docker-test-volume]#

使用dockerfile构建镜像

bash 复制代码
[root@wq docker-test-volume]# docker build -f dockerfile1 -t wq/centos:1.0 .
[+] Building 0.2s (5/5) FINISHED                                                                     docker:default
 => [internal] load build definition from dockerfile1                                                          0.1s
 => => transferring dockerfile: 124B                                                                           0.0s
 => [internal] load .dockerignore                                                                              0.1s
 => => 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:c32b80a59aaf0df707899602daf20e786973349dbc09307d516646b5c73db27f                   0.0s
 => => naming to docker.io/wq/centos:1.0                                                                       0.0s

查看镜像,并用此镜像创建容器

bash 复制代码
[root@wq docker-test-volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED       SIZE
tomcat02              1.0       02ea20370fec   5 hours ago   684MB
nginx                 latest    605c77e624dd   2 years ago   141MB
tomcat                latest    fb5657adc892   2 years ago   680MB
wordpress             latest    c3c92cc3dcb1   2 years ago   616MB
redis                 latest    7614ae9453d1   2 years ago   113MB
mysql                 5.7       c20987f18b13   2 years ago   448MB
mysql                 latest    3218b38490ce   2 years ago   516MB
centos                latest    5d0da3dc9764   2 years ago   231MB
wq/centos             1.0       c32b80a59aaf   2 years ago   231MB
portainer/portainer   latest    580c0e4e98b0   2 years ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   3 years ago   791MB
[root@wq docker-test-volume]#
[root@wq docker-test-volume]#
[root@wq docker-test-volume]# docker run -it c32b80a59aaf /bin/bash
[root@c9a8bacfba06 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  2 12:28 dev
drwxr-xr-x   1 root root 4096 Mar  2 12:28 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 174 root root    0 Mar  2 12:28 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  2 12:28 volume01
drwxr-xr-x   2 root root 4096 Mar  2 12:28 volume02
[root@c9a8bacfba06 /]# exit
exit

查看卷挂载的路径

bash 复制代码
[root@wq ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS              PORTS                                                  NAMES
75bf676f94c1   c32b80a59aaf          "/bin/bash"              About a minute ago   Up About a minute                                                          crazy_torvalds
[root@wq ~]# docker inspect 75bf676f94c1

测试文件是否同步

在容器中的volume01创建一个test.txt文件

bash 复制代码
[root@c9a8bacfba06 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  2 12:28 dev
drwxr-xr-x   1 root root 4096 Mar  2 12:28 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 174 root root    0 Mar  2 12:28 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  2 12:28 volume01
drwxr-xr-x   2 root root 4096 Mar  2 12:28 volume02
[root@75bf676f94c1 /]# cd volume01
[root@75bf676f94c1 volume01]# touch test.txt
[root@75bf676f94c1 volume01]# ls
test.txt
[root@75bf676f94c1 volume01]#
[root@75bf676f94c1 volume01]# pwd
/volume01
[root@75bf676f94c1 volume01]# exit
exit

新建终端查看主机挂载的目录下有没有出现test.txt文件

bash 复制代码
[root@wq ~]# cd /var/lib/docker/volumes/e6c1b5cd8bf557012f82df80300702dd316fbcbed3188a601f6c6256c855ee32/_data
[root@wq _data]# ls
test.txt
[root@wq _data]#

测试成功


数据容器卷

作用:用来实现容器之间的数据共享

使用自己创建的镜像运行centos01

bash 复制代码
[root@wq ~]# docker images |grep centos
centos                latest    5d0da3dc9764   2 years ago    231MB
wq/centos             1.0       c32b80a59aaf   2 years ago    231MB
[root@wq ~]# docker run -it --name centos01 wq/centos:1.0
[root@8827c22608a0 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  3 05:36 dev
drwxr-xr-x   1 root root 4096 Mar  3 05:36 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 152 root root    0 Mar  3 05:36 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume01
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume02
[root@8827c22608a0 /]#

创建centos02容器

bash 复制代码
[root@wq ~]# docker ps |grep centos
8827c22608a0   wq/centos:1.0   "/bin/sh -c /bin/bash"   About a minute ago   Up About a minute                                                          centos01

#--volumes-from 数据卷容器,挂载到centos01上
[root@wq ~]# docker run -it --name centos02 --volumes-from centos01 wq/centos:1.0
[root@1eefed24461c /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  3 05:38 dev
drwxr-xr-x   1 root root 4096 Mar  3 05:38 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 153 root root    0 Mar  3 05:38 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume01
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume02

查看当前运行的容器

bash 复制代码
[root@wq ~]# docker ps |grep centos
1eefed24461c   wq/centos:1.0   "/bin/sh -c /bin/bash"   4 minutes ago   Up 4 minutes                                                          centos02
8827c22608a0   wq/centos:1.0   "/bin/sh -c /bin/bash"   6 minutes ago   Up 6 minutes                                                    centos01

新建终端,进入第一个容器centos01,进行测试

bash 复制代码
[root@wq ~]# docker attach 8827c22608a0
[root@8827c22608a0 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  3 05:36 dev
drwxr-xr-x   1 root root 4096 Mar  3 05:36 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 155 root root    0 Mar  3 05:36 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume01
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume02
[root@8827c22608a0 /]# cd volume01
[root@8827c22608a0 volume01]# ls
[root@8827c22608a0 volume01]# touch centos01
[root@8827c22608a0 volume01]# ls
centos01
[root@8827c22608a0 volume01]#

再新建终端,进入第二个容器,查看是否存在文件

创建centos03容器,再次挂载到centos01上,发现数据还是同步的

bash 复制代码
[root@wq ~]# docker run -it --name centos03 --volumes-from centos01 wq/centos:1.0
[root@90918e6deae0 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Mar  3 05:50 dev
drwxr-xr-x   1 root root 4096 Mar  3 05:50 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15  2021 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 163 root root    0 Mar  3 05:50 proc
dr-xr-x---   2 root root 4096 Sep 15  2021 root
drwxr-xr-x  11 root root 4096 Sep 15  2021 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Feb 27 06:47 sys
drwxrwxrwt   7 root root 4096 Sep 15  2021 tmp
drwxr-xr-x  12 root root 4096 Sep 15  2021 usr
drwxr-xr-x  20 root root 4096 Sep 15  2021 var
drwxr-xr-x   2 root root 4096 Mar  3 05:39 volume01
drwxr-xr-x   2 root root 4096 Mar  3 05:36 volume02
[root@90918e6deae0 /]# cd volume01
[root@90918e6deae0 volume01]# ls
centos01
[root@90918e6deae0 volume01]#

在centos03上进行创建文件,在centos01上查看

bash 复制代码
#centos03的操作
[root@90918e6deae0 /]# cd volume01
[root@90918e6deae0 volume01]# ls
centos01
[root@90918e6deae0 volume01]# touch centos03
[root@90918e6deae0 volume01]# ls
centos01  centos03
[root@90918e6deae0 volume01]#


#centos01的操作
[root@8827c22608a0 /]# cd volume01
[root@8827c22608a0 volume01]# ls
centos01
#centos03上创建好之后查看的
[root@8827c22608a0 volume01]# ls
centos01  centos03
[root@8827c22608a0 volume01]#

测试 删除centos01容器,查看centos02和centos03上的文件是否存在

最后发现删除centos01之后,文件依旧存在

所以这是备份机制,并不是共享机制

实战案例:

多个mysql实现数据共享

创建两个mysql容器mysql01和mysql02,使用--volumes-from将mysql02挂载到mysql01

bash 复制代码
[root@wq ~]# docker run -d -p 8031:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=000000 --name mysql01 mysql:5.7
aaea112657e0062c205e12a05f5a43f60cf82e890ca9ab211cddcaffaf51c464
[root@wq ~]# docker ps |grep mysql
aaea112657e0   mysql:5.7   "docker-entrypoint.s..."   8 seconds ago   Up 7 seconds   33060/tcp, 0.0.0.0:8031->3306/tcp, :::8031->3306/tcp   mysql01
94239899c5bd   mysql       "docker-entrypoint.s..."   2 days ago      Up 2 days      33060/tcp, 0.0.0.0:8006->3306/tcp, :::8006->3306/tcp   my_mysql


[root@wq ~]# docker run -d -p 8032:3306 -e MYSQL_ROOT_PASSWORD=000000 --name mysql02 --volumes-from mysql01 mysql:5.7
2f240ebc39900448342992075435f0c8250df7acba604e9a0cfdd16c3c3d0c6d
[root@wq ~]# docker ps |grep mysql
2f240ebc3990   mysql:5.7   "docker-entrypoint.s..."   6 seconds ago        Up 5 seconds        33060/tcp, 0.0.0.0:8032->3306/tcp, :::8032->3306/tcp   mysql02
aaea112657e0   mysql:5.7   "docker-entrypoint.s..."   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:8031->3306/tcp, :::8031->3306/tcp   mysql01
94239899c5bd   mysql       "docker-entrypoint.s..."   2 days ago           Up 2 days           33060/tcp, 0.0.0.0:8006->3306/tcp, :::8006->3306/tcp   my_mysql

测试

进入mysql01容器登录mysql

bash 复制代码
[root@wq ~]# docker exec -it aaea112657e0 /bin/bash
root@aaea112657e0:/#
root@aaea112657e0:/# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database w;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| w                  |
+--------------------+
5 rows in set (0.01 sec)

mysql> exit;
Bye
root@aaea112657e0:/# exit
exit
[root@wq ~]# docker stop aaea112657e0
aaea112657e0

#需要退出,并且停止mysql01容器运行,否则进不了mysql02

进入mysql02容器,登录mysql02进行查看验证库是否存在

bash 复制代码
[root@wq ~]# docker exec -it 2f240ebc3990 /bin/bash
root@2f240ebc3990:/# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| w                  |
+--------------------+
5 rows in set (0.00 sec)

mysql>

验证成功

结论:

容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器为止

一旦持久化了本地,这时候本地的数据是不会删除的

相关推荐
锐策5 小时前
〔 MySQL 〕数据库基础
数据库·mysql
舞动CPU5 小时前
linux c/c++最高效的计时方法
linux·运维·服务器
皮锤打乌龟5 小时前
(干货)Jenkins使用kubernetes插件连接k8s的认证方式
运维·kubernetes·jenkins
钰@5 小时前
小程序开发者工具的network选项卡中有某域名的接口请求,但是在charles中抓不到该接口
运维·服务器·小程序
日月星宿~6 小时前
【MySQL】summary
数据库·mysql
wanhengwangluo6 小时前
云服务器和物理服务器的区别有哪些?
运维·服务器
南猿北者6 小时前
docker Network(网络)
网络·docker·容器
秦jh_7 小时前
【Linux】多线程(概念,控制)
linux·运维·前端
希忘auto7 小时前
详解MySQL安装
java·mysql
yaosheng_VALVE7 小时前
稀硫酸介质中 V 型球阀的材质选择与选型要点-耀圣
运维·spring cloud·自动化·intellij-idea·材质·1024程序员节