docker 鏡像的製作方法

一、docker image 的製作兩種方法:
 

方法1:docker commit     # 保持 container 的當前狀態到image後,然後生成對應的 image

方法2:docker build         # 使用dockerfile 文件自動化製作 image

方法1:docker commit

1.創建一個安裝好apache 工具的容器鏡像。

命令是:

[root@localhost]#docker run -it docker.io/centos:latest  /bin/bash              #運行docker 鏡像

[root@localhost]# yum install httpd                      # 在container 中安裝 apache 軟件包

[root@localhost]# exit   #退出容器鏡像

 

效果:下圖是沒有安裝apache 服務的鏡像

2.根據容器當前狀態做一個image 鏡像:創建一個安裝了apache 工具的centos 鏡像

語法:docker  commit <container 的ID>或者<image_name>

例如:{{

1.查看關閉的鏡像

[root@localhost]#docker ps -a

注意:目前在整個Linux的docker中只有一個centos的鏡像,上圖中所有的歷史打開的那個鏡像都是基於那個唯一的docker centos鏡像。

2.創建 鏡像

[root@localhost]# docker commit 2c5f2396d3f  docker.io/centos:latest

如下圖:

查看容器中的鏡像:

[root@localhost]# docker images

注意:

docker commit 2c5f2396d3f  docker.io/centos:latest 這個命令中latest也可以才成nginx,就成了docker commit 2c5f2396d3f  docker.io/centos:nginx ,這個是隨便修改的。如下圖:

3.使用 新創建的 docker.io/centos:nginx鏡像,生成一臺容器實例:

鏡像,生成一臺容器實例:

[root@localhost]# docker run -it docker.io/centos:nginx  /bin/bash

[root@localhost]# rpm -qa httpd #查看,已經安裝好apache命令

當返回:

httpd-2.4.6-67.el7.centos.2x.x86_64  #說明基於apache 鏡像的容器創建成功。

}}

方法2通過:docker build 創建一個基於centos的httpd web 服務器鏡像。

使用docker build 創建鏡像時,需要使用 dockerfile 文件自動化製作image 鏡像

注意:dockerfile 有點像源碼編譯時./configure後產生的 Makefile

 

1.創建 docker-build目錄

[root@localhost]# mkdir /docker -build

2.進入這個目錄

[root@localhost]# cd /docker-build

3.創建一個文件 Dockerfile

[root@localhost docker-build]# touch Dockerfile

注意:make自動化編譯時需要makefile文件,自動化創建docker 鏡像時,需要Dockerfile

4.編輯dockerfile

Dockerfile 用來創建一個自定義的image,包含了用戶指定的軟件依賴等。

[root@localhost]#vim Dockerfile

寫入的內容:

FROM docker.io/centos:latest               #我是基於那個鏡像來做的 

MAINTAINER    <[email protected]>             #作者是誰<鏡像創建者>

RUN yum  -y  install httpd                                  #做什麼《拿這個鏡像docker.io/centos:latest運行實例,然後執行yum命令,進行安裝》

ADD   start.sh  /usr/local/bin/start.sh            #把start.sh這個啓動腳本添加到/usr/local/bin這個文件下

ADD    index.html    /var/www/html/index.html         #把index.html這個啓動腳本添加到/var/www/html這個文件下

 

CMD   echo   hello word  #container 啓動時執行的命令或啓動的服務,但是一個dockerfile中只能有一條CMD命令,多條則只執行最後一條CMD

注意:ADD 將文件<src>拷貝到新產生的鏡像的文件系統對應的路徑<dest>,所有拷貝到新鏡像重的文件和文件夾權限爲0755,uld和gld爲0

{{

例如:dockerfile1 中的內容如下:

#vim dockerfile1

FROM Ubuntu

MAINTAINER  XXX

RUN echo hello 1 > test1.txt

RUN echo hello2 > /test2.txt

EXPOSE 80

EXPOSE 81

CMD ["/bin/bash"]

 

}}

5.啓動一個東西

[root@localhost docker- build]# echo "/usr/sbin/httpd -DFOREGROUND"  > start.sh     #在DFOREGROUND裏面啓動一個apache裏面用的東西->httpd   <相當於執行了 systemctl start httpd>

6.加載 一個權限

[root@localhost docker- build]# chmod a+x start.sh

7.創建一個index.html 文件

[root@localhost docker- build]# echo “docker image build test” > index.html

8.查看我們創建的文件有沒有    #非必敲項

[root@localhost docker- build]#  ls

9.使用build 來創建新的image

語法:docker build -t  父鏡像名:鏡像的tag    Dockerfile  文件所在的路徑

-t:表示tage,鏡像名

[root@localhost docker- build]# docker build  -t  docker.io/centos:httpd ./

10.使用docker images查看我們創建的鏡像

注意: ./表示當前目錄。另外你的當前目錄下要包含Dockerfile 

注意:什麼是docker鏡像?

{

docker 鏡像=應用/程序 + 庫

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章