docker私有镜像仓库的搭建及认证

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: docker私有镜像仓库的搭建及认证

前言


在生产上使用的 Docker 镜像可能包含我们的代码、配置信息等,不想被外部人员获取,只允许内

网的开发人员下载。

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

配置私有仓库(无认证)

拉取私有仓库镜像

docker pull registry

修改配置文件

修改 daemon.json 文件。

添加以下内容,用于让 Docker 信任私有仓库地址,保存退出。

vim /etc/docker/daemon.json

注意json文件,除了最后一行不加逗号,前面的行末尾都要加逗号,否则下面restart将踩坑

{
       "registry-mirrors":["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"],
       "insecure-registries":["192.168.135.10:5000"]
}

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

# 重新加载某个服务的配置文件
sudo systemctl daemon-reload
# 重新启动 docker
sudo systemctl restart docker

创建私有仓库容器

[root@centos8 docker_registry]# docker run -id --name registry -p 5000:5000 -v /root/mydate/docker_registry:/var/lib/registry registry

-v :将容器内 /var/lib/registry 目录下的数据挂载至宿主机 /root/mydate/docker_registry目录下


打开浏览器输入:http://192.168.135.10:5000/v2/_catalog 看到 {“repositories”:[]} 表示私有

仓库搭建成功并且内容为空。

这里的192.168.135.10这个ip即你的Linux的ip,每个人都不同,自己手动查阅

推送镜像至私有仓库

先给镜像设置标签

再将镜像推送至私有仓库

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname
[root@centos8 docker_registry]#docker tag mycentos:7 192.168.135.10:5000/mycentos7:1.0
[root@centos8 docker_registry]#docker push 192.168.135.10:5000/mycentos7:1.0
[root@centos8 docker_registry]#docker tag hello-world:latest 192.168.135.10:5000/myhelloworld
[root@centos8 docker_registry]#docker push 192.168.135.10:5000/myhelloworld

由于我们做了目录挂载,因此可以在宿主机/root/mydate/docker_registry/docker/registry/v2/repositories目录下查看

[root@centos8 repositories]# pwd
/root/mydate/docker_registry/docker/registry/v2/repositories
[root@centos8 repositories]# ls
mycentos7  myhelloworld

此时无认证情况下拉取镜像

docker pull 192.168.135.10:5000/myhelloworld
[root@centos8 repositories]# docker rmi 192.168.135.10:5000/myhelloworld:latest 
Untagged: 192.168.135.10:5000/myhelloworld:latest
Untagged: 192.168.135.10:5000/myhelloworld@sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
[root@centos8 repositories]# docker pull 192.168.135.10:5000/myhelloworld
Using default tag: latest
latest: Pulling from myhelloworld
Digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
Status: Downloaded newer image for 192.168.135.10:5000/myhelloworld:latest
192.168.135.10:5000/myhelloworld:latest
[root@centos8 repositories]# docker images
REPOSITORY                         TAG       IMAGE ID       CREATED        SIZE
mycentos                           7         1b99ed7bf2b5   18 hours ago   525MB
192.168.135.10:5000/mycentos7      1.0       1b99ed7bf2b5   18 hours ago   525MB
redis                              6         7faaec683238   5 days ago     113MB
redis                              latest    7faaec683238   5 days ago     113MB
nginx                              latest    87a94228f133   6 days ago     133MB
192.168.135.10:5000/myhelloworld   latest    feb5d9fea6a5   3 weeks ago    13.3kB
hello-world                        latest    feb5d9fea6a5   3 weeks ago    13.3kB
registry                           latest    b2cb11db9d3d   6 weeks ago    26.2MB

配置私有仓库(认证)

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


创建证书存储目录

[root@centos8 /]# mkdir -p /usr/local/registry/certs

生成自签名证书命令

[root@centos8 /]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/registry/certs/domain.key -x509 -days 365 -out /usr/local/registry/certs/domain.crt

openssl req :创建证书签名请求等功能;

-newkey :创建 CSR 证书签名文件和 RSA 私钥文件;

rsa:2048 :指定创建的 RSA 私钥长度为 2048;

-nodes :对私钥不进行加密;

-sha256 :使用 SHA256 算法;

-keyout :创建的私钥文件名称及位置;

-x509 :自签发证书格式;

-days :证书有效期;

-out :指定 CSR 输出文件名称及位置

生成自签名证书

通过 openssl 先生成自签名证书,运行命令以后需要填写一些证书信息,里面最关键的部分是:

