Docker的自动构建镜像
类似ansible剧本,大小几kb
手动做镜像:大小几百M+
基础镜像信息 FROM centos:6.9
制作镜像操作指令 RUN yum install openssh-server -y
容器启动时执行指令 CMD ["/bin/bash"]
dockerfile常用指令:
FROM 这个镜像的妈妈是谁?(指定基础镜像)
MAINTAINER 告诉别人,谁负责养它?(指定维护者信息,可以没有)
LABLE 描述,标签
RUN 你想让它干啥(在命令前面加上RUN即可)
ADD 给它点创业资金(会自动解压tar) 制作docker基础的系统镜像
WORKDIR 我是cd,今天刚化了妆(设置当前工作目录)
VOLUME 给它一个存放行李的地方(设置卷,挂载主机目录)
EXPOSE 它要打开的门是啥(指定对外的端口)(-P 随机端口)
CMD 奔跑吧,兄弟!(指定容器启动后的要干的事情)(容易被替换)
dockerfile其他指令:
COPY 复制文件(不会解压)rootfs.tar.gz
ENV 环境变量
ENTRYPOINT 容器启动后执行的命令(无法被替换,启容器的时候指定的命令,会被当成参数)
官方dockerfile或者时速云镜像广场
[root@localhost centos6.9_ssh_nginx]# pwd /opt/dockerfile/centos6.9_ssh_nginx [root@localhost centos6.9_ssh_nginx]# cat dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx unzip RUN service sshd start RUN echo "123456"|passwd --stdin root RUN rm -rf /usr/share/nginx/html/index.html ADD xiaoniaofeifei.zip /usr/share/nginx/html/ RUN unzip /usr/share/nginx/html/xiaoniaofeifei.zip -d /usr/share/nginx/html/ ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# cat init.sh #!/bin/bash service sshd restart nginx -g 'daemon off;' 创建镜像 docker build --network=host -t centos6.9_nginx_ssh:v1 . [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v1 f01eeebf918a 9 minutes ago 431MB
优化上述dockerfile文件:
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx unzip RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html 这里指的是之后所有命令都在这个路径下面执行 RUN rm -rf * ADD xiaoniaofeifei.zip . 这里最后是点不能忘记了,表示在当前目录 RUN unzip xiaoniaofeifei.zip ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"]
执行创建案之后的命令结果是:
[root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v2 . Sending build context to Docker daemon 94.72kB Step 1/12 : FROM centos:6.9 ---> adf829198a7f Step 2/12 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/12 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/12 : RUN yum -y install openssh-server nginx unzip ---> Using cache ---> 66bb9530d868 Step 5/12 : RUN service sshd start ---> Using cache ---> 5974df83e839 Step 6/12 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> d8cbe019d081 Step 7/12 : WORKDIR /usr/share/nginx/html ---> Using cache ---> e4bf78b38489 Step 8/12 : RUN rm -rf * ---> Using cache ---> a7040fa17ab7 Step 9/12 : ADD xiaoniaofeifei.zip . ---> dcc79248a09f Step 10/12 : RUN unzip xiaoniaofeifei.zip ---> Running in cbca6d5ca592 Archive: xiaoniaofeifei.zip inflating: sound1.mp3 creating: img/ inflating: img/bg1.jpg inflating: img/bg2.jpg inflating: img/number1.png inflating: img/number2.png inflating: img/s1.png inflating: img/s2.png inflating: 21.js inflating: 2000.png inflating: icon.png inflating: index.html Removing intermediate container cbca6d5ca592 ---> f4a99a6a7128 Step 11/12 : ADD init.sh /init.sh ---> 455bf5fe6902 Step 12/12 : CMD ["/bin/bash","/init.sh"] ---> Running in e5caf5424a18 Removing intermediate container e5caf5424a18 ---> 6307e23ee16d Successfully built 6307e23ee16d Successfully tagged centos6.9_nginx_ssh:v2 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v2 6307e23ee16d About a minute ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 16 minutes ago 431MB [localhost centos6.9_ssh_nginx]# docker run -d -p 80:80 -p 2222:22 centos6.9_nginx_ssh:v2 277d3714590b9b9faaba1b682790c694bcc96f0868293d5505b69848f8d03421 [root@localhost centos6.9_ssh_nginx]# docker exec -it 277d37145 /bin/bash [root@277d3714590b html]# pwd /usr/share/nginx/html 从这里就可以看出目录由于设定了WORKDIR,所以钉死在了这个目录下面
在上面可以看出任然还有可以优化的地方,所以继续,使用ADD命令可以直接解压tar包
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD可以直接解压tar包 ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v3 . Sending build context to Docker daemon 186.9kB Step 1/11 : FROM centos:6.9 ---> adf829198a7f Step 2/11 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/11 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/11 : RUN yum -y install openssh-server nginx ---> Using cache ---> 16a434cf89be Step 5/11 : RUN service sshd start ---> Using cache ---> 60f03276a66c Step 6/11 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> b4dc176a311b Step 7/11 : WORKDIR /usr/share/nginx/html ---> Using cache ---> 209c253860e4 Step 8/11 : RUN rm -rf * ---> Using cache ---> d8ca4eff7ee9 Step 9/11 : ADD xiaoniaofeifei.tar.gz . ---> 139914b243cb Step 10/11 : ADD init.sh /init.sh ---> 482f2582dd02 Step 11/11 : CMD ["/bin/bash","/init.sh"] ---> Running in 66b1794dbe6e Removing intermediate container 66b1794dbe6e ---> 92b1087df3f5 Successfully built 92b1087df3f5 Successfully tagged centos6.9_nginx_ssh:v3 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v3 92b1087df3f5 About a minute ago 431MB centos6.9_nginx_ssh v2 6307e23ee16d 19 minutes ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 35 minutes ago 431MB
企业标准化EXPOSE
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD init.sh /init.sh EXPOSE 80/tcp 22/tcp CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v4 . Sending build context to Docker daemon 186.9kB Step 1/12 : FROM centos:6.9 ---> adf829198a7f Step 2/12 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/12 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/12 : RUN yum -y install openssh-server nginx ---> Using cache ---> 16a434cf89be Step 5/12 : RUN service sshd start ---> Using cache ---> 60f03276a66c Step 6/12 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> b4dc176a311b Step 7/12 : WORKDIR /usr/share/nginx/html ---> Using cache ---> 209c253860e4 Step 8/12 : RUN rm -rf * ---> Using cache ---> d8ca4eff7ee9 Step 9/12 : ADD xiaoniaofeifei.tar.gz . ---> Using cache ---> 139914b243cb Step 10/12 : ADD init.sh /init.sh ---> Using cache ---> 482f2582dd02 Step 11/12 : EXPOSE 80/tcp 22/tcp ---> Running in 4a9a9c270afc Removing intermediate container 4a9a9c270afc ---> b980eead4bdc Step 12/12 : CMD ["/bin/bash","/init.sh"] ---> Running in fc73bab98352 Removing intermediate container fc73bab98352 ---> ac9eca43049a Successfully built ac9eca43049a Successfully tagged centos6.9_nginx_ssh:v4 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v4 ac9eca43049a 45 seconds ago 431MB centos6.9_nginx_ssh v3 92b1087df3f5 11 minutes ago 431MB centos6.9_nginx_ssh v2 6307e23ee16d 29 minutes ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 45 minutes ago 431MB [root@localhost centos6.9_ssh_nginx]# docker run -d -P centos6.9_nginx_ssh:v4 697030125c669ee2e92ae09111833eec38cc00ba9ee367140de97ebcf95fbe00 [root@localhost centos6.9_ssh_nginx]# docker ps -al CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 697030125c66 centos6.9_nginx_ssh:v4 "/bin/bash /init.sh" 2 minutes ago Up 2 minutes 0.0.0.0:32769->22/tcp, 0.0.0.0:32768->80/tcp flamboyant_hoover
由上可知,加入了EXPOSE以后就能看到容器的端口出现随机映射的现象
volume的意义:
[root@localhost centos6.9_ssh_nginx]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD init.sh /init.sh EXPOSE 80/tcp 22/tcp VOLUME /usr/share/nginx/html CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker run -d -P centos6.9_nginx_ssh:v5 . [root@localhost centos6.9_ssh_nginx]# docker volume ls DRIVER VOLUME NAME local b56d4c1f5042c2f7c2e8afd4e53f4f6c0ac9a1c09d12c5dd81ab40c783e09f07 产生一个随机的卷
Docker的自动构建镜像的更多相关文章
- docker微服务部署之:五、利用DockerMaven插件自动构建镜像
docker微服务部署之:四.安装docker.docker中安装mysql和jdk1.8.手动构建镜像.部署项目 在上一篇文章中,我们是手动构建镜像,即: 4.1.2.5.1.2.6.1.2中的将d ...
- 使用Docker+Jenkins自动构建部署
环境 Windows 10 Docker Version 18.06.1-ce-win73 (19507) 运行jenkins 运行jenkins 容器 docker run -d --name ln ...
- Docker:dockerfile自动构建镜像 [六]
一.手动docker镜像的缺点 相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点: 1.dockerfile只有几kb,便于传输 2.使用dockerfile构建出来的镜 ...
- 【转】使用Docker+Jenkins自动构建部署
转载自 https://segmentfault.com/a/1190000012921606 环境 阿里云ESC,宿主机服务器安装Docker,在安全规则中确认8080端口开启. 客户端mac 运行 ...
- [Linux] 编写Dockerfile文件自动构建镜像
Dockerfile是一个文本文件,按顺序包含构建给定镜像所需的所有命令Docker通过读取Dockerfile中的指令自动构建图像 . Dockerfile遵循特定的格式和指令集,您可以在Docke ...
- Docker build Dockerfile 构建镜像 - 二
Dockerfile 制作镜像 https://hub.docker.com/ 搜索需要镜像: https://hub.docker.com/_/centos/ 官方示例: centos:6 1.这里 ...
- Docker镜像构建原理解析(不装docker也能构建镜像)
在devops流程里面 构建镜像是一个非常重要的过程,一般构建镜像是写dockerfile文件然后通过docker client来构建的image. docker client 会先检查本地有没有im ...
- Docker学习(7) 构建镜像
构建docker镜像 1 构建镜像的两种方式 1 通过容器构建镜像 2 通过Dockerfile构建镜像
- docker commit理解构建镜像(7)
镜像是多层存储,每一层是在前一层的基础上进行的修改: 而容器同样也是多层存储是在以镜像为基础层,在基础层上加一层作为容器运行时的存储层. 当我们使用Docker Hub的镜像无法满足我们的需求时,我们 ...
随机推荐
- 微信小程序2048开发进度(一)
父亲是个体劳动者,他的兴趣就是下象棋,针对平时兴趣,我想做一款自己的2048小游戏,在慕课网观看了2048小游戏的讲解,以及关于开发小游戏的理论知识,对开发一款小游戏有了基本的认识.
- vi——终端中的编辑器
vi--终端中的编辑器 目标 vi 简介 打开和新建文件 三种工作模式 常用命令 分屏命令 常用命令速查图 01. vi 简介 1.1 学习 vi 的目的 在工作中,要对 服务器 上的文件进行 简单 ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
- 量化投资_关于Multicharts砖型图(传统砖型图和非传统砖型图)最详细的解释_调用篇
1. 砖图形成数组后,我们再对他们复制到数组中然后再调用出来看一下. 2. 如下是累计只取20个数组大小为例,如果开始阶段数组长度组成初始的Array[20]的长度,然后这个数组的最后一个值填满后,进 ...
- docker安装文档
Docker离线安装以及本地yum源构建http://blog.csdn.net/joniers/article/details/64122820http://blog.csdn.net/wsscy2 ...
- dubbo使用Spring配置暴露服务和使用Spring配置引用远程服务
提供者: <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-servic ...
- maven仓库镜像、私服与jdk版本配置
--配置全局镜像,setting.xml <mirrors> <mirror> <id>alimaven</id> <name>aliyun ...
- vue中过滤器filter
Vue.js 允许我们自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插值表达式. v-bind表达式.过滤器应该被添加在 JavaScript 表达式的尾部,由 ...
- 物联网应用层协议选择和分析--MQTT、CoAP 、HTTP、XMPP、SoAP
MQTT协议 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)最早是IBM开发的一个即时通讯协议,MQTT协议是为大量计算能力有限且工作在低带宽.不 ...
- servletHomeWork
2. http全称是什么? 超文本传输协议(HTTP, HyperText Transfer Protocol)是互联网上应用为最广泛的一种网络协议. 3.http协议是无状态的协议是什么意思?请说明 ...