一、準(zhǔn)備工作
準(zhǔn)備一個用來制作鏡像的容器。這里我們使用centos安裝apache的容器。容器的制作方法如下
# 1.使用centos啟動一個交互式容器
docker run -it centos:latest /bin/bash
# 2.安裝apache
yum -y install httpd
# 3.退出容器
exit
操作步驟如下:
[root@localhost ~]# docker run -it centos:latest /bin/bash
[root@a554ba6ed056 /]# yum -y install httpd
Failed to set locale, defaulting to C.UTF-8
...
mod_http2-1.11.3-3.module_el8.2.0+307+4d18d695.x86_64
Complete!
[root@a554ba6ed056 /]# exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a554ba6ed056 centos:latest "/bin/bash" About a minute ago Exited (0) 33 seconds ago musing_wilson
這里能看到我們的容器ID為
a554ba6ed056,就是用該容器,制作一個自己的鏡像。
二、使用docker commit制作鏡像
語法如下:
docker commit <container的ID> <新的image_name>
示例如下:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a554ba6ed056 centos:latest "/bin/bash" About a minute ago Exited (0) 33 seconds ago musing_wilson
[root@localhost ~]# docker commit a554ba6ed056 centos:apache
sha256:9cb1f6b6242fd29032772b9507505ed6fc953fbc31adf90e550af93b07823eed
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos apache 9cb1f6b6242f 9 seconds ago 255 MB
docker.io/centos latest 831691599b88 6 weeks ago 215 MB
[root@localhost ~]# docker run -it centos:apache /bin/bash
[root@61d98f417e87 /]# rpm -qa httpd
httpd-2.4.37-21.module_el8.2.0+382+15b0afa8.x86_64
當(dāng)前的鏡像ID為
a554ba6ed056,新的鏡像的名稱為centos:apache。
鏡像創(chuàng)建成功后,進入鏡像內(nèi)部執(zhí)行rpm -qa httpd,能正確的查詢該軟件包。
三、使用docker build制作鏡像
使用docker build創(chuàng)建鏡像時,需要使用Dockerfile文件自動化制作鏡像。Dockerfile的執(zhí)行過程,很像源碼編譯時./configure后產(chǎn)生的Makefile。
-
創(chuàng)建工作目錄
# 1.創(chuàng)建并進入目錄 mkdir /docker-build && cd /docker-build # 2.創(chuàng)建自動化構(gòu)建文件 touch Dockerfilemake 自動化編譯時需要
Makefile文件,自動化創(chuàng)建鏡像時,也需要Dockerfile[root@localhost ~]# mkdir /docker-build && cd /docker-build [root@localhost docker-build]# touch Dockerfile [root@localhost docker-build]# ls -a . .. Dockerfile -
編輯
Dockerfile文件
在Dockerfile文件中編輯自己的自定義鏡像的操作,可以包含用戶指定軟件的依賴等。
使用vim將下列內(nèi)容寫入到Dockerfile文件中。FROM centos:latest MAINTAINER <hx@hxsen.com> RUN yum -y install httpd ADD start.sh /usr/local/bin/start.sh ADD template /var/www/html/ CMD /usr/local/bin/start.sh注釋:
關(guān)鍵詞 說明 FROM 基于哪個鏡像 MAINTAINER MAINTAINER鏡像創(chuàng)建者RUN yum -y install httpd安裝軟件用ADD 將文件[src]拷貝到新產(chǎn)生的鏡像的文件系統(tǒng)對應(yīng)的路徑[dest]。所有拷貝到新鏡像中的文件和文件夾權(quán)限為 0755,uid 和 gid 為 0 CMD docker實例啟動成功后,會執(zhí)行CMD后面的命令。所以CMD后面一般跟需要開機啟動的服務(wù)或腳本。一個Dockerfile中只能有一條CMD命令,多條則只執(zhí)行最后一條CMD -
創(chuàng)建
start.sh腳本啟動的httpd服務(wù)和apache默認(rèn)首頁index.html文件
設(shè)置腳本# 1. 設(shè)置啟動腳本 echo "/usr/sbin/httpd -DFOREGROUND" > start.sh # 2. 給啟動腳本添加運行權(quán)限 chmod a+x start.sh默認(rèn)首頁文件
# 創(chuàng)建模板目錄 mkdir template之后,將自己的
index.html文件放置到template文件夾里面,我的index.html內(nèi)容是hello world。 -
使用命令
build來創(chuàng)建新的image
語法如下docker build -t [父鏡像名]:[鏡像的tag] [Dockerfile文件所在路徑]-t:表示tag,鏡像名
實例
docker build -t centos:httpd ./[root@localhost docker-build]# docker build -t centos:httpd ./ Sending build context to Docker daemon 4.608 kB Step 1/6 : FROM centos:latest ---> 831691599b88 Step 2/6 : MAINTAINER <hx@hxsen.com> ---> Running in 01ae32a4e2fd ---> acf758c5234d Removing intermediate container 01ae32a4e2fd Step 3/6 : RUN yum -y install httpd ---> Running in d3ecdcc793d5 ... Step 4/6 : ADD start.sh /usr/local/bin/start.sh ---> 05d06ac82917 Removing intermediate container 4e5992822142 Step 5/6 : ADD template /var/www/html/ ---> b7b8b4efdc5b Removing intermediate container 9cf72dd07cf8 Step 6/6 : CMD /usr/local/bin/start.sh ---> Running in 17973309cca9 ---> 7e03de04b894 Removing intermediate container 17973309cca9 Successfully built 7e03de04b894 [root@localhost docker-build]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos httpd 7e03de04b894 4 minutes ago 255 MB docker.io/centos latest 831691599b88 6 weeks ago 215 MB執(zhí)行成功。內(nèi)容挺多的,只保留了關(guān)鍵的步驟。列出所有的鏡像后,能看到自己的
TAG為httpd的鏡像。
四、本地鏡像
- 保存鏡像到本地
保存鏡像到tar包,語法:
實例docker save -o [導(dǎo)出的鏡像名.tar] [本地鏡像名]:[鏡像標(biāo)簽]docker save -o docker-centos-httpd-image.tar centos:httpd
保存成功,能看到本地的tar包[root@localhost ~]# docker save -o docker-centos-httpd-image.tar centos:httpd [root@localhost ~]# ls -a . abc.txt .bash_history .bash_profile .cshrc docker-centos-httpd-image.tar .tcshrc .viminfo .. anaconda-ks.cfg .bash_logout .bashrc def.txt .pki test.txtdocker-centos-httpd-image.tar。 - 使用本地鏡像
語法:
實例docker load -i [本地tar包文件]
我這里使用的是同一個系統(tǒng)操作的。所以需要把之前的鏡像刪除,然后加載自己的本地鏡像。加載后,可以成功看到顯示了自己的加載后的鏡像。[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos httpd 7e03de04b894 20 minutes ago 255 MB docker.io/centos latest 831691599b88 6 weeks ago 215 MB [root@localhost ~]# docker rmi 7e03de04b894 Untagged: centos:httpd Deleted: sha256:7e03de04b894cff137074740df18ac967e681ce022636707890b2ad1e896364d ... [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 831691599b88 6 weeks ago 215 MB [root@localhost ~]# docker load -i docker-centos-httpd-image.tar d25caed0fdbe: Loading layer [==================================================>] 39.81 MB/39.81 MB 805e763ef330: Loading layer [==================================================>] 3.584 kB/3.584 kB b1f7a1e7f61a: Loading layer [==================================================>] 3.584 kB/3.584 kB Loaded image: centos:httpd [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos httpd 7e03de04b894 22 minutes ago 255 MB docker.io/centos latest 831691599b88 6 weeks ago 215 MB
五、發(fā)布鏡像
這里將鏡像發(fā)布到hub.docker.com,docker的官方倉庫。
- 需要到
https://hub.docker.com/注冊賬號。 - 使用命令行登錄到docker hub。
實例docker login -u houxin -p 123456[root@localhost ~]# docker login -u houxin -p 123456 Login Succeeded - 發(fā)布鏡像
如果直接這樣發(fā)布,命令行可能提示找不到本地鏡像。我們需要對我們發(fā)布的鏡像加標(biāo)簽,標(biāo)簽為docker push houxin/centos:httpdhouxin/centos:httpd。然后再發(fā)布就行了。
實例docker tag centos:httpd houxin/centos:httpd[root@localhost ~]# docker tag centos:httpd houxin/centos:httpd [root@localhost ~]# docker push houxin/centos:httpd The push refers to a repository [docker.io/houxin/centos:httpd] b1f7a1e7f61a: Pushed 805e763ef330: Pushed d25caed0fdbe: Pushed eb29745b8228: Mounted from library/centos latest: digest: sha256:6c17e3ace7397a56f023bbc322e404bd6a4aa44492e3f5dd9b8852f312e9348e size: 1155
能看到自己的[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE houxin/centos httpd 7e03de04b894 About an hour ago 255 MB centos-httpd latest 7e03de04b894 About an hour ago 255 MB centos httpd 7e03de04b894 About an hour ago 255 MB docker.io/centos latest 831691599b88 6 weeks ago 215 MBhouxin/centos下的TAG為httpd的鏡像。 - 下載鏡像
示例:docker pull houxin/centos:httpd[root@localhost ~]# docker pull houxin/centos:httpd Trying to pull repository docker.io/houxin/centos ... httpd: Pulling from docker.io/houxin/centos Digest: sha256:6c17e3ace7397a56f023bbc322e404bd6a4aa44492e3f5dd9b8852f312e9348e Status: Downloaded newer image for docker.io/houxin/centos:httpd