Dockerfile自动构建docker镜像
类似ansible剧本,大小几kb
手动做镜像:大小几百M+
dockerfile 支持自定义容器的初始命令
dockerfile主要组成部分:
 基础镜像信息       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
官方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的自动构建镜像的更多相关文章

  1. docker微服务部署之:五、利用DockerMaven插件自动构建镜像

    docker微服务部署之:四.安装docker.docker中安装mysql和jdk1.8.手动构建镜像.部署项目 在上一篇文章中,我们是手动构建镜像,即: 4.1.2.5.1.2.6.1.2中的将d ...

  2. 使用Docker+Jenkins自动构建部署

    环境 Windows 10 Docker Version 18.06.1-ce-win73 (19507) 运行jenkins 运行jenkins 容器 docker run -d --name ln ...

  3. Docker:dockerfile自动构建镜像 [六]

    一.手动docker镜像的缺点 相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点: 1.dockerfile只有几kb,便于传输 2.使用dockerfile构建出来的镜 ...

  4. 【转】使用Docker+Jenkins自动构建部署

    转载自 https://segmentfault.com/a/1190000012921606 环境 阿里云ESC,宿主机服务器安装Docker,在安全规则中确认8080端口开启. 客户端mac 运行 ...

  5. [Linux] 编写Dockerfile文件自动构建镜像

    Dockerfile是一个文本文件,按顺序包含构建给定镜像所需的所有命令Docker通过读取Dockerfile中的指令自动构建图像 . Dockerfile遵循特定的格式和指令集,您可以在Docke ...

  6. Docker build Dockerfile 构建镜像 - 二

    Dockerfile 制作镜像 https://hub.docker.com/ 搜索需要镜像: https://hub.docker.com/_/centos/ 官方示例: centos:6 1.这里 ...

  7. Docker镜像构建原理解析(不装docker也能构建镜像)

    在devops流程里面 构建镜像是一个非常重要的过程,一般构建镜像是写dockerfile文件然后通过docker client来构建的image. docker client 会先检查本地有没有im ...

  8. Docker学习(7) 构建镜像

    构建docker镜像 1 构建镜像的两种方式 1 通过容器构建镜像 2 通过Dockerfile构建镜像

  9. docker commit理解构建镜像(7)

    镜像是多层存储,每一层是在前一层的基础上进行的修改: 而容器同样也是多层存储是在以镜像为基础层,在基础层上加一层作为容器运行时的存储层. 当我们使用Docker Hub的镜像无法满足我们的需求时,我们 ...

随机推荐

  1. PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]

    题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...

  2. Linux-使用syslog记录调试信息

    1.有三个函数:openlog.syslog.closelog 2.一般的log信息都在操作系统的/var/log/messages这个文件中存储着,但是ubuntu中是在/var/log/syslo ...

  3. python-day3爬虫基础之下载网页

    今天主要学习了关于网页下载器的一些内容,下边做一下总结: 1.网页下载器,顾名思义,就是将URL所对应的网页以HTML的形式下载到本地,最终存储成本地文件或者还是本地内存字符串,然后进行后续的分析与处 ...

  4. JavaScript学习笔记 - 进阶篇(8)- DOM对象,控制HTML元素

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...

  5. Python程序中的进程操作--—--开启多进程

    Python程序中的进程操作-----开启多进程 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创 ...

  6. VMware12 + Ubuntu16.04 虚拟磁盘扩容

    转载自:https://blog.csdn.net/Timsley/article/details/50742755 今天用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间.经过百 ...

  7. ruoyi HttpUtils

    package com.ruoyi.common.utils.http; import java.io.BufferedReader; import java.io.IOException; impo ...

  8. 吴裕雄--天生自然TensorFlow高层封装:Keras-RNN

    # 1. 数据预处理. from keras.layers import LSTM from keras.datasets import imdb from keras.models import S ...

  9. [原]PInvoke导致栈破坏

    原, 总结, 调试, 调试案例  项目中遇到一个诡异的问题,程序在升级到.net4.6.1后会崩溃,提示访问只读内存区.大概现象如下: debug版不崩溃,release版稳定崩溃. 只有x64位的程 ...

  10. 基于Dijsktra算法的最短路径求解

    基于Dijsktra算法的最短路径求解   描述 一张地图包括n个城市,假设城市间有m条路径(有向图),每条路径的长度已知.给定地图的一个起点城市和终点城市,利用Dijsktra算法求出起点到终点之间 ...