初识dockerfile

dockerfile是什么

dockerfile就是用来构建docker镜像的构建文件,命令脚本的集合

  • 编写dockerfile
[root@localhost home]# cat dockerfile 
FROM nginx # 镜像基于什么

VOLUME ["nginx_volume01", "nginx_volume02"] # 镜像挂载在哪里

CMD echo "-----end-----" #命令输出

CMD /bin/bash #最后进入的窗口
  • 通过dockerfile生成镜像
docker bulid -f dockerfile路径 -t 镜像名字:[tag] 镜像存储路径
# 命令
docker build -f dockerfile -t moon/nginx:1.0 .

# 结果
[root@localhost home]# docker build -f ./dockerfile -t moon/nginx:1.0 .
Sending build context to Docker daemon  219.8MB
Step 1/4 : FROM nginx
 ---> 605c77e624dd
Step 2/4 : VOLUME ["nginx_volume01", "nginx_volume02"]
 ---> Running in bc2809e221d5
Removing intermediate container bc2809e221d5
 ---> 0746793b6c56
Step 3/4 : CMD echo "-----end-----"
 ---> Running in 0466a49fc3ee
Removing intermediate container 0466a49fc3ee
 ---> 4e6c33c9f949
Step 4/4 : CMD /bin/bash
 ---> Running in f5b3ea441d02
Removing intermediate container f5b3ea441d02
 ---> 88a1daa0c261
Successfully built 88a1daa0c261
Successfully tagged moon/nginx:1.0

# 查看当前镜像
[root@localhost home]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED       STATUS       PORTS                                       NAMES
72e98c45be08   nginx                 "/docker-entrypoint.…"   3 hours ago   Up 3 hours   0.0.0.0:49155->80/tcp, :::49155->80/tcp     Nginx03
646d3474fd0c   nginx                 "/docker-entrypoint.…"   3 hours ago   Up 3 hours   0.0.0.0:49154->80/tcp, :::49154->80/tcp     Nginx02
f2ebd20fdd14   nginx                 "/docker-entrypoint.…"   3 hours ago   Up 3 hours   0.0.0.0:49153->80/tcp, :::49153->80/tcp     Nginx01
4bab1ceee43d   portainer/portainer   "/portainer"             7 days ago    Up 3 hours   0.0.0.0:8088->9000/tcp, :::8088->9000/tcp   zen_noyce
[root@localhost home]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
moon/nginx            1.0       88a1daa0c261   5 minutes ago   141MB
tomcat02              1.0       05300a7ac60d   6 days ago      684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                9.0       b8e65a4d736d   4 weeks ago     680MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
mysql                 5.7       c20987f18b13   4 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB

# 启动我们自己创建的镜像
[root@localhost home]# docker run -it --name Nginx04 88a1daa0c261
root@efd2f5680872:/# ls -al
total 12
drwxr-xr-x   1 root root   50 Jan 23 15:16 .
drwxr-xr-x   1 root root   50 Jan 23 15:16 ..
-rwxr-xr-x   1 root root    0 Jan 23 15:16 .dockerenv
drwxr-xr-x   2 root root 4096 Dec 20 00:00 bin
drwxr-xr-x   2 root root    6 Dec 11 17:25 boot
drwxr-xr-x   5 root root  360 Jan 23 15:16 dev
drwxr-xr-x   1 root root   41 Dec 29 19:28 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Dec 29 19:28 docker-entrypoint.sh
drwxr-xr-x   1 root root   66 Jan 23 15:16 etc
drwxr-xr-x   2 root root    6 Dec 11 17:25 home
drwxr-xr-x   1 root root   45 Dec 20 00:00 lib
drwxr-xr-x   2 root root   34 Dec 20 00:00 lib64
drwxr-xr-x   2 root root    6 Dec 20 00:00 media
drwxr-xr-x   2 root root    6 Dec 20 00:00 mnt
drwxr-xr-x   2 root root    6 Jan 23 15:16 nginx_volume01  #我们在dockerfile中挂载的目录
drwxr-xr-x   2 root root    6 Jan 23 15:16 nginx_volume02  #我们在dockerfile中挂载的目录
drwxr-xr-x   2 root root    6 Dec 20 00:00 opt
dr-xr-xr-x 133 root root    0 Jan 23 15:16 proc
drwx------   2 root root   37 Dec 20 00:00 root
drwxr-xr-x   3 root root   30 Dec 20 00:00 run
drwxr-xr-x   2 root root 4096 Dec 20 00:00 sbin
drwxr-xr-x   2 root root    6 Dec 20 00:00 srv
dr-xr-xr-x  13 root root    0 Jan 23 15:16 sys
drwxrwxrwt   1 root root    6 Dec 29 19:28 tmp
drwxr-xr-x   1 root root   66 Dec 20 00:00 usr
drwxr-xr-x   1 root root   41 Dec 20 00:00 var
root@efd2f5680872:/# 

# 查看当前通过我们自己创建的镜像运行的容器的挂载情况
root@efd2f5680872:/# [root@localhost home]# 
[root@localhost home]# docker inspect Nginx04
......
"Mounts": [
    {
        "Type": "volume",
        "Name": "b28d3de47491d81c1d88cdb25a2fc311baf53ef6b9a6d28142ba0bb51d70c484",
        "Source": "/var/lib/docker/volumes/b28d3de47491d81c1d88cdb25a2fc311baf53ef6b9a6d28142ba0bb51d70c484/_data",
        "Destination": "nginx_volume01",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "volume",
        "Name": "70f6050ee7b25fa93c58708eb032cf487d710a518f20c3330591c1e70c5a71c8",
        "Source": "/var/lib/docker/volumes/70f6050ee7b25fa93c58708eb032cf487d710a518f20c3330591c1e70c5a71c8/_data",
        "Destination": "nginx_volume02",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
......
# 我们可以看见这里的匿名挂载已经成功,毕竟我们在dockerfile中写的,也只是匿名挂载而已
posted @ 2022-01-23 23:22  影梦无痕  阅读(82)  评论(0)    收藏  举报