当前位置:   article > 正文

Docker8_3:Docker volume容器数据卷:DockerFile文件挂载(一般都用这个)_dcokerfile挂载文件

dcokerfile挂载文件

Docker8_3:Docker volume容器数据卷:DockerFile文件挂载【一般都用这个】

什么是DockerFile

DockerFile就是用来构建dokcer镜像的构建文件,是命令脚本,通过这个脚本可以生成镜像,镜像是一层层的,脚本类似,是一个个命令组合的,每个命令是一层。

构建镜像的两种方式
1.搞一个容器改完之后,commit
2.使用DockerFile构建

DockerFile构建镜像(很常用)

为什么经常用DockerFile文件中挂载?

这是自动挂载,启动就会挂载,否则需要你启动镜像的时候,通过命令参数,-v手动挂载。

编写dockerfile文件

# 创建一个dockerfile文件,文件名建议使用dockerfile,这样命令可以不写,默认就是dockerfile
# 文件中的内容 指令(大写)
FROM centos

VOLUME ["volume1","volume2"]

CMD echo "-------end-------"

CMD /bin/bash

# docker镜像是分层的,每个命令就是一层
# VOLUME ["volume1","volume2"],这里测试用的是匿名挂载,等会找对应本机卷目录是字符串,一般都用具名挂载
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

docker build构建镜像

# docker build -f dockerfile文件路径(相对绝对都可以) -t haokai/centos:1.0 .
# -t 表示target,生成的镜像名:TAG
# 注意:后面的.别忽略
docker build -f dockerfile1 -t haokai/centos:1.0 .
  • 1
  • 2
  • 3
  • 4

输出:

[root@localhost volume]# cat dockerfile1
FROM centos

VOLUME ["volume1","volume2"]

CMD echo "-------end-------"

CMD /bin/bash
[root@localhost volume]# docker build -f dockerfile1 -t haokai/centos:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume1","volume2"]
 ---> Running in ee2bb7923a30
Removing intermediate container ee2bb7923a30
 ---> b63619ea74aa
Step 3/4 : CMD echo "-------end-------"
 ---> Running in f8b93d0ba281
Removing intermediate container f8b93d0ba281
 ---> 5dc5d67f480f
Step 4/4 : CMD /bin/bash
 ---> Running in 4fc8003865da
Removing intermediate container 4fc8003865da
 ---> 347c4f0aabd2
Successfully built 347c4f0aabd2
Successfully tagged haokai/centos:1.0
[root@localhost volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
haokai/centos         1.0       347c4f0aabd2   8 minutes ago   231MB
mysql                 5.7       c20987f18b13   2 weeks ago     448MB
tomcat                9.0       76206e3ba4b1   7 weeks ago     680MB
nginx                 latest    ea335eea17ab   7 weeks ago     141MB
centos                latest    5d0da3dc9764   3 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   9 months ago    79.1MB
elasticsearch         7.7.0     7ec4f35ab452   20 months ago   757MB
elasticsearch         7.6.2     f29a1ee41030   21 months ago   791MB
[root@localhost volume]# docker run -it 347c4f0aabd2
[root@8bb084df8cb4 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume1  volume2
[root@8bb084df8cb4 /]#

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

查看挂载目录

在这里插入图片描述

# 查看容器挂载情况
docker inspect 容器id
  • 1
  • 2
 "Mounts": [
            {
                "Type": "volume",
                "Name": "b9de58c7d586c587509df78fbf7029dda0ff450cbd12af640d4037f9a264a352",
                "Source": "/var/lib/docker/volumes/b9de58c7d586c587509df78fbf7029dda0ff450cbd12af640d4037f9a264a352/_data",
                "Destination": "volume1",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "75a18f597decfb6f2207899e0354acd176bf0df7dd42b40f02137d83ed9580cf",
                "Source": "/var/lib/docker/volumes/75a18f597decfb6f2207899e0354acd176bf0df7dd42b40f02137d83ed9580cf/_data",
                "Destination": "volume2",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/196044?site
推荐阅读
相关标签
  

闽ICP备14008679号