搭建本地私有docker仓库

1、使用registry镜像创建私有仓库

 docker run -d -p 5000:5000   --restart=always --name registry registry:2

这条命令将自动下载并启动一个registry容器,创建本地的私有仓库

--restart=always:表示当docker服务重启时,registry也会自动启动


2、 从配置的公共registry地址下载ubuntu:16.04 镜像到本地

docker pull ubuntu:16.04



3、将镜像重新打一个tag

# docker tag ubuntu:16.04 localhost:5000/my-ubuntu
[root@OPS01-LINTEST02 ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
localhost:5000/my-ubuntu   latest              5e8b97a2a082        7 days ago          114MB
ubuntu                     16.04               5e8b97a2a082        7 days ago          114MB


4、将镜像push到自己搭建的私有仓库中

# docker push localhost:5000/my-ubuntu
The push refers to repository [localhost:5000/my-ubuntu]
2de391e51d73: Pushed 
d73dd9e65295: Pushed 
686245e78935: Pushed 
d7ff1dc646ba: Pushed 
644879075e24: Pushed 
latest: digest: sha256:689aa49d87d325f951941d789f7f7c8fae3394490cbcf084144caddba9c1be12 size: 1357


5、删除本地缓存的ubuntu:16.04localhost:5000/my-ubuntu 镜像,这不会删除私有仓库中的镜像。然后测试从私有仓库pull镜像。

# docker image remove ubuntu:16.04
# docker image remove localhost:5000/my-ubuntu


6、从本地私有仓库下载镜像

# docker pull localhost:5000/my-ubuntu
Using default tag: latest
latest: Pulling from my-ubuntu
b234f539f7a1: Pull complete 
55172d420b43: Pull complete 
5ba5bbeb6b91: Pull complete 
43ae2841ad7a: Pull complete 
f6c9c6de4190: Pull complete 
Digest: sha256:689aa49d87d325f951941d789f7f7c8fae3394490cbcf084144caddba9c1be12
Status: Downloaded newer image for localhost:5000/my-ubuntu:latest


7、stop 本地registry

# docker container stop registry

停止私有仓库的容器

docker container stop registry && docker container rm -v registry

停止容器并删除


8、自定义registry的端口,当5000端口被占用时,可以使用其他端口替代

# docker run -d -p 5001:5000 --name registry-test registry:2
29f769711de0c981abf7b2dff7e79297338e860abf01ec330d09036da8045a42
[root@OPS01-LINTEST02 ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      506/rpcbind         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      73954/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1197/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      960/zabbix_agentd   
tcp6       0      0 :::5001                 :::*                    LISTEN      111483/docker-proxy 
tcp6       0      0 :::111                  :::*                    LISTEN      506/rpcbind         
tcp6       0      0 :::21                   :::*                    LISTEN      953/vsftpd          
tcp6       0      0 :::22                   :::*                    LISTEN      73954/sshd          
tcp6       0      0 :::10050                :::*                    LISTEN      960/zabbix_agentd

-p后面第一个5001是docker服务器对外的端口,第二个5000是容器的端口。


9、如果想修改容器内的registry服务器监听的端口,可以使用下面的命令

docker run -d -e REGISTRY_HTTP_ADDR=0.0.0.0:5001 -p 5001:5001 --name registry-test registry:2


10、将自定义仓库中的镜像存储到主机上

默认情况下,我们上传到私有仓库中的镜像存储在容器的/var/lib/registry路径下,如果想将镜像保存到本地,可以在容器启动时,创建一个卷

docker run -d \
  -p 5000:5000 \
  --restart=always \
  --name registry \
  -v /mnt/registry:/var/lib/registry \
  registry:2

这样重新上传的镜像也会保存在本地的/mnt/registry下

通过API来查询本地仓库镜像信息

# curl http://172.16.2.14:5000/v2/_catalog
{"repositories":["my-ubuntu"]}


#  curl http://172.16.2.14:5000/v2/my-ubuntu/tags/list
{"name":"my-ubuntu","tags":["latest"]}





猜你喜欢

转载自blog.51cto.com/zengestudy/2129031