dockerfile就是用来构建docker镜像的构建文件!命令脚本!先体验一下!通过这个脚本可以生成镜像,镜像是一层一层,脚本一个个的命令,每个命令都是一层!
#创建一个dockerfile文件,名字可以随机,建议dockerfile
#文件中的内容 指令(大写) 参数
[root@localhost home]# mkdir docker-test-volume
[root@localhost home]# ls
docker-test-volume
[root@localhost home]# vim dockerfile1
[root@localhost home]# ls
dockerfile1 docker-test-volume
[root@localhost docker-test-volume]# vim dockerfile1
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
#这里的每个命令就是镜像的一层!
这里为匿名挂载。
生成
[root@localhost docker-test-volume]# docker build -f /home/docker-test-volume/dockhome/docker-test-volume/dockerfile1 -t yyds/centos .
[+] Building 27.7s (5/5) FINISHED docker:default
=> [internal] load build definition from docke 0.0s
=> => transferring dockerfile: 180B 0.0s
=> [internal] load metadata for docker.io/lib 21.3s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/1] FROM docker.io/library/centos:latest@ 6.3s
=> => resolve docker.io/library/centos:latest@ 0.0s
=> => sha256:a27fd8080b517143cbbba 762B / 762B 0.0s
=> => sha256:a1801b843b1bfaf77c501 529B / 529B 0.0s
=> => sha256:5d0da3dc976460b72 2.14kB / 2.14kB 0.0s
=> => sha256:a1d0c7532777641 83.52MB / 83.52MB 2.0s
=> => extracting sha256:a1d0c75327776413fa0db9 4.1s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:88a327c6cfeef6087c8 0.0s
=> => naming to docker.io/yyds/centos 0.0s
[root@localhost docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 years ago 141MB
tomcat latest fb5657adc892 2 years ago 680MB
mysql latest 3218b38490ce 2 years ago 516MB
yyds/centos latest 88a327c6cfee 2 years ago 231MB
运行
[root@localhost docker-test-volume]# docker run -it 88a327c6cfee /bin/bash
查看
[root@a265576654eb /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp volume01
etc lib64 mnt root srv usr volume02
[root@a265576654eb /]# ls -l
total 4
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 360 May 29 12:56 dev
drwxr-xr-x. 1 root root 66 May 29 12:56 etc
drwxr-xr-x. 2 root root 6 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 6 Sep 15 2021 lost+found
drwxr-xr-x. 2 root root 6 Nov 3 2020 media
drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x. 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x. 338 root root 0 May 29 12:56 proc
dr-xr-x---. 2 root root 162 Sep 15 2021 root
drwxr-xr-x. 11 root root 163 Sep 15 2021 run
lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x. 13 root root 0 May 28 00:39 sys
drwxrwxrwt. 7 root root 171 Sep 15 2021 tmp
drwxr-xr-x. 12 root root 144 Sep 15 2021 usr
drwxr-xr-x. 20 root root 4096 Sep 15 2021 var
drwxr-xr-x. 2 root root 6 May 29 12:56 volume01
drwxr-xr-x. 2 root root 6 May 29 12:56 volume02
volume01、volume02就是生成镜像时自动挂载的,数据卷目录。
这个卷和外部有同步的目录!
进入volume01并创建一个文件
[root@a265576654eb /]# cd volume01
[root@a265576654eb volume01]# ls
[root@a265576654eb volume01]# touch a.txt
[root@a265576654eb volume01]# ls
a.txt
查看挂载卷的路径
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 years ago 141MB
tomcat latest fb5657adc892 2 years ago 680MB
mysql latest 3218b38490ce 2 years ago 516MB
yyds/centos latest 88a327c6cfee 2 years ago 231MB
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a265576654eb 88a327c6cfee "/bin/bash" 10 minutes ago Up 9 minutes cool_khayyam
29ce6c98f7d7 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:32772->80/tcp, :::32772->80/tcp nginx101
61610fa8beb6 tomcat "catalina.sh run" 3 hours ago Up 3 hours 0.0.0.0:32771->8080/tcp, :::32771->8080/tcp tomcat1
2810762e1a50 nginx "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:32770->80/tcp, :::32770->80/tcp nginx10
5ed947898abd nginx "/docker-entrypoint.…" 3 hours ago Up 3 hours 0.0.0.0:32769->80/tcp, :::32769->80/tcp nginx01
[root@localhost ~]# docker inspect a265576654eb
[
"Mounts": [
{
"Type": "volume",
"Name": "ac907dd1bc239d5b88725dfb9337617d0710fef66079283e6fb9f4b76e81c5e4",
"Source": "/var/lib/docker/volumes/ac907dd1bc239d5b88725dfb9337617d0710fef66079283e6fb9f4b76e81c5e4/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "a63f236eae94b62e3a9c281e69c07af5f022369d94ec866e8051a8775af7b7ce",
"Source": "/var/lib/docker/volumes/a63f236eae94b62e3a9c281e69c07af5f022369d94ec866e8051a8775af7b7ce/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
]
测试查看同步目录下的文件
[root@localhost ~]# cd /var/lib/docker/volumes/ac907dd1bc239d5b88725dfb9337617d0710fef66079283e6fb9f4b76e81c5e4/_data
[root@localhost _data]# ls
a.txt
标签:ago,dockerfile,初识,0.0,xr,docker,root,localhost From: https://www.cnblogs.com/muxinq/p/18221134