構建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 鏡像,是鏡像的描述文件。)mysql

FROM scratch  //從 0 開始構建。
COPY hello /    //    複製文件「hello」到鏡像的根目錄。
CMD ["/hello"]   //    容器啓動時,執行 /hello

docker中的base鏡像

一般指linux的各大發行版本的Doctor鏡像linux

FROM scratch   //從0構建
 ADD centos-7-docker.tar.xz /      //用戶空間,解壓後,就生成/bin、/dev等目錄
 CMD [「/bin/bash」]

Docker鏡像的擴展

Docker支持擴展示有鏡像,生成新的鏡像,大多數鏡像都是在base鏡像基礎上構建出來的。
示例:sql

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

構建鏡像

兩種方法:docker

  • docker commit
    1)交互模式運行容器 docker run -it image-name
    2)修改容器(安裝部署應用等)
    3)將容器保存爲新的鏡像 docker commit
    示例:apache

    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:文本文件,記錄鏡像的構建步驟
    編寫一個Dockerfileubuntu

    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鏡像的差別centos

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
相關文章
相關標籤/搜索