CentOS7.2服务器上搭建Docker私有镜像仓库操作示例
程序员文章站
2024-01-31 23:14:58
本文实例讲述了centos7.2服务器上搭建docker私有镜像仓库操作。分享给大家供大家参考,具体如下:
鉴于国内pull镜像的速度较慢,很有必要搭建docker私...
本文实例讲述了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 ca subjectaltname = 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/ubuntu using default tag: latest trying to pull repository 192.168.120.128:5000/ubuntu ... latest: pulling from 192.168.120.128:5000/ubuntu digest: 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/ubuntu using default tag: latest trying to pull repository 192.168.120.128:5000/ubuntu ... latest: pulling from 192.168.120.128:5000/ubuntu b6f892c0043b: pull complete 55010f332b04: pull complete 2955fb827c94: pull complete 3deef3fcbd30: pull complete cf9722e506aa: pull complete digest: sha256:382452f82a8bbd34443b2c727650af46aced0f94a44463c62a9848133ecb1aa8
希望本文所述对大家docker容器使用有所帮助。