Dockerfile
dockerfile 是用来构建 docker 镜像的文件,命令和参数组成的脚本。
构建步骤
- 编写一个 dockerfile 文件。名字能够自定义,举荐“Dockerfile”
- docker build 构建镜像
- docker run 构建容器
- docker push 公布镜像(DockerHub 阿里云镜像仓库)
构建镜像过程
- 每个指令必须大写
- 执行由上到下
-
示意正文
- 每个指令都是一个新的镜像层
dockerfile 是面向开发人员的,docker 镜像成为企业交付规范。
dockerfile 常用命令
- FROM 根底镜像比方操作系统
- MAINTAINER 维护者信息姓名 email smallForest<<1032817724@qq.com>>
- RUN 镜像构建时运行的命令
- ADD 向镜像增加文件,会主动解压 tar
- WORKDIR 指定工作目录
- VOLUME 挂载目录
- EXPOSE 裸露端口
- CMD 容器启动时要运行的命令
- ENTRYPOINT 容器启动时要运行的命令,能够追加命令
- ONBUILD 当构建一个被继承的 docker,触发该命令
- COPY 相似 ADD 命令,将文件复制到镜像
- ENV 构建时设置环境变量
之前用他人的镜像,当前本人也能够写镜像。
实际出真知
本人做一个镜像
# 查看以后门路
➜ file pwd
/Users/dada/Downloads/docker-centos/file
➜ file ls
# 创立文件
➜ file touch myDockerfie
# 编辑文件
➜ file vim myDockerfie
# 查看编辑的内容
➜ file cat myDockerfie
FROM centos
MAINTAINER smallForest<1032817724@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
# 进行构建镜像,如果文件名字是 Dockerfile,则能够不必 -f 指定文件门路
➜ file docker build -f myDockerfie -t mycentos03:1.0 .
[+] Building 85.8s (8/8) FINISHED
=> [internal] load build definition from myDockerfie 0.0s
=> => transferring dockerfile: 255B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [1/4] FROM docker.io/library/centos 0.0s
=> CACHED [2/4] WORKDIR /usr/local 0.0s
=> [3/4] RUN yum -y install vim 73.6s
=> [4/4] RUN yum -y install net-tools 11.3s
=> exporting to image 0.7s
=> => exporting layers 0.7s
=> => writing image sha256:13fa3a8f9134f7b7b0e76e2b22ee68d07a09bc1e927365474a66fcc8a3666e6a 0.0s
=> => naming to docker.io/library/mycentos03:1.0
# 查看后果
➜ file docker images|grep mycentos03
mycentos03 1.0 13fa3a8f9134 59 seconds ago 282MB
➜ file docker run -d -it --name mycentos03 mycentos03:1.0
bf6575057cbf0517eeb4ec70d98ef4e232c4246174e4dc15e4f2c1cbd4d917db
➜ file docker ps|grep mycentos03
bf6575057cbf mycentos03:1.0 "/bin/sh -c /bin/bash" 13 seconds ago Up 12 seconds 80/tcp mycentos03
➜ file docker exec -it bf6575057cbf /bin/bash
[root@bf6575057cbf local]# ls
bin etc games include lib lib64 libexec sbin share src
# 查看工作目录和设置的 /usr/local 是否统一
[root@bf6575057cbf local]# pwd
/usr/local
[root@bf6575057cbf local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.6 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:06 txqueuelen 0 (Ethernet)
RX packets 9 bytes 726 (726.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
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@bf6575057cbf local]# vim a.sh
[root@bf6575057cbf local]#
# 运行后果发现,vim ifconfig 是能够应用的,因为 yum install 进行了装置
-
docker history 镜像 ID,能够查看镜像的构建历史
➜ file docker history 13fa3a8f9134 IMAGE CREATED CREATED BY SIZE COMMENT 13fa3a8f9134 8 minutes ago CMD ["/bin/sh" "-c" "/bin/bash"] 0B buildkit.dockerfile.v0 <missing> 8 minutes ago CMD ["/bin/sh" "-c" "echo \"----end----\""] 0B buildkit.dockerfile.v0 <missing> 8 minutes ago CMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0 <missing> 8 minutes ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0 <missing> 8 minutes ago RUN /bin/sh -c yum -y install net-tools # bu… 14.4MB buildkit.dockerfile.v0 <missing> 8 minutes ago RUN /bin/sh -c yum -y install vim # buildkit 58.1MB buildkit.dockerfile.v0 <missing> 39 hours ago WORKDIR /usr/local 0B buildkit.dockerfile.v0 <missing> 39 hours ago ENV MYPATH=/usr/local 0B buildkit.dockerfile.v0 <missing> 39 hours ago MAINTAINER smallForest<1032817724@qq.com> 0B buildkit.dockerfile.v0 <missing> 5 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 5 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB