当前位置:   article > 正文

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

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

编写dockerfile挂载卷

编写dockerfile文件

  1. [root@wq docker-test-volume]# vim dockerfile1
  2. [root@wq docker-test-volume]# cat dockerfile1
  3. FROM centos
  4. VOLUME ["volume01","volume02"]
  5. CMD echo "------end------"
  6. CMD /bin/bash
  7. [root@wq docker-test-volume]#

使用dockerfile构建镜像

  1. [root@wq docker-test-volume]# docker build -f dockerfile1 -t wq/centos:1.0 .
  2. [+] Building 0.2s (5/5) FINISHED docker:default
  3. => [internal] load build definition from dockerfile1 0.1s
  4. => => transferring dockerfile: 124B 0.0s
  5. => [internal] load .dockerignore 0.1s
  6. => => transferring context: 2B 0.0s
  7. => [internal] load metadata for docker.io/library/centos:latest 0.0s
  8. => [1/1] FROM docker.io/library/centos 0.0s
  9. => exporting to image 0.0s
  10. => => exporting layers 0.0s
  11. => => writing image sha256:c32b80a59aaf0df707899602daf20e786973349dbc09307d516646b5c73db27f 0.0s
  12. => => naming to docker.io/wq/centos:1.0 0.0s

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

  1. [root@wq docker-test-volume]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. tomcat02 1.0 02ea20370fec 5 hours ago 684MB
  4. nginx latest 605c77e624dd 2 years ago 141MB
  5. tomcat latest fb5657adc892 2 years ago 680MB
  6. wordpress latest c3c92cc3dcb1 2 years ago 616MB
  7. redis latest 7614ae9453d1 2 years ago 113MB
  8. mysql 5.7 c20987f18b13 2 years ago 448MB
  9. mysql latest 3218b38490ce 2 years ago 516MB
  10. centos latest 5d0da3dc9764 2 years ago 231MB
  11. wq/centos 1.0 c32b80a59aaf 2 years ago 231MB
  12. portainer/portainer latest 580c0e4e98b0 2 years ago 79.1MB
  13. elasticsearch 7.6.2 f29a1ee41030 3 years ago 791MB
  14. [root@wq docker-test-volume]#
  15. [root@wq docker-test-volume]#
  16. [root@wq docker-test-volume]# docker run -it c32b80a59aaf /bin/bash
  17. [root@c9a8bacfba06 /]# ls -l
  18. total 56
  19. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  20. drwxr-xr-x 5 root root 360 Mar 2 12:28 dev
  21. drwxr-xr-x 1 root root 4096 Mar 2 12:28 etc
  22. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  23. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  24. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  25. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  26. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  27. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  28. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  29. dr-xr-xr-x 174 root root 0 Mar 2 12:28 proc
  30. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  31. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  32. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  33. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  34. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  35. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  36. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  37. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  38. drwxr-xr-x 2 root root 4096 Mar 2 12:28 volume01
  39. drwxr-xr-x 2 root root 4096 Mar 2 12:28 volume02
  40. [root@c9a8bacfba06 /]# exit
  41. exit

