通常我们在docker拉取的镜像都是在docker hubquay.io等公有仓库获取,那么在实际工作中,每个公司如果使用到docker,那么肯定是要搭建自己的私有仓库。那么接下来就通过docker提供的registry镜像来搭建我们自己的私有仓库。

1、拉取registry镜像

这里默认拉取最新版本,不指定版本就默认拉取latest版本

docker pull registry
Using default tag: latest
latest: Pulling from library/registry
ddad3d7c1e96: Downloading 
6eda6749503f: Download complete 
363ab70c2143: Download complete 
5b94580856e6: Download complete 
12008541203a: Download complete 
latest: Pulling from library/registry
ddad3d7c1e96: Pull complete 
6eda6749503f: Pull complete 
363ab70c2143: Pull complete 
5b94580856e6: Pull complete 
12008541203a: Pull complete 
Digest: sha256:121baf25069a56749f249819e36b386d655ba67116d9c1c6c8594061852de4da
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

使用镜像查看命令,查看镜像

docker images registry
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
registry     latest    1fd8e1b0bb7e   3 months ago   26.2MB

2、运行registry镜像

docker run -d -p 5000:5000 --name registry registry:latest 
795e0a78aea0bf39ea95f800221f26fb4882efd4c4dc2143f90b37ee10da3126

查看容器是否启动成功

docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                       NAMES
795e0a78aea0   registry:latest   "/entrypoint.sh /etc…"   43 seconds ago   Up 42 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

查看私有仓库

curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}

可以看到,目前私有仓库里面为空。

3、向私有仓库推送镜像

docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
arvin88/tomcat_user   v1        77fc61f55a0c   2 days ago     590MB

现在将镜像arvin88/tomcat_user修改为127.0.0.1:5000/tomcat_user

docker tag arvin88/tomcat_user:v1 127.0.0.1:5000/tomcat_user:v2
[root@localhost docker]# docker images
REPOSITORY                   TAG       IMAGE ID       CREATED        SIZE
127.0.0.1:5000/tomcat_user   v2        77fc61f55a0c   2 days ago     590MB
arvin88/tomcat_user          v1        77fc61f55a0c   2 days ago     590MB

推送到私有仓库。

docker push 127.0.0.1:5000/tomcat_user:v2
The push refers to repository [127.0.0.1:5000/tomcat_user]
fa24164bb3bf: Pushed 
e4457e70997e: Pushed 
ffb06ffdd1e8: Pushed 
b6a3bb3fa031: Pushed 
32fff2065e67: Pushed 
ea822a1a4f9d: Pushed 
fe6a347f93b5: Pushed 
f3d5b8f65132: Pushed 
ad83f0aa5c0a: Pushed 
5a9a65095453: Pushed 
4b0edb23340c: Pushed 
afa3e488a0ee: Pushed 
v2: digest: sha256:8d4ed98df7c6ea9b22e33f61c9fac6020625592d75c42759c640bc8ad2af889e size: 2841

然后再使用curl -XGET http://127.0.0.1:5000/v2/_catalog查看私有仓库。

curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":["tomcat_user"]}

可以看到已经推成功了。

4、从私有仓库拉取镜像

为了测试,先将本地镜像全部删除

docker pull 127.0.0.1:5000/tomcat_user:v2
v2: Pulling from tomcat_user
627b765e08d1: Pull complete 
c040670e5e55: Pull complete 
073a180f4992: Pull complete 
bf76209566d0: Pull complete 
f10db7ba7580: Pull complete 
5e5dee180760: Pull complete 
c26c02f721c2: Pull complete 
d24d42fda432: Pull complete 
20abf5c2533f: Pull complete 
d16e82e0b4a0: Pull complete 
32f2c6ec5ff1: Pull complete 
975419add384: Pull complete 
Digest: sha256:8d4ed98df7c6ea9b22e33f61c9fac6020625592d75c42759c640bc8ad2af889e
Status: Downloaded newer image for 127.0.0.1:5000/tomcat_user:v2
127.0.0.1:5000/tomcat_user:v2

查看本地镜像结果

REPOSITORY                   TAG       IMAGE ID       CREATED        SIZE
127.0.0.1:5000/tomcat_user   v2        77fc61f55a0c   2 days ago     590MB

5、常用API

  • 查看私有仓库:curl -XGET http://127.0.0.1:5000/v2/_catalog
curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":["tomcat_user"]}
  • 查看镜像tag:curl -XGET http://127.0.0.1:5000/v2//tags/list
curl -XGET http://127.0.0.1:5000/v2/tomcat_user/tags/list
{"name":"tomcat_user","tags":["v2"]}

官网还提供了其他API,有兴趣可以查看仓库API

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