【Docker】第四篇 Docker仓库管理

时间:2024-01-14 08:54:08

一、仓库概述

  • 仓库(Repository)Docker仓库主要用于镜像的存储,它是镜像分发、部署的关键。仓库分为公共仓库和私有仓库。
  • 注册服务器(Registry)和仓库区别注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)
  • 官方的公用仓库Docker Hub:如果仅仅是搜索和使用Docker Hub的公共镜像,不需要Docker Hub账户就可以直接操作。如果要上传和分享我们自己创建的镜像,就需要Docker Hub账户。注:注册账户需要借助*工具

二、仓库管理

、注册账号
https://hub.docker.com/ #在此页面注册账号,需要用户名,邮箱,密码(注:需要*才能注册,注册通过邮箱激活后可以通过网页登陆)
、登陆docker hub
root@localhost ~]# docker login
#Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: *******
Password:
Login Succeeded
、查找镜像 #可参考https://www.cnblogs.com/yangleitao/p/9683104.html
[root@localhost ~]# docker search centos #可以加上版本号
、下载镜像
[root@localhost ~]# docker pull centos
、上传镜像
#我们可以把自己的镜像传到docker hub官网上,前提是已经注册了账号
[root@localhost ~]# docker push image_name

三、搭建私有仓库

、使用registry镜像创建私有仓库
[root@localhost ~]# docker search registry
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
registry The Docker Registry 2.0 implementation for s… [OK]
[root@localhost ~]# docker pull registry #直接下载镜像
[root@localhost ~]# docker images #查看新下载的镜像 、
[root@localhost ~]# mkdir -p /data/registry/ #创建一个本地目录,等一下挂载
[root@localhost ~]#
[root@localhost ~]# docker run -d -p : -v /data/registry/:/tmp/registry registry
599c0e1a298f5e7a19b9ba01ff314c3e3a26a22b3cba1e6800e21ffb54c8e9d5
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
599c0e1a298f registry "/entrypoint.sh /etc…" seconds ago Up seconds 0.0.0.0:->/tcp vibrant_engelbart
17c54a92a4e8 ubuntu:latest "/bin/bash" days ago Up days quizzical_bhabha
[root@localhost ~]#

-d : 后台运行

-p : 宿主机跟容器映射端口 SERVER_PORT:CONTAINER_PORT

-v : 挂载宿主机目录到容器中作为数据卷, docker registry上传镜像默认存放到容器/var/lib/registry,将本地/data/registry目录挂载到容器中,避免删除容器是数据丢失

、管理私有仓库
[root@localhost ~]# docker run -d -p : registry
19003703c71307603cdb48fab242c96dc34c0e37f0dcfe2e568658abbea40557
[root@localhost ~]# ps -aux|grep docker [root@localhost ~]# docker push 192.168.19.130:/test
报如下错:
The push refers to a repository [192.168.19.130:/test]
Get https://192.168.19.130:5000/v1/_ping: dial tcp 192.168.19.130:5000: getsockopt: connection refused
解决办法:
a,执行
echo '{ "insecure-registries":["192.168.19.130:5000"] }' >> /etc/docker/daemon.json #或者直接修改配置文件
b, 重启docker client的docker 服务 [root@localhost ~]# systemctl restart docker #如果容器没有开启也会报错
[root@localhost ~]# docker push 192.168.19.130:/test1 #再次上传成功
The push refers to repository [192.168.19.130:/test1]
8d7ea83e3c62: Pushed
6a061ee02432: Pushed
f73b2816c52a: Pushed
6267b420796f: Pushed
a30b835850bf: Pushed
latest: digest: sha256:a819482773d99bbbb570626b6101fa37cd93a678581ee564e89feae903c95f20 size: [root@localhost ~]# curl -XGET http://192.168.19.130:5000/v2/_catalog
{"repositories":["test","test1"]} [root@localhost ~]# curl -XGET http://192.168.19.130:5000/v2/test1/tags/list
{"name":"test1","tags":["latest"]}
[root@localhost ~]#