环境信息
[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
举例
- 拉取
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
- 打新的
tag
,标明私有仓库地址
[root@suhw ~]# docker tag hello-world:latest 127.0.0.1:4000/suhw/suhw-hello-world:20200601
- 推送至本地仓库
# 推送至私有仓库
[root@suhw ~]# docker push 127.0.0.1:4000/suhw/suhw-hello-world:20200601
- 查看私有仓库镜像
[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