dockerfile介绍
dockerfile是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
- 编写一个dokerfile文件
- docker build 构建一个镜像
- docker run 运行镜像
- docker push 发布镜像(dckerhub 、阿里云镜像仓库)
dockerfiel构建过程
基础知识:
- 每个保留关键字(指令)都必须是大写字母
- 执行从上到下顺序执行
- #表示注释
- 每个指令都会创建提交一个新的镜像层,并提交!
dockerfile指令
实战测试
创建一个自己的centos
1.编写dockerfile文件
[root@localhost dockerfile]# cat mydockerfile
FROM centos
MAINTAINER yyds<3069150475@qq.com>
ENV MYPATH /var/local
WORKDIR $MYPATH
RUN rm -rf /etc/yum.repos.d/* && curl -o /etc/yum.repos.d/CentOS-vault-8.5.2111.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
2.通过这个文件构建镜像
[root@localhost dockerfile]# docker build -f dockerfile -t mycentos .
查看代码
[+] Building 70.2s (6/8) docker:default
[+] Building 70.5s (6/8) docker:default
[+] Building 72.3s (6/8) docker:default
[+] Building 72.6s (6/8) docker:default
[+] Building 117.5s (9/9) FINISHED docker:default
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 449B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/5] FROM docker.io/library/centos:latest 0.0s
=> CACHED [2/5] WORKDIR /var/local 0.0s
=> [3/5] RUN rm -rf /etc/yum.repos.d/* && curl -o /etc/yum.repos 15.9s
=> [4/5] RUN yum -y install vim 79.6s
=> [5/5] RUN yum -y install net-tools 21.7s
=> exporting to image 0.3s
=> => exporting layers 0.3s
=> => writing image sha256:c7defb0511c187a891e8b504400827c6a54379 0.0s
=> => naming to docker.io/library/mycentos 0.0s
构建后的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos latest c7defb0511c1 3 minutes ago 312MB
centos latest 5d0da3dc9764 2 years ago 231MB
cmdtest latest 2b06f9641629 2 years ago 231MB
[root@localhost ~]# docker run -it mycentos
[root@b8511ea46079 local]# ls
[root@b8511ea46079 local]# pwd
/var/local
[root@b8511ea46079 local]# vim a
[1]+ Stopped vim a
[root@b8511ea46079 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看构建过程
[root@localhost dockerfile]# docker history c7defb0511c1
IMAGE CREATED CREATED BY SIZE COMMENT
c7defb0511c1 12 minutes ago CMD ["/bin/sh" "-c" "/bin/bash"] 0B buildkit.dockerfile.v0
<missing> 12 minutes ago CMD ["/bin/sh" "-c" "echo \"----end----\""] 0B buildkit.dockerfile.v0
<missing> 12 minutes ago CMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0
<missing> 12 minutes ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 12 minutes ago RUN /bin/sh -c yum -y install net-tools # bu… 14.7MB buildkit.dockerfile.v0
<missing> 12 minutes ago RUN /bin/sh -c yum -y install vim # buildkit 66.3MB buildkit.dockerfile.v0
<missing> 13 minutes ago RUN /bin/sh -c rm -rf /etc/yum.repos.d/* && … 2.5kB buildkit.dockerfile.v0
<missing> 8 hours ago WORKDIR /var/local 0B buildkit.dockerfile.v0
<missing> 8 hours ago ENV MYPATH=/var/local 0B buildkit.dockerfile.v0
<missing> 8 hours ago MAINTAINER yyds<3069150475@qq.com> 0B buildkit.dockerfile.v0
<missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
标签:ago,bin,0.0,buildkit,dockerfile1,docker,dockerfile From: https://www.cnblogs.com/muxinq/p/18223170