CentOS7.2服务器上搭建Docker私有镜像仓库操作示例

时间:2021-05-19

本文实例讲述了CentOS7.2服务器上搭建Docker私有镜像仓库操作。分享给大家供大家参考,具体如下:

鉴于国内pull镜像的速度较慢,很有必要搭建docker私有或者本地镜像仓库。

安装docker

# yum -y install docker# systemctl start docker && systemctl enable docker

使用自签名进行安全认证

创建存放证书和密钥的certs目录

# mkdir -p /docker/certs# chcon -Rt svirt_sandbox_file_t /docker/certs/

修改/etc/pki/tls/openssl.cnf配置文件

在该文件的[ v3_ca ]配置项中添加镜像仓库IP地址:

[ v3_ca ]# Extensions for a typical CAsubjectAltName = IP:192.168.120.128

生成证书和密钥

# cd /docker && openssl req \-newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key \-x509 -days 365 -out certs/domain.crt# mkdir -p /etc/docker/certs.d/192.168.120.128:5000/# cp certs/domain.crt /etc/docker/certs.d/192.168.120.128\:5000/ca.crt

创建存放镜像文件的后端存储

# mkdir -p /docker/data/private_registry# chcon -Rt svirt_sandbox_file_t /docker/data/private_registry

重新启动docker daemon

# systemctl restart docker

启动私有镜像仓库

# docker run \-d \--name private_registry --restart=always \-u root \-p 5000:5000 \-v /docker/data/private_registry:/var/lib/registry \-v /docker/certs:/certs \-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \registry:2

Docker加速器

该加速器可在pull镜像较慢时配置实用。
复制代码 代码如下:# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://c282dc88.m.daocloud.io

测试

本地测试

# docker pull ubuntu# docker tag ubuntu 192.168.120.128:5000/ubuntu# docker pull 192.168.120.128:5000/ubuntuUsing default tag: latestTrying to pull repository 192.168.120.128:5000/ubuntu ...latest: Pulling from 192.168.120.128:5000/ubuntuDigest: sha256:382452f82a8bbd34443b2c727650af46aced0f94a44463c62a9848133ecb1aa8

远程测试

在另一台主机上执行以下命令进行测试:

# mkdir -p /etc/docker/certs.d/192.168.120.128:5000/# scp 192.168.120.128:/etc/docker/certs.d/192.168.120.128\:5000/ca.crt /etc/docker/certs.d/192.168.120.128\:5000/# systemctl restart docker# docker pull 192.168.120.128:5000/ubuntuUsing default tag: latestTrying to pull repository 192.168.120.128:5000/ubuntu ...latest: Pulling from 192.168.120.128:5000/ubuntub6f892c0043b: Pull complete55010f332b04: Pull complete2955fb827c94: Pull complete3deef3fcbd30: Pull completecf9722e506aa: Pull completeDigest: sha256:382452f82a8bbd34443b2c727650af46aced0f94a44463c62a9848133ecb1aa8

希望本文所述对大家Docker容器使用有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章