Docker公共镜像仓库与私有镜像仓库的构建

一、DockerHub公共镜像仓库的使用

1、登录docker.com官网注册一个账号

在这里插入图片描述

2、登录docker官网

docker login 
#输入用户ID和密码

3、推送镜像至仓库

为了方便测试,我们将hello-world 镜像拉取至本地,然后再上传至DockerHub仓库中。
先给镜像设置标签docker tag local-image:tagname new-repo:tagname;
再将镜像推送至仓库docker push new-repo:tagname

docker tag hello-world:latest mrgcc1314/test-hello-world:1.0.0    #将镜像打上标签

docker push mrgcc1314/test-hello-world:1.0.0         #将镜像上传到docker中


docker pull mrgcc1314/test-hello-world:1.0.0         #在docker仓库中拉取镜像测试是否创建成功

docker logout        #退出当前登录的docker账号

二、Docker私有仓库的搭建及认证

DockerHub为我们提供了很多官方镜像和个人上传的镜像,我们可以下载机构或个人提供的镜像,也可以上传我们自己的本地镜像,但缺点是:
● 由于网络的原因,从DockerHub下载和上传镜像速度可能会比较慢;
● 在生产上使用的Docker镜像可能包含我们的代码、配置信息等,不想被外部人员获取,只允许内网的开发人员下载。

为了解决以上问题,Docker 官方提供了一个叫做 registry 的镜像用于搭建本地私有仓库使用。在内部网络措建的Docker 私有仓库可以使内网人员下载、上传都非常快速, 不受外网带宽等因素的影响,同时不在内网的人员也无法下载我们的镜像,并且私有仓库也支持配置仓库认证功能。接下来详细讲解registry 私有仓库的搭建过程。

1.拉取私有仓库镜像

拉取私有仓库镜像。
docker pull registey

2.修改配置

修改 daemon.json 文件。

vi /etc/docker/daemon.json

添加以下内容,用于让Docker信任私有仓库地址,保存退出。
{
    
    
......
"insecure-registries": ["192.168.200.60:5000"]
}

重新加载配置信息及重启Docker服务。

#重新加载某个服务的置文件
systemctl daemon-reload

#重新启动docker
systemctl restart docker

3.创建私有仓库容器

创建私有仓库容器:	
docker run -di --name registry -p 5000:5000 -v /mydata/docker_registry:/var/lib/registry registry
●-d :后台运行容器;
●--name :为创建的容器命名;
●-p :表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射;
●-v :将容器内/var/lib/registry 目录下的数据挂载至宿主机/mydata/docker_registry 目录下;

打开浏览器输入: http://192.168.200.60:5000/v2/_catalog 看到{“repositories”:[]}表示私有仓库搭建成功并且内容为空。

或者直接输入命令:

curl -XGET http://192.168.200.60:5000/v2/_catalog

4.推送镜像至私有仓库

先给镜像设置标签docker tag local-image:tagname new-repo:tagname;
再将镜像推送至私有仓库docker push new-repo:tagname

docker tag hello-world:latest 192.168.200.60:5000/test-hello-world:1.0.0
docker push 192.168.200.60:5000/test-hello-world:1.0.0

打开浏览器输入: http://192.168.200.60:5000/v2/_catalog 可以看到私有仓库中已,上传的镜像。

或者直接输入命令:

curl -XGET http://192.168.200.60:5000/v2/_catalog
#当创建好私有仓库并且上传镜像到该私有仓库以后,便可以通过该私有仓库中的镜像创建容器了
docker run -it --name hello 192.168.200.60:5000/test-hello-world:1.0.0 

5.配置私有仓库认证

私有仓库已经搭建好了,要确保私有仓库的安全性,还需要一个安全认证证书, 防止发生意想不到的事情。所以需要在搭建私有仓库的Docker主机上先生成自签名证书。

#创建证书存储目录。
mkdir -p /usr/1ocal/registry/certs

#生成自签名证书命令。
openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/1ocal/registry/certs/domain.key -x509 -days 365 -out /usr/1ocal/registry/certs/domain.crt

选项:
openssl req :创建证书签名请求等功能;
-newkey :创建CSR证书签名文件和RSA私钥文件;
rsa:2048 :指定创建的RSA私钥长度为2048;
-nodes :对私钥不进行加密;
-sha256 :使用SHA256算法;
-keyout :创建的私钥文件名称及位置;
-x509 :自签发证书格式;
-days :证书有效期; 
-out :指定CSR输出文件名称及位置;

5.1.生成自签名证书

通过openssl先生成自签名证书,运行命令以后需要填写一些证书信息,里面最关键的部分是: Common Name (eg, your name or your server’s hostname) []:192.168.200.60 ,这里填写的是私有仓库的地址。

[ root@localhost ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/1ocal/registry/certs/domain.key -x509 -days 365 -out /usr/1ocal/registry/certs/domain.crt
Generating a 2048 bit RSA private key
...........+++
..............+++
writing new private key to '/usr/1ocal/registry/certs/domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:192.168.200.60
Email Address []:

5.2.生成鉴权密码文件

#创建存储鉴权密码文件目录
mkdir -p /usr/local/registry/auth

#如果没有htpasswd 功能需要安装httpd
yum install -y httpd

#创建用户和密码
htpasswd -Bbn root 1234 > /usr/local/registry/auth/htpasswd
htpasswd 是apache http的基本认证文件,使用htpasswd命令可以生成用户及密码文件。

5.3.创建私有仓库容器

docker run -di --name registry -p 5000:5000 \
-v /mydata/docker_registry:/var/lib/registry \
-v /usr/1ocal/registry/certs:/certs \
-v /usr/1ocal/registry/auth:/auth \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
registry

5.4.推送镜像至私有仓库失败

#先给镜像设置标签
[root@localhost ~]#docker tag hello-world:latest 192.168.200.60:5000/test-hello-world:1.0.0 

#再将镜像推送至私有仓库
[root@localhost ~]#docker push 192.168.200.60:5000/test-hello-world:1.0.0 
The push refers to repository [192.168.200.60:5000/test-hello-world]
f22b99068db9: Preparing 
no basic auth credentials

如果直接push镜像肯定会失败,并且出现no basic auth credentials 的错误,这是因为我们没有进行登录认证。

5.5.登录账号

通过docker login 命令输入账号密码登录私有仓库。
docker login 192.168.200.60:5000

5.6.推送镜像至私有仓库成功

再次push镜像,发现已经可以推送成功了。
docker push 192.168.200.60:5000/test-hello-world:1.0.0 

5.7.退出账号

通过docker logout 命令退出账号。
docker logout 192.168.200.60

猜你喜欢

转载自blog.csdn.net/Gengchenchen/article/details/115125438