仓库是存放镜像的地方,每个服务器可以有多个仓库,而每个仓库下面有多个镜像。仓库分为公库和私库,官方的公共仓库就是docker Hub,不过国内被墙。今天就来创建一个私有仓库
docker registry是官方提供的工具,用来构建私有镜像仓库
启动registry镜像
[root@ubuntu:~]# docker run -d -p 5000:5000 --restart=always --name registry registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
81033e7c1d6a: Pull complete
b235084c2315: Pull complete
c692f3a6894b: Pull complete
ba2177f3a70e: Pull complete
a8d793620947: Pull complete
Digest: sha256:672d519d7fd7bbc7a448d17956ebeefe225d5eb27509d8dc5ce67ecb4a0bce54
Status: Downloaded newer image for registry:latest
e97c298aabcdb30b1901b33b03768c78bcda3643c3255084ad3cbe4df68e6a8c
将registry镜像启动私有仓库,仓库会被创建在容器/var/lib/registry目录下,
使用-v参数来讲镜像文件放到本地/opt/data/registry下
报错:
[root@ubuntu:~]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
08ccd61cfc5fe2fdcbee16aede38807ca80239141f5e6dd6adf3cb825ef76f62
docker: Error response from daemon: driver failed programming external connectivity on endpoint unruffled_villani (b12b514dc8419abf708dca81f227997b4957f0cb3f52432978b25760b4f7a9dc): Bind for 0.0.0.0:5000 failed: port is already allocated.
停掉占用端口的容器即可
[root@ubuntu:/]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
49bc7964d631d1c45058a1b03305106c13afb959eb4c794c6fed9233c1c1ce1b
[root@ubuntu:/]# cd /opt/data/
[root@ubuntu:/opt/data]# ls
registry
可以看到registry目录,表示成功了
docker tag
将test/ubuntu v1.0 这个镜像标记为 127.0.0.0:5000/ubuntu:latest
[root@ubuntu:/opt/data]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 072cd43a3a6a 2 hours ago 85.8MB
<none> <none> 0ccd5ef276a3 47 hours ago 85.8MB
ubuntu latest c9d990395902 2 days ago 113MB
ubuntu 17.10 14107f6d2c97 2 days ago 99.1MB
centos latest e934aafc2206 8 days ago 199MB
registry latest d1fd7d86a825 3 months ago 33.3MB
[root@ubuntu:/opt/data]# docker tag test/ubuntu:v1.0 127.0.0.0:5000/ubuntu:latest
[root@ubuntu:/opt/data]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.0:5000/ubuntu latest 072cd43a3a6a 2 hours ago 85.8MB
test/ubuntu v1.0 072cd43a3a6a 2 hours ago 85.8MB
<none> <none> 0ccd5ef276a3 47 hours ago 85.8MB
ubuntu latest c9d990395902 2 days ago 113MB
ubuntu 17.10 14107f6d2c97 2 days ago 99.1MB
centos latest e934aafc2206 8 days ago 199MB
registry latest d1fd7d86a825 3 months ago 33.3MB
[root@ubuntu:/opt/data]#
docker push
上传标记的镜像
[root@ubuntu:/opt/data]# docker push 127.0.0.1:5000/ubuntu
The push refers to repository [127.0.0.1:5000/ubuntu]
414667feb05a: Pushed
latest: digest: sha256:c408657b59e55a1ca24e7b5165bdf620031c79d5b803b2df68b12ed249dfe951 size: 528
curl查看仓库中的镜像
可以看到 {"repositories":["ubuntu"]} 表示上传成功
[root@ubuntu:/opt/data]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["ubuntu"]}
验证:
删除已经创建的127.0.0.1:5000/ubuntu镜像,再从私有仓库下载刚创建的镜像
[root@ubuntu:/opt/data]# docker image rm 127.0.0.1:5000/ubuntu
Untagged: 127.0.0.1:5000/ubuntu:latest
Untagged: 127.0.0.1:5000/ubuntu@sha256:c408657b59e55a1ca24e7b5165bdf620031c79d5b803b2df68b12ed249dfe951
这里可以看到镜像已经被删除了
[root@ubuntu:/opt/data]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 072cd43a3a6a 2 hours ago 85.8MB
<none> <none> 0ccd5ef276a3 47 hours ago 85.8MB
ubuntu latest c9d990395902 2 days ago 113MB
ubuntu 17.10 14107f6d2c97 2 days ago 99.1MB
centos latest e934aafc2206 8 days ago 199MB
registry latest d1fd7d86a825 3 months ago 33.3MB
重新从仓库下载带有127.0.0.1:5000/ubuntu标签的镜像
[root@ubuntu:/opt/data]# docker pull 127.0.0.1:5000/ubuntu
Using default tag: latest
latest: Pulling from ubuntu
Digest: sha256:c408657b59e55a1ca24e7b5165bdf620031c79d5b803b2df68b12ed249dfe951
Status: Downloaded newer image for 127.0.0.1:5000/ubuntu:latest
这样就下载成功了
[root@ubuntu:/opt/data]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/ubuntu latest 072cd43a3a6a 2 hours ago 85.8MB
test/ubuntu v1.0 072cd43a3a6a 2 hours ago 85.8MB
<none> <none> 0ccd5ef276a3 47 hours ago 85.8MB
ubuntu latest c9d990395902 2 days ago 113MB
ubuntu 17.10 14107f6d2c97 2 days ago 99.1MB
centos latest e934aafc2206 8 days ago 199MB
registry latest d1fd7d86a825 3 months ago 33.3MB
[root@ubuntu:/opt/data]#
注意:不使用127.0.0.1:5000这个地址作为仓库地址,将无法推送镜像,因为docker默认不允许使用https的方式推送镜像