容器提交镜像以及推送镜像

在本地创建一个容器后,可以依据这个容器创建本地镜像,并可把这个镜像推送到Docker hub中,以便在网络上下载使用

查看镜像
[root@client ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
ubuntu                                    16.04               005d2078bdfa        9 days ago          125MB
192.168.31.10:8080/kevin_bo/hello-world   v1                  bf756fb1ae65        4 months ago        13.3kB
 
创建一个名为myubuntu的容器
[root@client ~]# docker run -it --name myubuntu -d ubuntu:16.04 
ef4ca69f1419e1e1a74e4451a8cfded66d11849c36573a0089c7788995382140
[root@client ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef4ca69f1419        ubuntu:16.04        "/bin/bash"         23 seconds ago      Up 9 seconds                            myubuntu
 
 
docker commit :从容器创建一个新的镜像。
# docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
 
根据这个myubuntu容器提交镜像
[root@client ~]# docker commit -a "hanshuaiping" -m "this is test" ef4ca69f1419 myubuntu:v1
sha256:4b24ccf98a3a17b7dc8a4253fb9e57e4e2d11fbd2d63988174bcdca39c434d9c
 
再次查看镜像,发现镜像myubuntu:v1已经提交到本地了
[root@client ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
myubuntu                                  v1                  4b24ccf98a3a        5 seconds ago       125MB
ubuntu                                    16.04               005d2078bdfa        9 days ago          125MB
192.168.31.10:8080/kevin_bo/hello-world   v1                  bf756fb1ae65        4 months ago        13.3kB
 
在mybuntu:v1镜像推送到docker hub仓库中去
# docker push [OPTIONS] NAME[:TAG]
OPTIONS说明:
--disable-content-trust :忽略镜像的校验,默认开启
 
 
首先是登录docker hub (用户名:hanshuaiping    密码:*******)
[root@client ~]# 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: hanshuaiping
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@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里面就可以看到自己在上面推送的镜像hanshuaiping/myubuntu:v1了,这是个对外的镜像,可以在网络上下载。
在Docker hub上可以看到这个镜像的下载命令(注意下载时跟上tag标签,如果是latest的默认tag可以不跟)
也可以直接在Docker hub上删除这个镜像(Repositories-镜像-Settings-delete)

在这里插入图片描述

下载这个镜像
[root@client ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
hanshuaiping/myubuntu                     v1                  4b24ccf98a3a        19 minutes ago      125MB
myubuntu                                  v1                  4b24ccf98a3a        19 minutes ago      125MB
ubuntu                                    16.04               005d2078bdfa        9 days ago          125MB
192.168.31.10:8080/kevin_bo/hello-world   v1                  bf756fb1ae65        4 months ago        13.3kB
[root@client ~]# docker rmi hanshuaiping/myubuntu:v1
Untagged: hanshuaiping/myubuntu:v1
Untagged: hanshuaiping/myubuntu@sha256:675be105a0df1de53ee9d7866bf3522e1c496605f9cf142c75d2609e0e21415a
[root@client ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
myubuntu                                  v1                  4b24ccf98a3a        19 minutes ago      125MB
ubuntu                                    16.04               005d2078bdfa        9 days ago          125MB
192.168.31.10:8080/kevin_bo/hello-world   v1                  bf756fb1ae65        4 months ago        13.3kB
[root@client ~]# docker login
Authenticating with existing credentials...
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@client ~]# docker pull hanshuaiping/myubuntu:v1
v1: Pulling from hanshuaiping/myubuntu
Digest: sha256:675be105a0df1de53ee9d7866bf3522e1c496605f9cf142c75d2609e0e21415a
Status: Downloaded newer image for hanshuaiping/myubuntu:v1
[root@client ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
myubuntu                                  v1                  4b24ccf98a3a        21 minutes ago      125MB
hanshuaiping/myubuntu                     v1                  4b24ccf98a3a        21 minutes ago      125MB
ubuntu                                    16.04               005d2078bdfa        9 days ago          125MB
192.168.31.10:8080/kevin_bo/hello-world   v1                  bf756fb1ae65        4 months ago        13.3kB

=删除镜像失败一例分享==

[root@client ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@client ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 
打算删除掉docker的nginx镜像, 但是发现这个镜像就是删除不了,加了-f强制删除也不行,一直报Error: No such image的错误,
最后终于找到了办法直接删除文件!
 
[root@client ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              62f816a209e6        7 days ago          109MB
 
[root@client ~]# docker rmi nginx
Error: No such image: nginx
 
[root@client ~]# docker rmi 62f816a209e6
Error: No such image: 62f816a209e6
 
[root@client ~]# docker rmi 62f816a209e6 -f
Error: No such image: 62f816a209e6
 
如上,怎么都删除不掉nginx镜像!!!!!
 
处理办法:
[root@client ~]# systemctl stop docker
[root@client ~]# rm -rf /var/lib/docker
rm: cannot remove ‘/var/lib/docker/containers’: Device or resource busy
 
删除不了的原因是: 在建立容器的时候做了相应目录的挂载,没有卸载,所以Device or resource busy
 
解决办法: 查找挂载的目录并卸载
[root@client ~]# 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@client ~]# umount /var/lib/docker/containers
[root@client ~]# rm -rf /var/lib/docker
 
[root@client ~]# systemctl start docker
 
再次查看就没有这个镜像了
[root@client ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值