概念:
Dockerfile 就是用来构建镜像的文件,命令脚本。
通过这个脚本可以生成镜像,镜像是一层层的,脚本是一个个命令的,每个命令都是一层。
创建一个dockerfile文件dockerfile01,下面是文件里的内容:
FROM centos //以哪个为基础
VOLUME ["volume01","volume02"] //挂载的文件目录 匿名挂载
CMD echo "----end-------" //运行完成的输出
CMD /bin/bash //构建完成,默认走的路径
利用docker build 去构建dockerfile ,其中-f表示当前默认路径 ,-t 即tag 打个标签别名。
[root@iZuf6gomcpmv2fk1vu2w85Z docker-test-volumn]# docker build -f dockerfile01 -t yourname/centos .
[+] Building 0.1s (5/5) FINISHED
=> [internal] load build definition from dockerfile01 0.1s
=> => transferring dockerfile: 128B 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:7b294a0a516ad6c20f431fe0638dc00554d256e0624b1990b86ea889cdf058b6 0.0s
=> => naming to docker.io/yourname/centos 0.0s
[root@iZuf6gomcpmv2fk1vu2w85Z docker-test-volumn]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 17 months ago 231MB
yourname/centos latest 7b294a0a516a 17 months ago 231MB
ls-l以长格式的形式查看当前目录下所有可见文件的详细属性。其中rwx、r-x、r-x各有功能。
[root@9d716f01919a /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Mar 6 08:51 dev
drwxr-xr-x 1 root root 4096 Mar 6 08:51 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 132 root root 0 Mar 6 08:51 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 Mar 6 08:51 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 6 08:51 volume01 //下面这两个目录就是生成镜像时自动挂载的数据卷目录
drwxr-xr-x 2 root root 4096 Mar 6 08:51 volume02
这个卷和外部一定有一个同步目录,可以用docker inspect 容器ID查看一下,发现外部的目录都在/var/lib/docker/volumes/下面
"Mounts": [
{
"Type": "volume",
"Name": "e575f40c7cc57b130368e3f3e4deb5df96b69dc6ce72af0d35ae504f4d5b772d",
"Source": "/var/lib/docker/volumes/e575f40c7cc57b130368e3f3e4deb5df96b69dc6ce72af0d35ae504f4d5b772d/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "8a048439124f99fc8cbcfc2c854f3fa43afb1ac2a96f347f0b0257f5451f355d",
"Source": "/var/lib/docker/volumes/8a048439124f99fc8cbcfc2c854f3fa43afb1ac2a96f347f0b0257f5451f355d/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
所以我们在dockerfile里面的volume确实是挂载成功了,假设在构建镜像时没有挂在卷,就要手动挂载 -v卷名 :容器内路径。