编写dockerfile文件
[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构建镜像
[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
查看镜像,并用此镜像创建容器
[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
查看卷挂载的路径
[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文件
[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文件
[root@wq ~]# cd /var/lib/docker/volumes/e6c1b5cd8bf557012f82df80300702dd316fbcbed3188a601f6c6256c855ee32/_data
[root@wq _data]# ls
test.txt
[root@wq _data]#
测试成功