在本地建立一個容器後,可以依據這個容器建立本地映象,並可把這個映象推送到Docker hub中,以便在網路上下載使用。
檢視映象 [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB 建立一個名為myubuntu的容器 [root@docker-test1 ~]# docker run -ti --name myubuntu -d docker.io/ubuntu [root@docker-test1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 651a8541a47d docker.io/ubuntu "/bin/bash" 37 seconds ago Up 36 seconds myubuntu docker commit :從容器建立一個新的映象。 # docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] -a :提交的映象作者; -c :使用Dockerfile指令來建立映象; -m :提交時的說明文字; -p :在commit時,將容器暫停。 根據這個myubuntu容器提交映象 [root@docker-test1 ~]# docker commit -a "wangshibo" -m "this is test" 651a8541a47d myubuntu:v1 sha256:6ce4aedd12cda693d0bbb857cc443a56f9f569106e09ec61aa53563d0932ea4d 再次檢視映象,發現映象myubuntu:v1已經提交到本地了 [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu v1 6ce4aedd12cd 59 seconds ago 84.1 MB docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB 在mybuntu:v1映象推送到docker hub倉庫中去 # docker push [OPTIONS] NAME[:TAG] OPTIONS說明: --disable-content-trust :忽略映象的校驗,預設開啟 首先是登入docker hub (使用者名稱:wangshibo 密碼:*******) [root@docker-test1 ~]# 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 (wangshibo): wangshibo Password: Login Succeeded [root@docker-test1 ~]# docker push wangshibo/myubuntu:v1 The push refers to a repository [docker.io/wangshibo/myubuntu] An image does not exist locally with the tag: docker.io/wangshibo/myubuntu 這裡需要將ubuntu:v1映象改名,在名稱前加上自己的docker hub的Docker ID,即wangshibo [root@docker-test1 ~]# docker tag 6ce4aedd12cd wangshibo/myubuntu:v1 [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu v1 6ce4aedd12cd 6 minutes ago 84.1 MB wangshibo/myubuntu v1 6ce4aedd12cd 6 minutes ago 84.1 MB docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB 再次進行推送(注意:下面的v1的tag標籤可以不打,預設是latest)。推送操作時間稍微會有一點長,耐心等待~ [root@docker-test1 ~]# docker push wangshibo/myubuntu:v1 The push refers to a repository [docker.io/wangshibo/myubuntu] b5948ba9486d: Pushed 8d7ea83e3c62: Mounted from library/ubuntu 6a061ee02432: Mounted from library/ubuntu f73b2816c52a: Mounted from library/ubuntu 6267b420796f: Mounted from library/ubuntu a30b835850bf: Mounted from library/ubuntu v1: digest: sha256:e9cd9075d262848a307c92751e1a5890d883b814a31abd118161442461a1ca2d size: 1564 最後登入自己的Docker Hub,即https://hub.docker.com/ 登入後,在Repositories裡面就可以看到自己在上面推送的映象wangshibo/myubuntu:v1了,這是個對外的映象,可以在網路上下載。 在Docker hub上可以看到這個映象的下載命令(注意下載時跟上tag標籤,如果是latest的預設tag可以不跟) 也可以直接在Docker hub上刪除這個映象(Repositories-映象-Settings-delete) 比如在另一臺伺服器上下載這個映象 [root@kevin-test ~]# docker pull wangshibo/myubuntu Pulling repository wangshibo/myubuntu Repository not found 需要跟上tag標籤 [root@kevin-test ~]# docker pull wangshibo/myubuntu:v1 v1: Pulling from wangshibo/myubuntu 68e2a091ef24: Pull complete 8f9dd35f6299: Pull complete a81a6171600b: Pull complete a211a2bc7010: Pull complete 9e71a0b4f83a: Pull complete 0cf75bb335aa: Pull complete c393a882769e: Pull complete Digest: sha256:845fa3dcc9d0de1b9c701e1009918995da35a29012015f6c297a05edc489e018 Status: Downloaded newer image for wangshibo/myubuntu:v1 [root@kevin-test ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE wangshibo/myubuntu v1 c393a882769e 12 minutes ago 84.11 MB 在本機刪除這個映象 [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu v1 6ce4aedd12cd 15 minutes ago 84.1 MB wangshibo/myubuntu v1 6ce4aedd12cd 15 minutes ago 84.1 MB docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB 注意上面有兩個6ce4aedd12cd的映象ID,此時直接使用這個映象ID是刪除不掉的 [root@docker-test1 ~]# docker rmi 6ce4aedd12cd Error response from daemon: conflict: unable to delete 6ce4aedd12cd (must be forced) - image is referenced in multiple repositories 應該先刪除docker tag改名前的映象,使用映象名稱刪除。(一般在docker tag映象改名後,最好刪除改名前的映象) [root@docker-test1 ~]# docker rmi myubuntu:v1 Untagged: myubuntu:v1 Untagged: wangshibo/myubuntu@sha256:e9cd9075d262848a307c92751e1a5890d883b814a31abd118161442461a1ca2d [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE wangshibo/myubuntu v1 6ce4aedd12cd 15 minutes ago 84.1 MB docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB 這個時候就可以刪除映象ID刪除了 [root@docker-test1 ~]# docker rmi 6ce4aedd12cd Untagged: wangshibo/myubuntu:v1 Deleted: sha256:6ce4aedd12cda693d0bbb857cc443a56f9f569106e09ec61aa53563d0932ea4d Deleted: sha256:0ddeb6a16badd042914c2e72b9ef0331550c1cdcc4bdc6650a90cd9f57f1501b [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu 16.04 7aa3602ab41e 5 weeks ago 115 MB
=================================刪除映象失敗一例分享==================================
[root@docker-test1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker-test1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 打算刪除掉docker的nginx映象, 但是發現這個映象就是刪除不了,加了-f強制刪除也不行,一直報Error: No such image的錯誤, 最後終於找到了辦法直接刪除檔案! [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 62f816a209e6 7 days ago 109MB [root@docker-test1 ~]# docker rmi nginx Error: No such image: nginx [root@docker-test1 ~]# docker rmi 62f816a209e6 Error: No such image: 62f816a209e6 [root@docker-test1 ~]# docker rmi 62f816a209e6 -f Error: No such image: 62f816a209e6 如上,怎麼都刪除不掉nginx映象!!!!! 處理辦法: [root@docker-test1 ~]# systemctl stop docker [root@docker-test1 ~]# rm -rf /var/lib/docker rm: cannot remove ‘/var/lib/docker/containers’: Device or resource busy 刪除不了的原因是: 在建立容器的時候做了相應目錄的掛載,沒有解除安裝,所以Device or resource busy 解決辦法: 查詢掛載的目錄並解除安裝 [root@docker-test1 ~]# cat /proc/mounts | grep "docker" /dev/mapper/centos-root /var/lib/docker/containers xfs rw,relatime,attr2,inode64,noquota 0 0 proc /run/docker/netns/default proc rw,nosuid,nodev,noexec,relatime 0 0 proc /run/docker/netns/a0626c54fd03 proc rw,nosuid,nodev,noexec,relatime 0 0 proc /run/docker/netns/b18072de4224 proc rw,nosuid,nodev,noexec,relatime 0 0 proc /run/docker/netns/b5298f643455 proc rw,nosuid,nodev,noexec,relatime 0 0 proc /run/docker/netns/9f5e97637c98 proc rw,nosuid,nodev,noexec,relatime 0 0 [root@docker-test1 ~]# umount /var/lib/docker/containers [root@docker-test1 ~]# rm -rf /var/lib/docker [root@docker-test1 ~]# systemctl start docker 再次檢視就沒有這個映象了 [root@docker-test1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE