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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。