阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Registry搭建Docker私有仓库及验证

128次阅读
没有评论

共计 3408 个字符,预计需要花费 9 分钟才能阅读完成。

1. 关于Registry

官方的 Docker hub 是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去。但是,有时候,我们的使用场景需要我们拥有一个私有的镜像仓库用于管理我们自己的镜像。这个可以通过开源软件 Registry 来达成目的。

 Registrygithub 上有两份代码:老代码库和新代码库。老代码是采用 Python 编写的,存在 pullpush的性能问题,出到 0.9.1 版本之后就标志为 deprecated,不再继续开发。从2.0 版本开始就到在新代码库进行开发,新代码库是采用 go 语言编写,修改了镜像 id 的生成算法、registry上镜像的保存结构,大大优化了 pullpush镜像的效率。

 官方在 Docker hub 上提供了 registry 的镜像(详情),我们可以直接使用该 registry 镜像来构建一个容器,搭建我们自己的私有仓库服务。Taglatestregistry镜像是 0.9.1 版本的,我们直接采用 2.1.1 版本。

 2. Registry的部署

运行下面命令获取 registry 镜像,

$ sudo docker pull registry:2.1.1

然后启动一个容器,

$ sudo docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:2.1.1

Registry服务默认会将上传的镜像保存在容器的 /var/lib/registry,我们将主机的/opt/registry 目录挂载到该目录,即可实现将镜像保存到主机的 /opt/registry 目录了。

 运行 docker ps 看一下容器情况,

linuxidc@linuxidc~ $ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f3766397a458        registry:2.1.1      "/bin/registry /etc/d"   46 seconds ago      Up 45 seconds       0.0.0.0:5000->5000/tcp   registry

说明我们已经启动了 registry 服务,打开浏览器输入 http://127.0.0.1:5000/v2,出现下面情况说明registry 运行正常,

 使用 Registry 搭建 Docker 私有仓库及验证

3. 验证

现在我们通过将镜像 pushregistry来验证一下。

我的机器上有个 hello-world 的镜像,我们要通过 docker tag 将该镜像标志为要推送到私有仓库,

$ sudo docker tag hello-world 127.0.0.1:5000/hello-world

然后查看以下本地的镜像,

linuxidc@linuxidc~ $ sudo docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry                     2.1.1               b91f745cd233        5 days ago          220.1 MB
Ubuntu                       14.04               a5a467fddcb8        6 days ago          187.9 MB
hello-world                  latest              975b84d108f1        2 weeks ago         960 B
127.0.0.1:5000/hello-world   latest              975b84d108f1        2 weeks ago         960 B

接下来,我们运行 docker pushhello-world镜像 push 到我们的私有仓库中,

linuxidc@linuxidc~ $ sudo docker push 127.0.0.1:5000/hello-world
The push refers to a repository [127.0.0.1:5000/hello-world] (len: 1)
975b84d108f1: Image successfully pushed 
3f12c794407e: Image successfully pushed 
latest: digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b size: 2744

现在我们可以查看我们本地 /opt/registry 目录下已经有了刚推送上来的hello-world。我们也在浏览器中输入http://127.0.0.1:5000/v2/_catalog,如下图所示,

 使用 Registry 搭建 Docker 私有仓库及验证

现在我们可以先将我们本地的 127.0.0.1:5000/hello-world 和 hello-world 先删除掉,

$ sudo docker rmi hello-world
$ sudo docker rmi 127.0.0.1:5000/hello-world

然后使用 docker pull 从我们的私有仓库中获取 hello-world 镜像,

linuxidc@linuxidc~ $ sudo docker pull 127.0.0.1:5000/hello-world
Using default tag: latest
latest: Pulling from hello-world
b901d36b6f2f: Pull complete 
0a6ba66e537a: Pull complete 
Digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b
Status: Downloaded newer image for 127.0.0.1:5000/hello-world:latest
linuxidc@linuxidc~ $ sudo docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry                     2.1.1               b91f745cd233        5 days ago          220.1 MB
ubuntu                       14.04               a5a467fddcb8        6 days ago          187.9 MB
127.0.0.1:5000/hello-world   latest              0a6ba66e537a        2 weeks ago         960 B

4. 可能问题

可能会出现无法 push 镜像到私有仓库的问题。这是因为我们启动的 registry 服务不是安全可信赖的。这是我们需要修改 docker 的配置文件/etc/default/docker,添加下面的内容,

    DOCKER_OPTS=”–insecure-registry xxx.xxx.xxx.xxx:5000″

然后重启 docker 后台进程,

$ sudo service docker restart

这是再 push 即可。

(done)

更多 Docker 相关教程见以下内容

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm

Ubuntu 14.04 安装 Docker  http://www.linuxidc.com/linux/2014-08/105656.htm

Ubuntu 使用 VNC 运行基于 Docker 的桌面系统  http://www.linuxidc.com/Linux/2015-08/121170.htm

阿里云 CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm

Ubuntu 15.04 下安装 Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm

在 Ubuntu Trusty 14.04 (LTS) (64-bit)安装 Docker http://www.linuxidc.com/Linux/2014-10/108184.htm

在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm

Ubuntu 16.04 上 Docker 使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/141054.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3408字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中