查看卷挂载的路径

  1. [root@wq ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 75bf676f94c1 c32b80a59aaf "/bin/bash" About a minute ago Up About a minute crazy_torvalds
  4. [root@wq ~]# docker inspect 75bf676f94c1

测试文件是否同步

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

  1. [root@c9a8bacfba06 /]# ls -l
  2. total 56
  3. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  4. drwxr-xr-x 5 root root 360 Mar 2 12:28 dev
  5. drwxr-xr-x 1 root root 4096 Mar 2 12:28 etc
  6. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  7. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  8. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  9. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  10. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  11. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  12. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  13. dr-xr-xr-x 174 root root 0 Mar 2 12:28 proc
  14. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  15. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  16. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  17. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  18. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  19. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  20. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  21. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  22. drwxr-xr-x 2 root root 4096 Mar 2 12:28 volume01
  23. drwxr-xr-x 2 root root 4096 Mar 2 12:28 volume02
  24. [root@75bf676f94c1 /]# cd volume01
  25. [root@75bf676f94c1 volume01]# touch test.txt
  26. [root@75bf676f94c1 volume01]# ls
  27. test.txt
  28. [root@75bf676f94c1 volume01]#
  29. [root@75bf676f94c1 volume01]# pwd
  30. /volume01
  31. [root@75bf676f94c1 volume01]# exit
  32. exit

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

  1. [root@wq ~]# cd /var/lib/docker/volumes/e6c1b5cd8bf557012f82df80300702dd316fbcbed3188a601f6c6256c855ee32/_data
  2. [root@wq _data]# ls
  3. test.txt
  4. [root@wq _data]#

测试成功


数据容器卷

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

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

  1. [root@wq ~]# docker images |grep centos
  2. centos latest 5d0da3dc9764 2 years ago 231MB
  3. wq/centos 1.0 c32b80a59aaf 2 years ago 231MB
  4. [root@wq ~]# docker run -it --name centos01 wq/centos:1.0
  5. [root@8827c22608a0 /]# ls -l
  6. total 56
  7. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  8. drwxr-xr-x 5 root root 360 Mar 3 05:36 dev
  9. drwxr-xr-x 1 root root 4096 Mar 3 05:36 etc
  10. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  11. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  12. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  13. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  14. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  15. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  16. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  17. dr-xr-xr-x 152 root root 0 Mar 3 05:36 proc
  18. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  19. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  20. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  21. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  22. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  23. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  24. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  25. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  26. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume01
  27. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume02
  28. [root@8827c22608a0 /]#

创建centos02容器

  1. [root@wq ~]# docker ps |grep centos
  2. 8827c22608a0 wq/centos:1.0 "/bin/sh -c /bin/bash" About a minute ago Up About a minute centos01
  3. #--volumes-from 数据卷容器,挂载到centos01上
  4. [root@wq ~]# docker run -it --name centos02 --volumes-from centos01 wq/centos:1.0
  5. [root@1eefed24461c /]# ls -l
  6. total 56
  7. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  8. drwxr-xr-x 5 root root 360 Mar 3 05:38 dev
  9. drwxr-xr-x 1 root root 4096 Mar 3 05:38 etc
  10. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  11. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  12. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  13. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  14. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  15. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  16. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  17. dr-xr-xr-x 153 root root 0 Mar 3 05:38 proc
  18. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  19. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  20. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  21. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  22. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  23. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  24. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  25. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  26. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume01
  27. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume02

查看当前运行的容器

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

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

  1. [root@wq ~]# docker attach 8827c22608a0
  2. [root@8827c22608a0 /]# ls -l
  3. total 56
  4. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  5. drwxr-xr-x 5 root root 360 Mar 3 05:36 dev
  6. drwxr-xr-x 1 root root 4096 Mar 3 05:36 etc
  7. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  8. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  9. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  10. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  11. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  12. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  13. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  14. dr-xr-xr-x 155 root root 0 Mar 3 05:36 proc
  15. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  16. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  17. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  18. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  19. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  20. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  21. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  22. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  23. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume01
  24. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume02
  25. [root@8827c22608a0 /]# cd volume01
  26. [root@8827c22608a0 volume01]# ls
  27. [root@8827c22608a0 volume01]# touch centos01
  28. [root@8827c22608a0 volume01]# ls
  29. centos01
  30. [root@8827c22608a0 volume01]#

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

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

  1. [root@wq ~]# docker run -it --name centos03 --volumes-from centos01 wq/centos:1.0
  2. [root@90918e6deae0 /]# ls -l
  3. total 56
  4. lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
  5. drwxr-xr-x 5 root root 360 Mar 3 05:50 dev
  6. drwxr-xr-x 1 root root 4096 Mar 3 05:50 etc
  7. drwxr-xr-x 2 root root 4096 Nov 3 2020 home
  8. lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
  9. lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
  10. drwx------ 2 root root 4096 Sep 15 2021 lost+found
  11. drwxr-xr-x 2 root root 4096 Nov 3 2020 media
  12. drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
  13. drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
  14. dr-xr-xr-x 163 root root 0 Mar 3 05:50 proc
  15. dr-xr-x--- 2 root root 4096 Sep 15 2021 root
  16. drwxr-xr-x 11 root root 4096 Sep 15 2021 run
  17. lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
  18. drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
  19. dr-xr-xr-x 13 root root 0 Feb 27 06:47 sys
  20. drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
  21. drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
  22. drwxr-xr-x 20 root root 4096 Sep 15 2021 var
  23. drwxr-xr-x 2 root root 4096 Mar 3 05:39 volume01
  24. drwxr-xr-x 2 root root 4096 Mar 3 05:36 volume02
  25. [root@90918e6deae0 /]# cd volume01
  26. [root@90918e6deae0 volume01]# ls
  27. centos01
  28. [root@90918e6deae0 volume01]#

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

  1. #centos03的操作
  2. [root@90918e6deae0 /]# cd volume01
  3. [root@90918e6deae0 volume01]# ls
  4. centos01
  5. [root@90918e6deae0 volume01]# touch centos03
  6. [root@90918e6deae0 volume01]# ls
  7. centos01 centos03
  8. [root@90918e6deae0 volume01]#
  9. #centos01的操作
  10. [root@8827c22608a0 /]# cd volume01
  11. [root@8827c22608a0 volume01]# ls
  12. centos01
  13. #centos03上创建好之后查看的
  14. [root@8827c22608a0 volume01]# ls
  15. centos01 centos03
  16. [root@8827c22608a0 volume01]#

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

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

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

实战案例:

多个mysql实现数据共享

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

  1. [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
  2. aaea112657e0062c205e12a05f5a43f60cf82e890ca9ab211cddcaffaf51c464
  3. [root@wq ~]# docker ps |grep mysql
  4. 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
  5. 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
  6. [root@wq ~]# docker run -d -p 8032:3306 -e MYSQL_ROOT_PASSWORD=000000 --name mysql02 --volumes-from mysql01 mysql:5.7
  7. 2f240ebc39900448342992075435f0c8250df7acba604e9a0cfdd16c3c3d0c6d
  8. [root@wq ~]# docker ps |grep mysql
  9. 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
  10. 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
  11. 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

  1. [root@wq ~]# docker exec -it aaea112657e0 /bin/bash
  2. root@aaea112657e0:/#
  3. root@aaea112657e0:/# mysql -uroot -p000000
  4. mysql: [Warning] Using a password on the command line interface can be insecure.
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 2
  7. Server version: 5.7.36 MySQL Community Server (GPL)
  8. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> show databases;
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | sys |
  21. +--------------------+
  22. 4 rows in set (0.00 sec)
  23. mysql> create database w;
  24. Query OK, 1 row affected (0.00 sec)
  25. mysql> show databases;
  26. +--------------------+
  27. | Database |
  28. +--------------------+
  29. | information_schema |
  30. | mysql |
  31. | performance_schema |
  32. | sys |
  33. | w |
  34. +--------------------+
  35. 5 rows in set (0.01 sec)
  36. mysql> exit;
  37. Bye
  38. root@aaea112657e0:/# exit
  39. exit
  40. [root@wq ~]# docker stop aaea112657e0
  41. aaea112657e0
  42. #需要退出,并且停止mysql01容器运行,否则进不了mysql02

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

  1. [root@wq ~]# docker exec -it 2f240ebc3990 /bin/bash
  2. root@2f240ebc3990:/# mysql -uroot -p000000
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 2
  6. Server version: 5.7.36 MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> show databases;
  13. +--------------------+
  14. | Database |
  15. +--------------------+
  16. | information_schema |
  17. | mysql |
  18. | performance_schema |
  19. | sys |
  20. | w |
  21. +--------------------+
  22. 5 rows in set (0.00 sec)
  23. mysql>

验证成功

结论:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/186703?site
推荐阅读
相关标签
  

闽ICP备14008679号