您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么構(gòu)建自己的docker容器鏡像”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“怎么構(gòu)建自己的docker容器鏡像”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
使用docker commit命令進行鏡像的構(gòu)建
我們執(zhí)行 docker help commit來查看:
[root@mufeng63 ~]# docker help commit Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes Aliases: docker container commit, docker commit Options: -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") -c, --change list Apply Dockerfile instruction to the created image -m, --message string Commit message -p, --pause Pause container during commit (default true) [root@mufeng63 ~]#
拉取Tomcat的鏡像
[root@mufeng63 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@mufeng63 ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 0e29546d541c: Pull complete 9b829c73b52b: Pull complete cb5b7ae36172: Pull complete 6494e4811622: Pull complete 668f6fcc5fa5: Pull complete dc120c3e0290: Pull complete 8f7c0eebb7b1: Pull complete 77b694f83996: Pull complete 0f611256ec3a: Pull complete 4f25def12f23: Pull complete Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324 Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@mufeng63 ~]#
基于Tomcat的鏡像創(chuàng)建容器
[root@mufeng63 ~]# docker run --name mytomcat -d -p 8080:8080 tomcat 1f5bd6dcca0a614ec047bf3882e3fe2e4cdb17b7f343d7834145b17d1fc3c3c3 [root@mufeng63 ~]#
在Tomcat中部署一個簡單的頁面
如果不創(chuàng)建頁面,直接訪問Tomcat是會報錯的,所以這里需要手動創(chuàng)建一個web測試頁面:
//查看正在運行的容器 [root@mufeng63 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5bd6dcca0a tomcat "catalina.sh run" 53 seconds ago Up 52 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp mytomcat // 登錄Tomcat容器 [root@mufeng63 ~]# docker exec -it 1f5bd6dcca0a /bin/bash root@1f5bd6dcca0a:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work // 創(chuàng)建web測試目錄 root@1f5bd6dcca0a:/usr/local/tomcat# mkdir webapps/mufeng_demo //創(chuàng)建index.html root@1f5bd6dcca0a:/usr/local/tomcat# echo "<h2>This is mufenggrow </>" > webapps/mufeng_demo/index.html root@1f5bd6dcca0a:/usr/local/tomcat#
瀏覽器訪問測試:
使用ip+端口號/mufeng_demo/index.html進行訪問:
先要確定自己要基于哪個容器生成鏡像, 這里我們用Tomcat的容器來構(gòu)建:
[root@mufeng63 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5bd6dcca0a tomcat "catalina.sh run" 19 minutes ago Up 19 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp mytomcat [root@mufeng63 ~]#
可以確定該容器的ID是1f5bd6dcca0a
[root@mufeng63 ~]# docker commit 1f5bd6dcca0a mufeng_tomcat sha256:3dbdf1f915ac9cc3210fb0e77d9f5d93f24064ac604cf12d24f602693396eed3 [root@mufeng63 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mufeng_tomcat latest 3dbdf1f915ac 9 seconds ago 680MB tomcat latest fb5657adc892 15 months ago 680MB [root@mufeng63 ~]#
執(zhí)行完成后,看到多了一個mufeng_tomcat 的鏡像。
docker hub的官網(wǎng)地址: https://hub.docker.com/
注冊:
創(chuàng)建存儲庫
你可以手動創(chuàng)建存儲庫,當然也可以不創(chuàng)建,我們這里默認是不創(chuàng)建的,上傳的時候直接用mufenggrow/
即可
如果要上傳到鏡像倉庫中,需要在鏡像名稱前加上倉庫的路徑信息:
[root@mufeng63 ~]# docker commit 1f5bd6dcca0a mufenggrow/mufeng_tomcat sha256:dc9420341e4bd06cdd7dc5881220481c9dbe8faf4d548356c6dbab265a36416b [root@mufeng63 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mufenggrow/mufeng_tomcat latest dc9420341e4b 5 seconds ago 680MB mufeng_tomcat latest 3dbdf1f915ac 16 minutes ago 680MB tomcat latest fb5657adc892 15 months ago 680MB
使用docker login 登錄
[root@mufeng63 ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: mufenggrow Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@mufeng63 ~]#
開始上傳
[root@mufeng63 ~]# docker push mufenggrow/mufeng_tomcat Using default tag: latest The push refers to repository [docker.io/mufenggrow/mufeng_tomcat] 66f50273e519: Pushed 3e2ed6847c7a: Mounted from library/tomcat bd2befca2f7e: Mounted from library/tomcat 59c516e5b6fa: Mounted from library/tomcat 3bb5258f46d2: Mounted from library/tomcat 832e177bb500: Mounted from library/tomcat f9e18e59a565: Mounted from library/tomcat 26a504e63be4: Mounted from library/tomcat 8bf42db0de72: Mounted from library/tomcat 31892cc314cb: Mounted from library/tomcat 11936051f93b: Mounted from library/tomcat latest: digest: sha256:4a696362a8586173d7f0f9b12eef1354060d10746796e59519ed083fe0acd5f8 size: 2630 [root@mufeng63 ~]#
到網(wǎng)頁端查看
下載測試
先刪除系統(tǒng)中已經(jīng)有的包:
[root@mufeng63 ~]# docker rmi mufenggrow/mufeng_tomcat:latest Untagged: mufenggrow/mufeng_tomcat:latest Untagged: mufenggrow/mufeng_tomcat@sha256:4a696362a8586173d7f0f9b12eef1354060d10746796e59519ed083fe0acd5f8 Deleted: sha256:dc9420341e4bd06cdd7dc5881220481c9dbe8faf4d548356c6dbab265a36416b Deleted: sha256:6d5bd3130a0c60a6075e5856fa11c8acfdab7a2252360a6fbeabdef1f83fc526 [root@mufeng63 ~]# docker rmi mufeng_tomcat Untagged: mufeng_tomcat:latest Deleted: sha256:3dbdf1f915ac9cc3210fb0e77d9f5d93f24064ac604cf12d24f602693396eed3 Deleted: sha256:ea120c9388c452a6f215f1761b5981e40370e74b68d1886237e0931ce33a45f3 [root@mufeng63 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest fb5657adc892 15 months ago 680MB [root@mufeng63 ~]#
下載鏡像包
[root@mufeng63 ~]# docker search mufenggrow/mufeng_tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED mufenggrow/mufeng_tomcat 0 [root@mufeng63 ~]# docker pull mufenggrow/mufeng_tomcat Using default tag: latest latest: Pulling from mufenggrow/mufeng_tomcat 0e29546d541c: Already exists 9b829c73b52b: Already exists cb5b7ae36172: Already exists 6494e4811622: Already exists 668f6fcc5fa5: Already exists dc120c3e0290: Already exists 8f7c0eebb7b1: Already exists 77b694f83996: Already exists 0f611256ec3a: Already exists 4f25def12f23: Already exists 5a8cdbad1b10: Pull complete Digest: sha256:4a696362a8586173d7f0f9b12eef1354060d10746796e59519ed083fe0acd5f8 Status: Downloaded newer image for mufenggrow/mufeng_tomcat:latest docker.io/mufenggrow/mufeng_tomcat:latest [root@mufeng63 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mufenggrow/mufeng_tomcat latest dc9420341e4b 24 minutes ago 680MB tomcat latest fb5657adc892 15 months ago 680MB [root@mufeng63 ~]#
讀到這里,這篇“怎么構(gòu)建自己的docker容器鏡像”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。