docker - 管理镜像

本地编译好镜像之后,需要分发给其它人使用构建镜像,docker提供了三种方式分发构建好的镜像:

    (1)、上传到公共 registry,例如docker hub、阿里云的docker镜像等

    (2)、导出tar镜像包,在其它host上导入镜像

    (3)、上传到私有库,例如docker registry 或者 Harbor 本文将分别介绍这几种分发镜像的方式。

本文将利用在 dockerfile构建一个简单的springboot应用镜像 中创建的镜像

1、使用公共Registry

1.1 使用docker hub

    docker hub是docker的官方仓库。在docker hub里,需要区分是哪个用户创建的镜像,镜像的格式为:[username]/镜像名:tag。前面创建了一个镜像名为springboot-docker的镜像,明显不符合镜像格式。所以,需要通过使用docker tag命令给原有镜像做一个别名 thinwonton/springboot-docker ,前面一段是在docker hub的用户名。

[root@localhost springboot-docker]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
springboot-docker              1.0                 de46a3f96d91        23 hours ago        121MB
thinwonton/springboot-docker   1.0                 de46a3f96d91        23 hours ago        121MB

 

首先需要在docker hub注册用户,然后利用login命令登录docker hub。thinwonton是我的账户

[root@localhost springboot-docker]# docker login -u thinwonton
Password: 
Login Succeeded

接下来,我们需要上传的镜像,需要修改为格式 [username]/镜像名:tag。 

使用push命令上传镜像

[root@localhost springboot-docker]# docker push thinwonton/springboot-docker:1.0
The push refers to repository [docker.io/thinwonton/springboot-docker]
e16d1e52c41b: Pushed 
0e09c652764f: Pushed 
ed6f0bd39121: Mounted from library/openjdk 
0c3170905795: Mounted from library/openjdk 
df64d3292fd6: Mounted from library/openjdk 
1.0: digest: sha256:384ce38ddb3c65e642f0f860ab4f54fba521118ee1aeaf35b427b5e61cfa2aee size: 1366

OK,上传完毕,到网站自己的仓库中看下

45467bf391c5623ee4862b28110a52597a9.jpg

这样子,就可以从docker hub上拉取镜像了。由于是公有镜像,所以人人都可以使用,要私有就得交钱罗

docker pull thinwonton/springboot-docker:1.0

1.2 使用阿里云

创建命名空间

链接:https://cr.console.aliyun.com/cn-shenzhen/namespaces
点击命名空间管理,创建命名空间。命名空间是一组仓库的集合,应以公司、组织或团队等命名,不建议使用系统名称进行命名。 

73a91af8ec8782fec4e3ac5aa95ebfd5318.jpg

这里创建了一个命名空间:thinwonton,设置为公有库,如果设置为私有库,可以通过授权可访问的用户

创建镜像仓库

链接:https://cr.console.aliyun.com/cn-shenzhen/repositories

点击镜像列表右上角的创建镜像仓库,一个镜像仓库就对应一个镜像。

和docker hub一样,阿里云的docker仓库也要区分用户的镜像,但是规则有少许不同,镜像名的规则在下面可以了解。

=========================================================================

5c82e199e2abf8cd36575bc003a37336c67.jpg

选择本地仓库

35a5f978b1937390fd1d7d162f81802a25e.jpg

创建完仓库后,可以在仓库的基本信息里面获取到仓库的公网地址和镜像仓库的操作步骤

4205fd79ed5b7ed09652d26790141c2240c.jpg

995b3ba23b228ed36e2a1f1e7710b82afe6.jpg

 

 

Ok,完成上面的步骤后,我们开始往这个镜像仓库推送上面的 thinwonton/springboot-docker:1.0 镜像

首先,需要用 docker login 命令登录阿里云,我账户的个人情况如下

sudo docker login --username=thinwonton registry.cn-shenzhen.aliyuncs.com

 

然后,给springboot-docker镜像建立别名

docker tag de46a3f96d91 registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker:1.0

看一下刚才创建的镜像别名

[root@localhost ~]# docker images
REPOSITORY                                                       TAG                 IMAGE ID            CREATED             SIZE
thinwonton/springboot-docker                                     1.0                 de46a3f96d91        2 days ago          121MB
registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker   1.0                 de46a3f96d91        2 days ago          121MB
springboot-docker                                                1.0                 de46a3f96d91        2 days ago          121MB

开始推送镜像

[root@localhost ~]# docker push registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker:1.0
The push refers to repository [registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker]
e16d1e52c41b: Pushed 
0e09c652764f: Pushed 
ed6f0bd39121: Pushed 
0c3170905795: Pushed 
df64d3292fd6: Pushed 
1.0: digest: sha256:384ce38ddb3c65e642f0f860ab4f54fba521118ee1aeaf35b427b5e61cfa2aee size: 1366

在页面上看下推送上来的镜像

9499ad4c25b9e3a35faa63d2cf6e6feecf7.jpg

从阿里云拉取刚才创建的镜像,拉取之前需要删除了本地的相关镜像。

[root@localhost ~]# docker pull registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker:1.0
1.0: Pulling from thinwonton/springboot-docker
4fe2ade4980c: Already exists 
6fc58a8d4ae4: Already exists 
ef87ded15917: Already exists 
8c74a3771fe2: Pull complete 
4030283f89db: Pull complete 
Digest: sha256:384ce38ddb3c65e642f0f860ab4f54fba521118ee1aeaf35b427b5e61cfa2aee
Status: Downloaded newer image for registry.cn-shenzhen.aliyuncs.com/thinwonton/springboot-docker:1.0

退出阿里云的登录

docker logout registry.cn-shenzhen.aliyuncs.com

2、使用导入/导出镜像的方式

我们可以导出镜像保存在文件上,然后拷贝镜像文件到需要安装该镜像的机器,然后利用导入命令把镜像导进本地docker仓库中

镜像导入命令

docker save -o /home/springboot-docker.tar springboot-docker:1.0

/home/springboot-docker.tar  是镜像导出路径在哪

springboot-docker:1.0  是本地镜像的名字和tag

 

镜像导入命令

docker load -i /home/springboot-docker.tar

 

3、使用私有registry

有时候公司内部建立的docker镜像只对公司内部人员开发,这时需要自己搭建私有镜像仓库。

开源界有名气的仓库有两个:docker registry 和 Harbor,由于我们公司使用Harbor搭建私有库,所以下面将介绍Harbor的使用方法。

请参考 安装企业级docker镜像仓库Harbor ,harbor的搭建和使用已经很详细

 

 

 

转载于:https://my.oschina.net/thinwonton/blog/2979274

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值