一.软件环境
宿主机操作系统:CentOS Linux release 7.5
虚拟机工具:VMware® Workstation 10
应用容器引擎:Docker version 19.03.7
二.任务背景
所谓私有仓库,也就是在本地(局域网)搭建的一个类似公共仓库的东西,搭建好之后,我们可以将镜像提交到私有仓库中。这样我们既能使用 Docker 来运行我们的项目镜像,也避免从公共仓库中来取来拿所暴露出去的风险;利用Docker Registry快速搭建私有镜像仓库。
三.操作步骤
仓库地址:192.168.153.141
#下载registry镜像
[root@docker docker]# docker pull registry
#创建registry容器并启动
[root@docker docker]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000
--restart=always --name registry registry
b697d68647a95e01ee09115c3b9c035a393cac7e51f96e2e58c1896b500e8cfb
[root@docker docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b697d68647a9 registry "/entrypoint.sh /etc…" 40 seconds ago Up 19 seconds 0.0.0.0:5000->5000/tcp registry
客户端:192.168.153.142
#测试,查看镜像仓库中所有镜像
[root@redis_master docker]# curl http://192.168.153.141:5000/v2/_catalog
{"repositories":[]}
#私有镜像仓库管理
#配置私有仓库:
# vi /etc/docker/dameon.jsp
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"],
"insecure-registries":["192.168.153.141:5000"]
}
~
# systemctl restart docker
#下载镜像
# docker pull nginx:1.12
#打标签
# docker tag nginx:1.12 192.168.153.142:5000/centos:7
# docker tag nginx:1.12 192.168.153.141:5000/nginx:1.12
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.153.141:5000/nginx 1.12 4037a5562b03 22 months ago 108 MB
192.168.153.142:5000/centos 7 4037a5562b03 22 months ago 108 MB
docker.io/nginx 1.12 4037a5562b03 22 months ago 108 MB
#上传镜像到仓库
# docker push 192.168.153.141:5000/nginx:1.12
The push refers to a repository [192.168.153.141:5000/nginx]
4258832b2570: Pushed
683a28d1d7fd: Pushed
d626a8ad97a1: Pushed
1.12: digest: sha256:09e210fe1e7f54647344d278a8d0dee8a4f59f275b72280e8b5a7c18c560057f size: 948
# docker tag nginx:1.12 192.168.153.141:5000/centos:7
#查看仓库镜像
[root@redis_master /]# curl http://192.168.153.141:5000/v2/_catalog
{"repositories":["centos","nginx"]}
#列出镜像标签
[root@redis_master /]# curl http://192.168.153.141:5000/v2/nginx/tags/list
{"name":"nginx","tags":["1.12"]}
# curl http://192.168.153.141:5000/v2/centos/tags/list
{"name":"centos","tags":["7"]}
#从本地仓库(192.168.153.141:5000)下载镜像
#并创建nginx容器
[root@redis_master /]# docker run -itd --name nginx -p 88:80 192.168.153.141:5000/nginx:1.12
0c75bba5a04eb075d432f4409923007a4f8c29b4fb987475d37cae4eeccb303b
#查看正在运行的容器
[root@redis_master /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c75bba5a04e 192.168.153.141:5000/nginx:1.12 "nginx -g 'daemon ..." 40 seconds ago Up 38 seconds 0.0.0.0:88->80/tcp nginx