Docker搭建私有仓库
文章目录
- Docker搭建私有仓库
- 安装docker
- 安装运行 docker-registry
- 在私有仓库上传镜像
- 私有仓库下载镜像
- 其他主机上传镜像
- 参考
Docker搭建私有仓库
安装docker
这里就不赘述了,我们可以直接参考:https://www.runoob.com/docker/docker-tutorial.html
本文采用的是Centos 7版本,docker version是19.03.1
安装运行 docker-registry
可以通过获取官方 registry 镜像来运行。
docker run -d -p 5000:5000 --restart=always --name registry registry
[root@cai ~]# docker run -d -p 5000:5000 --restart=always --name registry registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:latest
c56e18bc9616128030b22047d787a84d078c2356d56755e9e97e84c1037b0676
然后我们通过docker image命令查看到我们下载的镜像
[root@cai ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest f32a97de94e1 5 months ago 25.8MB
然后我们通过docker pull命令下载一个镜像,我们这里下载最简单的一个hello-world镜像
[root@cai ~]# docker pull hello-world
在私有仓库上传镜像
创建好私有仓库之后,就可以使用 docker tag 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为 127.0.0.1:5000,然后我们就可以把刚刚下载的hello-world镜像打包一个自己的镜像上传上去。
使用 docker tag 将 hello-world:latest 这个镜像标记为 127.0.0.1:5000/my-hello-world:latest。
格式为 docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]
。
[root@cai ~]# docker tag hello-world:latest 127.0.0.1:5000/my-hello-world:lastest
[root@cai ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest f32a97de94e1 5 months ago 25.8MB
127.0.0.1:5000/my-hello-world lastest fce289e99eb9 7 months ago 1.84kB
hello-world latest fce289e99eb9 7 months ago 1.84kB
使用 docker push
上传标记的镜像。
[root@cai ~]# docker push 127.0.0.1:5000/my-hello-world
The push refers to repository [127.0.0.1:5000/my-hello-world]
af0b15c8625b: Pushed
lastest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524
然后我们通过私有仓库接口查看一下镜像。
[root@cai ~]# curl 127.0.0.1:5000/v2/_catalog
{ "repositories":["my-hello-world"]}
说明镜像已经上传上去。
私有仓库下载镜像
现在我们尝试从私有仓库下载这个镜像。
首先我们先移除掉本地的镜像。
[root@cai ~]# docker image rm 127.0.0.1:5000/my-hello-world:lastest
Untagged: 127.0.0.1:5000/my-hello-world:lastest
Untagged: 127.0.0.1:5000/my-hello-world@sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
[root@cai ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest f32a97de94e1 5 months ago 25.8MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
然后我们通过docker pull来拉取我们的镜像。
[root@cai ~]# docker pull 127.0.0.1:5000/my-hello-world:lastest
lastest: Pulling from my-hello-world
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
Status: Downloaded newer image for 127.0.0.1:5000/my-hello-world:lastest
127.0.0.1:5000/my-hello-world:lastest
[root@cai ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest f32a97de94e1 5 months ago 25.8MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
127.0.0.1:5000/my-hello-world lastest fce289e99eb9 7 months ago 1.84kB
其他主机上传镜像
如果你不想使用 127.0.0.1:5000 作为仓库地址,比如想让本网段的其他主机也能把镜像推送到私有仓库。你就得把例如 192.168.174.136:5000 这样的内网地址作为私有仓库地址,这时你会发现无法成功推送镜像。
这是因为 Docker 默认不允许非 HTTPS 方式推送镜像。我们可以通过 Docker 的配置选项来取消这个限制。
我们现在有另一台主机,ip地址为192.168.174.137,然后我们尝试推送镜像到我们192.168.174.136的私有仓库中。
我们先下载个镜像。
[root@cai ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@cai ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 7 months ago 1.84kB
[root@cai ~]#
然后打包
[root@cai ~]# docker tag hello-world:latest 192.168.174.136:5000/my-docker2-hello-world:lastest
[root@cai ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.174.136:5000/my-docker2-hello-world lastest fce289e99eb9 7 months ago 1.84kB
hello-world latest fce289e99eb9 7 months ago 1.84kB
[root@cai ~]#
然后我们把镜像推到192.168.174.136这台机器的私有仓库中。
然后发现出现如下问题:
[root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
Get https://192.168.174.136:5000/v2/: dial tcp 192.168.174.136:5000: connect: no route to host
这个时候可以尝试把两台机器的防火墙都关闭。
[root@cai ~]# systemctl stop firewalld
然后我们继续
[root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
Get https://192.168.174.136:5000/v2/: http: server gave HTTP response to HTTPS client
[root@cai ~]#
然后我们需要在推送的那台机器,也就是在IP为192.168.174.137这台机器上配置一下/etc/docker/daemon.json
(如果文件不存在请新建该文件)
[root@cai ~]# vim /etc/docker/daemon.json
{
"registry-mirror": [
"https://registry.docker-cn.com"
],
"insecure-registries": [
"192.168.174.136:5000"
]
}
然后重启docker
[root@cai ~]# systemctl restart docker
重新push
[root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
af0b15c8625b: Pushed
lastest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524
然后查看私有仓库的接口返回
[root@cai ~]# curl 192.168.174.136:5000/v2/_catalog
{"repositories":["my-docker2-hello-world","my-hello-world"]}
说明已经能成功上传镜像了。
参考
https://yeasy.gitbooks.io/docker_practice/repository/registry.html
还没有评论,来说两句吧...