[root@localhost docker-test-volume]# cat dockerfile
form centos
volume [“volume01”, “volume02”]
cmd echo “—–end—–“
cmd /bin/bash
# 构建
# 命令 docker build -f shell脚本文件 -t 镜像名:版本号
[root@localhost docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t centos:1.0 .
sending build context to docker daemon 2.048kb
step 1/4 : from centos
—> 0d120b6ccaa8
step 2/4 : volume [“volume01”, “volume02”]
—> running in 4e6de7bc2f15
removing intermediate container 4e6de7bc2f15
—> f9e48207902b
step 3/4 : cmd echo “—–end—–“
—> running in b22adea363e5
removing intermediate container b22adea363e5
—> a7518e2e1c72
step 4/4 : cmd /bin/bash
—> running in ae1b746bef6b
removing intermediate container ae1b746bef6b
—> d840628c30a9
successfully built d840628c30a9
successfully tagged centos:1.0
# 查看镜像
[root@localhost overlay2]# docker images
repository tag image id created size
centos 1.0 d840628c30a9 12 minutes ago 215mb # 我们自己生成的镜像
centos latest 0d120b6ccaa8 2 days ago 215mb
# 启动我们生成的镜像
[root@1af673cf9c88 /]# docker run -it d840628c30a9 /bin/bash
[root@1af673cf9c88 /]# ls -l
total 0
lrwxrwxrwx. 1 root root 7 may 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 aug 13 02:18 dev
drwxr-xr-x. 1 root root 66 aug 13 02:18 etc
drwxr-xr-x. 2 root root 6 may 11 2019 home
lrwxrwxrwx. 1 root root 7 may 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 may 11 2019 lib64 -> usr/lib64
drwx——. 2 root root 6 aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 may 11 2019 media
drwxr-xr-x. 2 root root 6 may 11 2019 mnt
drwxr-xr-x. 2 root root 6 may 11 2019 opt
dr-xr-xr-x. 117 root root 0 aug 13 02:18 proc
dr-xr-x—. 2 root root 162 aug 9 21:40 root
drwxr-xr-x. 11 root root 163 aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 may 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 may 11 2019 srv
dr-xr-xr-x. 13 root root 0 aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 aug 9 21:40 var
drwxr-xr-x. 2 root root 6 aug 13 02:18 volume01 # 这是我们生成镜像的时候自动挂载的数据卷目录
drwxr-xr-x. 2 root root 6 aug 13 02:18 volume02
# 这个卷和外部一定有一个同步的目录!我们来找一下
# 1、 根据容器id查询该容器的信息–》数据卷信息
# docker inspect 1af673cf9c88
# 2、 根据数据卷信息找到 volume01 对应的数据卷名称
# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
# 3、 根据数据卷名称 查询数据卷的信息–》找到linux中对应的目录
# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
# 在容器内部创建一个文件 在数据卷 volume01 中创建了test.java文件
[root@1af673cf9c88 volume01]# touch test.java
[root@1af673cf9c88 volume01]# ls
test.java
# 退出容器
[root@1af673cf9c88 volume01]# exit
exit
# 查看该容器的信息
[root@localhost overlay2]# docker inspect 1af673cf9c88
# 找到挂载卷 volume01 对应的的名字, 即:8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
“mounts”: [
{
“type”: “volume”,
“name”: “8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3”,
“source”: “/var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data”,
“destination”: “volume01”,
“driver”: “local”,
“mode”: “”,
“rw”: true,
“propagation”: “”
},
{
“type”: “volume”,
“name”: “046d0baa3cc0bc3540c5e7248808358371641bfba4e0bbd139c99fe851751da2”,
“source”: “/var/lib/docker/volumes/046d0baa3cc0bc3540c5e7248808358371641bfba4e0bbd139c99fe851751da2/_data”,
“destination”: “volume02”,
“driver”: “local”,
“mode”: “”,
“rw”: true,
“propagation”: “”
}
],
# 根据数据卷名字 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3 找到该卷在 linux 所在位置
[root@localhost overlay2]# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
[
{
“createdat”: “2020-08-13t10:27:12+08:00”,
“driver”: “local”,
“labels”: null,
“mountpoint”: “/var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data”,
“name”: “8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3”,
“options”: null,
“scope”: “local”
}
]
# 在/var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data/目录下找到我们的test.java文件
[root@localhost volumes]# cd /var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data/
[root@localhost _data]# ls
test.java