通過容器提交鏡像DockerCommit及推送鏡像DockerPush

在本地創建一個容器後,可以依據這個容器創建本地鏡像,並可把這個鏡像推送到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

以上就是通過容器提交鏡像DockerCommit及推送鏡像DockerPush的詳細內容,更多關於容器提交及推送鏡像的資料請關註WalkonNet其它相關文章!

推薦閱讀: