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
相关实践学习
通过workbench远程登录ECS,快速搭建Docker环境
本教程指导用户体验通过workbench远程登录ECS,完成搭建Docker环境的快速搭建,并使用Docker部署一个Nginx服务。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
3天前
|
Docker 容器
【Docker】掌握 Docker 镜像操作:从基础到进阶
【Docker】掌握 Docker 镜像操作:从基础到进阶
|
3天前
|
存储 安全 持续交付
【Docker 专栏】Docker 镜像的版本控制与管理
【5月更文挑战第9天】本文探讨了Docker镜像版本控制与管理的重要性,包括可重复性、回滚能力、协作开发和持续集成。常用方法有标签、构建参数和版本控制系统。管理策略涉及定期清理、分层管理和镜像仓库。语义化标签、环境变量和配置文件在版本控制中有应用。版本系统与Docker结合能跟踪历史和促进协作。注意点包括优化镜像大小、确保安全性和兼容性。案例分析和未来趋势展示了持续发展的镜像管理技术,为Docker应用的稳定与进步保驾护航。
【Docker 专栏】Docker 镜像的版本控制与管理
|
3天前
|
Docker 容器
docker从指定repo拉取镜像
docker从指定repo拉取镜像
|
1天前
|
存储 Linux Docker
CentOS7修改Docker容器和镜像默认存储位置
CentOS7修改Docker容器和镜像默认存储位置
|
3天前
|
存储 安全 开发者
如何删除 Docker 镜像、容器和卷?
【5月更文挑战第11天】
19 2
如何删除 Docker 镜像、容器和卷?
|
3天前
|
运维 安全 Docker
【Docker 专栏】Docker 镜像安全扫描与漏洞修复
【5月更文挑战第9天】Docker技术在软件开发和部署中带来便利,但其镜像安全问题不容忽视。本文探讨了Docker镜像安全扫描与漏洞修复,强调了镜像安全对应用和系统的重要性。文中介绍了静态和动态扫描方法,列举了软件漏洞、配置漏洞和恶意软件等常见安全问题,并提到了Clair和Trivy等扫描工具。修复策略包括更新软件、调整配置和重建镜像。此外,加强安全意识、规范镜像制作流程和定期扫描是管理建议。未来,将持续面对新的安全挑战,需持续研究和完善安全技术。
【Docker 专栏】Docker 镜像安全扫描与漏洞修复
|
3天前
|
Java Linux 数据安全/隐私保护
Docker自定义JDK镜像并拉取至阿里云镜像仓库全攻略
Docker自定义JDK镜像并拉取至阿里云镜像仓库全攻略
|
3天前
|
存储 弹性计算 运维
Docker数据集与自定义镜像:构建高效容器的关键要素
Docker数据集与自定义镜像:构建高效容器的关键要素
|
3天前
|
存储 缓存 运维
【Docker 专栏】Docker 镜像的分层存储与缓存机制
【5月更文挑战第8天】Docker 镜像采用分层存储,减少空间占用并提升构建效率。每个镜像由多个层组成,共享基础层(如 Ubuntu)和应用层。缓存机制加速构建和运行,通过检查已有层来避免重复操作。有效管理缓存,如清理无用缓存和控制大小,可优化性能。分层和缓存带来资源高效利用、快速构建和灵活管理,但也面临缓存失效和层管理挑战。理解这一机制对开发者和运维至关重要。
【Docker 专栏】Docker 镜像的分层存储与缓存机制
|
3天前
|
数据库 Docker 容器
【Docker 专栏】使用 Dockerfile 自动化构建 Docker 镜像
【5月更文挑战第8天】Dockerfile是构建Docker镜像的关键,它包含一系列指令,用于描述应用运行环境及所需软件包。通过自动化构建,能提高效率、保证可重复性并提升灵活性。确定基础镜像、安装依赖、设置环境后,执行Dockerfile生成镜像,用于应用程序部署。虽然需要熟悉Docker技术和应用细节,但其带来的益处使其成为现代软件开发和部署的重要工具。
【Docker 专栏】使用 Dockerfile 自动化构建 Docker 镜像