Dockerfile是用来构建docker镜像的构建文件,可以理解为一个命令参数脚本!

构建步骤
1.编写一个 dockerfile 文件
2.docker build 构建镜像
3.docker run 运行镜像
4.docker push 发布镜像(dockerHub或者阿里云仓库)
Dockerfile构建过程

基础知识

1.每一个保留的关键字(指令)都是大写

2.指令从上到下执行

3.# 表示注释

4.每一个指令都会创建提交一个新的镜像,并提交!

dockerfile是面向研发的,发布项目做镜像,就需要编写dockerfile文件

Dockerfile指令
FROM                # 基础镜像,一切从这里开始
MAINTAINER          # 镜像作者(姓名+邮箱)
RUN                 # docker 镜像构建的时候运行的命令
ADD                 # 步骤:copy文件,会自动解压。(添加的内容)
WORKDIR             # 镜像的工作目录
VOLUME              # 挂载的目录位置
EXPOSE              # 暴露端口
CMD                 # 指定容器启动的时候运行的命令,只有最后一个命令会生效,可以被替代。
ENTRYPOINT          # 指定容器启动的时候运行的命令,可以追加命令
ONBUILD             # 当构建一个被继承的 dockerfile 这个时候就会运行一个 ONBUILD 的指令,触发指令
COPY                # 类似于DAA,将我们文件拷贝到镜像中
ENV                 # 构建的时候设置环境变量
​
CMD 和 ENTRYPOINT 的区别
====================================================================
# 测试 CMD命令
[root@localhost dockerfile]# cat dockerfile-cmd     # 创建一个dockerfile-cmd 做测试
FROM centos:6.6
CMD ["ls","-a"]
[root@localhost dockerfile]# docker build -f dockerfile-cmd -t cmdtest .
[root@localhost dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
cmdtest               latest    2b5cd3ddff46   6 years ago     203MB
[root@localhost dockerfile]# docker run cmdtest  # 启动容器会自动输出"ls","-a" 命令
.
..
.dockerenv
bin
dev
....
[root@localhost dockerfile]# docker run cmdtest -l
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
​
# CMD的清理 -l 替换了 ["ls","-a"] 命令,-l 不是命令,所以报错
​
[root@localhost dockerfile]# docker run cmdtest ls -al          # 追加 ls -al 就不会报错
total 20
drwxr-xr-x   1 root root    6 Oct 21 04:03 .
drwxr-xr-x   1 root root    6 Oct 21 04:03 ..
-rwxr-xr-x   1 root root    0 Oct 21 04:03 .dockerenv
...
​
====================================================================
# 测试 ENTRYPOINT 命令
​
# 创建dockerfile文件
[root@localhost dockerfile]# cat dockerfile-cmd-entrypoint 
FROM centos:6.6
ENTRYPOINT ["ls","-a"]
# 构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint .
# 运行镜像会执行 ["ls","-a"] 命令
[root@localhost dockerfile]# docker run entrypoint
.
..
.dockerenv
bin
dev
etc
home
....
​
# 追加命令 -l 不会报错
[root@localhost dockerfile]# docker run entrypoint -l
total 20
drwxr-xr-x   1 root root    6 Oct 21 04:12 .
drwxr-xr-x   1 root root    6 Oct 21 04:12 ..
-rwxr-xr-x   1 root root    0 Oct 21 04:12 .dockerenv
dr-xr-xr-x   2 root root 4096 Mar  4  2015 bin
drwxr-xr-x   5 root root  340 Oct 21 04:12 dev
drwxr-xr-x   1 root root   66 Oct 21 04:12 etc

创建一个dockerfile文件
# 创建一个dockerfile文件。
# 文件中内容指令大写 
[root@localhost docker-test-volume]# vim dockerfile1
>>>
FRoM centos:6.6
​
VOLUME ["volume01","volume02"]
​
CMD echo "------end-----"
CMD /bin/bash
>>>
[root@localhost docker-test-volume]# ls
dockerfile1
​
# VOLUME ["volume01","volume02"] 指定镜像中默认匿名挂载"volume01","volume02"
使用 docker buid 基于刚才的dockerfile文件构建一个新的镜像
# 构建镜像 命令后面一定有“.”
[root@localhost docker-test-volume]# docker build -f dockerfile1 -t xuxiake/centos:1.0 
[+] Building 0.2s (5/5) FINISHED                                                   docker:default
 => [internal] load build definition from dockerfile1                                        0.0s
 => => transferring dockerfile: 127B                                                         0.0s
 => [internal] load metadata for docker.io/library/centos:6.6                                0.0s
 => [internal] load .dockerignore                                                            0.0s
 => => transferring context: 2B                                                              0.0s
 => [1/1] FROM docker.io/library/centos:6.6                                                  0.0s
 => exporting to image                                                                       0.0s
 => => exporting layers                                                                      0.0s
 => => writing image sha256:4369a6ea581454854caa2681e4a602a6e66477198651e05f8a77c3239468b5c  0.0s
 => => naming to docker.io/xuxiake/centos:1.0                                                0.0s
查看构建的新镜像并运行起来
[root@localhost docker-test-volume]# docker images  # 查看镜像
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
xuxiake/centos        1.0       4369a6ea5814   6 years ago     203MB
[root@localhost docker-test-volume]# docker run -it 4369a6ea5814 /bin/bash
[root@f741eae3ebb3 /]# ll
total 20
dr-xr-xr-x   2 root root 4096 Mar  4  2015 bin
drwxr-xr-x   5 root root  360 Oct 20 04:34 dev
drwxr-xr-x   1 root root   66 Oct 20 04:34 etc
drwxr-xr-x   2 root root    6 Sep 23  2011 home
dr-xr-xr-x   7 root root   81 Mar  4  2015 lib
dr-xr-xr-x   6 root root 8192 Mar  4  2015 lib64
drwx------   2 root root    6 Mar  4  2015 lost+found
drwxr-xr-x   2 root root    6 Sep 23  2011 media
drwxr-xr-x   2 root root    6 Sep 23  2011 mnt
drwxr-xr-x   2 root root    6 Sep 23  2011 opt
dr-xr-xr-x 187 root root    0 Oct 20 04:34 proc
dr-xr-x---   2 root root   91 Mar  4  2015 root
dr-xr-xr-x   2 root root 4096 Mar  4  2015 sbin
drwxr-xr-x   3 root root   22 Mar  4  2015 selinux
drwxr-xr-x   2 root root    6 Sep 23  2011 srv
dr-xr-xr-x  13 root root    0 Oct 20 04:34 sys
drwxrwxrwt   2 root root    6 Mar  4  2015 tmp
drwxr-xr-x  13 root root  155 Mar  4  2015 usr
drwxr-xr-x  17 root root  197 Mar  4  2015 var
drwxr-xr-x   2 root root    6 Oct 20 04:34 volume01
drwxr-xr-x   2 root root    6 Oct 20 04:34 volume02
# 这里能看到刚才匿名挂载的卷 volume01 和 volume02

更多推荐