Docker Registry搭建私有仓库
一.简单介绍
在使用docker镜像的一些场景中,我们需要定制和封装自己的操作系统,特别是在公司内网的环境下使用时,搭建私有仓库就很有必要了。
所谓的私有仓库,也就是在本地的局域网搭建的一个类似公共的仓库,搭建完毕后,我们可以将镜像提交到私有仓库中,这样我们就可以在局域网内,使用docker运行我们的项目镜像
二.安装环境
这里我们安装一台centos7.4的系统作为服务端,安装模式选择开发者模式,这里为了减少后续依赖包的安装,建议选择所有的安装工具。
三.部署
3.1 运行docker服务,设置docker服务开机启动
[root@docker ~]# systemctl enable docker
3.2 下载镜像的registry
[root@docker ~]# docker pull registry
Using default tag: latest
Trying to pull repository docker.io/library/registry ...
latest: Pulling from docker.io/library/registry
d6a5679aa3cf: Pull complete
ad0eac849f8f: Pull complete
2261ba058a15: Pull complete
f296fda86f10: Pull complete
bcd4a541795b: Pull complete
Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8
Status: Downloaded newer image for docker.io/registry:latest
3.3 查看镜像是否pull成功
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/registry latest 2e2f252f3c88 2 weeks ago 33.3 MB
如上面所示,registry的镜像已经pull成功
3.4 运行registry容器
运行命令如下:
docker run -itd -v /var/data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest
参数说明:
- -itd: 在容器中打开一个伪终端进行交互操作,并在后台运行
- -v: 把宿主机的/var/data/registry目录绑定到容器/var/lib/registry目录。注意/var/lib/registry目录是registry容器中存放镜像文件的目录,从而来实现数据的持久化
- -p: 映射端口,访问宿主机的5000端口就访问到registry容器的服务了
- --restart=always: 这是重启的策略,假如这个容器异常退出会自动重启容器
- -name registry: 创建容器命名为registry,这个可以任意命名
- registry:latest: 这个是刚才pull下来的镜像
执行结果如下:
[root@docker ~]# mkdir -p /var/data/registry
[root@docker ~]# docker run -itd -v /var/data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest
d3eb95a6ed47b42e09a559d8208152444d759ea7195a344c41a0c2eff3e7bf7d
3.5 测试镜像仓库中的所有镜像
[root@docker ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}
上面显示docker镜像库是空的,因为我们没有向镜像仓库添加任务东西,所以显示的为空。
3.6 测试镜像仓库
下面的操作表示修改下载镜像源
[root@docker ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": [ "https://registry.docker-cn.com"]
}
修改完毕后,重启docker服务
[root@docker ~]# systemctl restart docker
3.7 下载kolla-ansible相关的docker镜像
这里以openstack rocky版本的centos-source-kuryr-libnetwork镜像为例:
docker pull kolla/centos-source-kuryr-libnetwork:rocky
执行完毕后,我们可以查看此镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/kolla/centos-source-kuryr-libnetwork rocky b035dc468492 3 days ago 873 MB
docker.io/registry latest 2e2f252f3c88 2 weeks ago 33.3 MB
3.8 为镜像打标签
命令说明:
#docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
- SOURCE_IMAGE:这是源镜像,对应上面pull镜像
- TAG:对应版本的标记,可以自己定义
- TARGET_IMAGE:对应的是目标镜像,也就是registry私有镜像服务器的IP地址和端口
这里我们以上面的kuryr镜像为例镜像标记tag:
[root@docker ~]# docker tag docker.io/kolla/centos-source-kuryr-libnetwork:rocky 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork:rocky
查看标记tag的镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork rocky b035dc468492 3 days ago 873 MB
docker.io/kolla/centos-source-kuryr-libnetwork rocky b035dc468492 3 days ago 873 MB
docker.io/registry latest 2e2f252f3c88 2 weeks ago 33.3 MB
[root@docker ~]#
标记完成后,需要将镜像push到服务器仓库中(可以理解类似Git代码提交):
[root@docker ~]# docker push 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork:rocky
The push refers to a repository [127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork]
5b73e9a49532: Pushed
c5205f679e5c: Pushed
e676b8e4e64a: Pushed
62629ddb5514: Pushed
66732cc6705c: Pushed
fdec4322dc05: Pushed
de56e5467e9a: Pushed
659c1fb96a8b: Pushed
5acffa004ecc: Pushed
f8b791cafea2: Pushed
7e941e6c22d9: Pushed
75cdbe684e3c: Pushed
b92b222f8d33: Pushed
37adea4d4a62: Pushed
6c2524892b03: Pushed
d72820786e47: Pushed
a8a0482cad2a: Pushed
e04dcafed429: Pushed
ca9e58aa7a67: Pushed
f50368587532: Pushed
4aca78802847: Pushed
f533c990098d: Pushed
10cd68da8d83: Pushed
125a014b33cd: Pushed
ee9ba7d137dd: Pushed
36f1266a7e8b: Pushed
eb5662c1e71b: Pushed
12ed8b0535ba: Pushed
234f4b667a31: Pushed
2d29bd7de33b: Pushed
5781bb48fca1: Pushed
c4efedf16900: Pushed
7406606a83d7: Pushed
32d8255b5677: Pushed
827c0aaa8003: Pushed
0d35db32b9da: Pushed
06d4dfba3f88: Pushed
957ad28ca1e0: Pushed
1d31b5806ba4: Pushed
rocky: digest: sha256:6f1ef73f275c14ca690c84ff4185eda4f394aae5c61310144d3b5227fbd05588 size: 8466
3.9 下载镜像测试
- 首先删除服务器节点上已经存在的kuryr镜像
[root@docker ~]# docker rmi -f $(docker images |grep kuryr |awk -F ' ' '{print $3}')
查看kuryr的镜像是否已经删除
[root@docker ~]# docker images
从本地仓库下载kuryr的镜像:
[root@docker ~]# docker pull 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork:rocky
Trying to pull repository 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork ...
rocky: Pulling from 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork
256b176beaff: Pull complete
ef89588d2fb8: Pull complete
4e137e881ddf: Pull complete
a32d88347455: Pull complete
b97e1eb80178: Pull complete
ee9134797386: Pull complete
d5b31b996fa4: Pull complete
bedbdf11bcb5: Pull complete
4cf9a268020d: Pull complete
683b317508ee: Pull complete
05c012122525: Pull complete
de016ee2a1d2: Pull complete
12e98fdcca18: Pull complete
12734231b260: Pull complete
9d44ef95cf45: Pull complete
b86461a35e6f: Pull complete
c4254a673cf2: Pull complete
b6252be956de: Pull complete
5ff43c93c349: Pull complete
b40545393899: Pull complete
348a4a03ae8f: Pull complete
02f66da7fa9c: Pull complete
f1978b8e4236: Pull complete
fd05c6570ba0: Pull complete
804cfffcb7df: Pull complete
7b31fef9e1b7: Pull complete
6b50f073141b: Pull complete
423964eade05: Pull complete
1edc976c8ae3: Pull complete
6bac85d74ab3: Pull complete
cbe73bb09bb5: Pull complete
ecad1cee7fb0: Pull complete
9f8721661e2a: Pull complete
c54465db9f7b: Pull complete
f3ef02872dd6: Pull complete
37e45b413139: Pull complete
3366c86d4ee4: Pull complete
8d7307dece5d: Pull complete
1133dd455640: Pull complete
Digest: sha256:6f1ef73f275c14ca690c84ff4185eda4f394aae5c61310144d3b5227fbd05588
Status: Downloaded newer image for 127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork:rocky
查看下载的镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/kolla/centos-source-kuryr-libnetwork rocky b035dc468492 3 days ago 873 MB
docker.io/registry latest 2e2f252f3c88 2 weeks ago 33.3 MB
3.10 列出本地仓库中所有的镜像
[root@docker ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":["kolla/centos-source-kuryr-libnetwork"]}
列出标签:
[root@docker ~]# curl http://127.0.0.1:5000/v2/kolla/centos-source-kuryr-libnetwork/tags/list
{"name":"kolla/centos-source-kuryr-libnetwork","tags":["rocky"]}