构建Docker镜像

最小的镜像hello-world,常用来验证docker是否安装成功

v2admin@ubuntu:~$ docker pull hello-world
v2admin@ubuntu:~$ docker images hello-world
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        5 months ago        1.84kB

hello-world的Dockerfile只有三条(Dockerfile 定义了如何构建 Docker 镜像,是镜像的描述文件。)

FROM scratch  //从 0 开始构建。
COPY hello /    //    复制文件“hello”到镜像的根目录。
CMD ["/hello"]   //    容器启动时,执行 /hello

docker中的base镜像

通常指linux的各大发行版本的Doctor镜像

 FROM scratch   //从0构建
 ADD centos-7-docker.tar.xz /      //用户空间,解压后,就生成/bin、/dev等目录
 CMD [“/bin/bash”]

Docker镜像的扩展

Docker支持扩展现有镜像,生成新的镜像,大多数镜像都是在base镜像基础上构建出来的。
示例:

  FROM ubuntu
  RUN apt install apache2
  RUN apt install mysql
  CMD ["/bin/bash"]

构建镜像

两种方法:

  • docker commit
    1)交互模式运行容器 docker run -it image-name
    2)修改容器(安装部署应用等)
    3)将容器保存为新的镜像 docker commit
    示例:

    v2admin@ubuntu:~$ docker run -it centos  //-it参数可以以交互模式进入容器,这样就可以对容器进行操作了
    [root@f3439e5b0092 /]# yum install httpd  //安装httpd服务
    //exit退出
    v2admin@ubuntu:~$ docker commit -m="install httpd" f3439e5b0092 centos-httpd  //生成新的镜像
     sha256:105e2960a30af973884e762f701fad52a608e9ce96246212ff6518ed220f5509
    v2admin@ubuntu:~$ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos-httpd        latest              105e2960a30a        21 seconds ago      339MB  //新生成的镜像
    ubuntu              latest              4c108a37151f        8 days ago          64.2MB
    httpd               latest              e77c77f17b46        2 weeks ago         140MB
    centos              latest              9f38484d220f        3 months ago       202MB
    
  • Dcokerfile:文本文件,记录镜像的构建步骤
    编写一个Dockerfile

    FROM ubuntu
    RUN apt update -y && apt install apache2 -y
    
    v2admin@ubuntu:~$ docker build -t ubuntu-httpd .   //构建镜像
    v2admin@ubuntu:~$ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu-httpd        latest              315b71d16eb4        About an hour ago   187MB
    centos-httpd        latest              105e2960a30a        4 hours ago         339MB
    ubuntu              latest              4c108a37151f        8 days ago          64.2MB
    

新生成的镜像与base镜像的差异

  v2admin@ubuntu:~$ docker history ubuntu  //查看镜像的构建历史
  IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
  4c108a37151f        8 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
  <missing>           8 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
  <missing>           8 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
  <missing>           8 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     987kB               
  <missing>           8 days ago          /bin/sh -c #(nop) ADD file:4e6b5d9ca371eb881…   63.2MB   
  v2admin@ubuntu:~$ docker history ubuntu-httpd
  IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
  315b71d16eb4        About an hour ago   /bin/sh -c apt update -y && apt install apac…   123MB      // 唯一的一条差异,在base镜像上多了一层         
  4c108a37151f        8 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
  <missing>           8 days ago          /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
  <missing>           8 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
  <missing>           8 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     987kB               
  <missing>           8 days ago          /bin/sh -c #(nop) ADD file:4e6b5d9ca371eb881…   63.2MB
posted @ 2019-07-04 23:14  leafgood  阅读(1022)  评论(0编辑  收藏  举报