手動構建自己的docker容器鏡像實戰
前言
之前的實戰中,我們實戰中,我們使用的鏡像都是鏡像倉庫已有的鏡像。 已有的鏡像都是別人已經開發好上傳的。今天我們一起來看看如何構建自己的鏡像並上傳到鏡像倉庫中。
一. 設置docker構建鏡像的環境
- 使用docker commit命令進行鏡像的構建
1.1 commit 命令的介紹
我們執行 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 <[email protected]>") -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 ~]#
1.2 創建一個tomcat容器
- 拉取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的鏡像創建容器
[root@mufeng63 ~]# docker run --name mytomcat -d -p 8080:8080 tomcat 1f5bd6dcca0a614ec047bf3882e3fe2e4cdb17b7f343d7834145b17d1fc3c3c3 [root@mufeng63 ~]#
- 在Tomcat中部署一個簡單的頁面
如果不創建頁面,直接訪問Tomcat是會報錯的,所以這裡需要手動創建一個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 // 創建web測試目錄 root@1f5bd6dcca0a:/usr/local/tomcat# mkdir webapps/mufeng_demo //創建index.html root@1f5bd6dcca0a:/usr/local/tomcat# echo "<h1>This is mufenggrow </>" > webapps/mufeng_demo/index.html root@1f5bd6dcca0a:/usr/local/tomcat#
- 瀏覽器訪問測試:
使用ip+端口號/mufeng_demo/index.html進行訪問:
二. 開始構建自己的鏡像
2.1 查看容器的ID
先要確定自己要基於哪個容器生成鏡像, 這裡我們用Tomcat的容器來構建:
[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
2.2 使用命令docker commit 生成該容器的鏡像,並保存在本地
[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 ~]#
執行完成後,看到多瞭一個mufeng_tomcat 的鏡像。
三. 上傳鏡像到鏡像倉庫
3.1 登錄docker hub
docker hub的官網地址: https://hub.docker.com/
- 註冊:
- 創建存儲庫
你可以手動創建存儲庫,當然也可以不創建,我們這裡默認是不創建的,上傳的時候直接用mufenggrow/
即可
3.2 創建鏡像
如果要上傳到鏡像倉庫中,需要在鏡像名稱前加上倉庫的路徑信息:
[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
3.3 上傳到鏡像倉庫
- 使用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 ~]#
- 到網頁端查看
- 下載測試
先刪除系統中已經有的包:
[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 ~]#
可以看到,我們自己構建的鏡像,已經可以下載瞭。
以上就是手動構建自己的docker容器鏡像實戰的詳細內容,更多關於構建docker容器鏡像的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- 通過容器提交鏡像DockerCommit及推送鏡像DockerPush
- Docker images導出和導入操作
- 5分鐘教你docker安裝啟動redis全教程(全新方式)
- Docker鏡像發佈到Docker Hub的實現方法
- Docker Compose部署Nginx的方法步驟