Common Name (eg, your name or your server's hostname) []:

这里填写的是私有仓库的地址。如本文即填写192.168.135.10

[root@centos8 /]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/registry/certs/domain.key -x509 -days 365 -out /usr/local/registry/certs/domain.crt
Generating a RSA private key
...........................+++++
............................+++++
writing new private key to '/usr/local/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]:cn
State or Province Name (full name) []:cn
Locality Name (eg, city) [Default City]:cn
Organization Name (eg, company) [Default Company Ltd]:cn
Organizational Unit Name (eg, section) []:cn
Common Name (eg, your name or your server's hostname) []:192.168.135.10
Email Address []:68725032@qq.com
[root@centos8 /]# 

生成鉴权密码文件

# 创建存储鉴权密码文件目录
[root@centos8 /]# mkdir -p /usr/local/registry/auth
# 如果没有 htpasswd 功能需要安装 httpd
[root@centos8 /]# yum install -y httpd-tools
# 创建用户和密码 root 和 123
[root@centos8 /]# htpasswd -Bbn root 123 > /usr/local/registry/auth/htpasswd

htpasswd 是 apache http 的基本认证文件,使用 htpasswd 命令可以生成用户及密码文件。

创建私有仓库容器

先把之前创建的无认证的容器删掉

[root@centos8 auth]# docker stop registry
registry
[root@centos8 auth]# docker rm registry 
registry
docker run -id --name registry -p 5000:5000 \
-v /root/mydate/docker_registry:/var/lib/registry \
-v /usr/local/registry/certs:/certs \
-v /usr/local/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
[root@centos8 auth]# docker run -id --name registry -p 5000:5000 \
> -v /root/mydate/docker_registry:/var/lib/registry \
> -v /usr/local/registry/certs:/certs \
> -v /usr/local/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
b6a53df1dfe60ac2ca77b278a315abc59cd20b2a378dbd7cea6d18ddeac92dca
[root@centos8 auth]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
b6a53df1dfe6   registry   "/entrypoint.sh /etc…"   4 minutes ago   Up 4 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

推送镜像至私有仓库失败

先给镜像设置标签

再将镜像推送至私有仓库

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname
[root@centos8 docker_registry]#docker tag hello-world:latest 192.168.135.10:5000/myhelloworld
[root@centos8 auth]# docker push 192.168.135.10:5000/myhelloworld
Using default tag: latest
The push refers to repository [192.168.135.10:5000/myhelloworld]
e07ee1baac5f: Preparing 
no basic auth credentials

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

登录账号

通过 docker login ip:port 命令输入账号密码登录私有仓库

[root@centos8 auth]# docker login 192.168.135.10:5000
Username: root
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

推送镜像至私有仓库成功

再次 push 镜像,发现已经可以推送成功了

[root@centos8 auth]# docker push 192.168.135.10:5000/myhelloworld
Using default tag: latest
The push refers to repository [192.168.135.10:5000/myhelloworld]
e07ee1baac5f: Layer already exists 
latest: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525

退出账号

通过 docker logout ip:port命令退出账号

[root@centos8 auth]# docker logout 192.168.135.10:5000
Removing login credentials for 192.168.135.10:5000
相关实践学习
通过ACR快速部署网站应用
本次实验任务是在云上基于ECS部署Docker环境,制作网站镜像并上传至ACR镜像仓库,通过容器镜像运行网站应用,网站运行在Docker容器中、网站业务数据存储在Mariadb数据库中、网站文件数据存储在服务器ECS云盘中,通过公网地址进行访问。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
1月前
|
JavaScript Docker 容器
使用Docker多阶段构建优化镜像大小
使用Docker多阶段构建优化镜像大小
272 100
|
1月前
|
缓存 安全 Linux
优化Docker镜像大小的多阶段构建实践
优化Docker镜像大小的多阶段构建实践
230 99
|
1月前
|
缓存 Docker 容器
优化Docker镜像大小的五个实用技巧
优化Docker镜像大小的五个实用技巧
214 98
|
1月前
|
安全 Go Docker
使用Docker多阶段构建优化镜像大小
使用Docker多阶段构建优化镜像大小
|
16天前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
164 5
|
6月前
|
Docker 容器 Perl
云效flow构建docker镜像更换apt源为阿里镜像源
在 Dockerfile 中添加命令以更换 Debian 源为阿里云镜像,加速容器内软件包下载。核心命令通过 `sed` 实现源地址替换,并更新 apt 软件源。其中 `cat` 命令用于验证替换是否成功,实际使用中可删除该行。
1335 32
|
1月前
|
Java Docker 容器
使用Docker多阶段构建优化镜像大小
使用Docker多阶段构建优化镜像大小
93 8
|
2月前
|
缓存 Ubuntu Docker
Ubuntu环境下删除Docker镜像与容器、配置静态IP地址教程。
如果遇见问题或者想回滚改动, 可以重启系统.
192 16
kde
|
3月前
|
Docker 容器 应用服务中间件
Docker 客户端是如何拉取镜像的?
Docker客户端拉取镜像的过程遵循Docker Registry HTTP API V2规范,主要分为解析镜像名、鉴权、获取Manifest、拉取Layers及本地合并五个步骤。它与Docker Hub、Harbor等仓库通信,确保镜像正确下载和构建。
kde
619 59

热门文章

最新文章