Docker-私有倉庫使用

環境信息

[root@suhw ~]# docker --version
Docker version 19.03.10, build 9424aeaee9
[root@suhw ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

獲取鏡像

可通過自己下載tar包引入,也可通過默認直接從docker hub下載,以下方式二選一


引入鏡像

下載地址:https://hub.docker.com/_/registry

若已經手動下載好私有倉庫鏡像,則可通過以下命令加載鏡像

[root@suhw ~]# docker load -i registry.tar

拉取鏡像

# 搜索registry 鏡像
[root@suhw ~]# docker search registry

# 默認拉取最新版本鏡像
[root@suhw ~]# docker pull registry

#查看鏡像
[root@suhw ~]# docker image list
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
registry                      latest              708bc6af7e5e        4 months ago        25.8MB

啓動私有倉庫

參數介紹

  • -d, --detach Run container in background and print container ID

  • -p, --publish list Publish a container's port(s) to the host

  • -v, --volume list Bind mount a volume


啓動鏡像

[root@suhw ~]# docker run -d -p 4000:5000 --name=local_registry -v /suhw/registry:/var/lib/registry registry

倉庫默認存放鏡像等信息在容器的 /var/lib/registry/docker 目錄下,所以將該目錄映射到本地,可以進入該目錄查看已上傳鏡像信息。


查看倉庫鏡像

#查看倉庫中的鏡像
[root@csmp-standalone ~]# curl http://127.0.0.1:4000/v2/_catalog
{"repositories":[]}

上傳鏡像

使用最原始的hello-world舉例

  1. 拉取hello-world鏡像
[root@suhw ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

[root@suhw ~]# docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              708bc6af7e5e        4 months ago        25.8MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

  1. 打新的tag,標明私有倉庫地址
[root@suhw ~]# docker tag hello-world:latest 127.0.0.1:4000/suhw/suhw-hello-world:20200601
  1. 推送至本地倉庫
# 推送至私有倉庫
[root@suhw ~]# docker push 127.0.0.1:4000/suhw/suhw-hello-world:20200601
  1. 查看私有倉庫鏡像
[root@suhw ~]# curl http://127.0.0.1:4000/v2/_catalog
{"repositories":["suhw/suhw-hello-world"]}

私有倉庫打包

倉庫默認存放鏡像等信息在容器的 /var/lib/registry/docker 目錄下,可以進入該目錄查看已上傳鏡像信息。

由於在創建容器的時候將私有倉庫的 /var/lib/registry/docker 目錄映射到了本地的/data/suhw/registry倉庫,所以打包時只需要將/suhw/registry目錄打包,以後用的時候解壓到與 /var/lib/registry/docker 對應的目錄下即可

打包

# 將私有倉庫映射目錄文件打包
[root@suhw ~]# tar -cf /suhw/suhw-registry-20200601.tar -C /suhw/registry/ .

參數介紹:

  • -c-c--create 建立新的備份文件。
  • -f:指定備份文件
  • -C:切換目錄

上述命令相當於切換到/suhw/registry目錄下,並將該目錄下所有文件統一打包

查看打包後的文件:

[root@suhw ~]# ll /suhw/
total 52
drwxr-xr-x. 3 root root    20 Jun  1 16:07 registry
-rw-r--r--. 1 root root 51200 Jun  1 16:09 suhw-registry-20200601.tar

生成md5

[root@suhw ~]# md5sum /suhw/suhw-registry-20200601.tar > /suhw/suhw-registry-20200601.tar.md5
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章