1. 程式人生 > >搭建本地私有docker倉庫

搭建本地私有docker倉庫

搭建本地私有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"]}





搭建本地私有docker倉庫