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

Docker8_3:Docker容器数据卷: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"],这里测试用的是匿名挂载,等会找对应本机卷目录是字符串,一般都用具名挂载

docker build构建镜像

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

输出:

[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 /]#

查看挂载目录

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

# 查看容器挂载情况
docker inspect 容器id
 "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": ""
            }
        ],
上一篇:docker基础_Dockerfile


下一篇:Mini2440裸机开发之LCD编程