docker构建私有仓库(无密码)

时间:2024-02-21 15:01:07

一、搭建docker私有镜像仓库
1.1、运行registry容器
docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest

参数说明
-itd:在容器中打开一个伪终端进行交互操作,并在后台运行;
-v:把宿主机的/data/registry目录绑定 到 容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化;
-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
--restart=always:这是重启的策略,假如这个容器异常退出会自动重启容器;
--name registry:创建容器命名为registry,你可以随便命名;
registry:latest:这个是刚才pull下来的镜像;

1.2、测试镜像仓库中所有的镜像
[root@registry ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}

1.3、修改下镜像源并重启docker服务
[root@ansible-server Dockerfile]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["59.47.71.229:5000"]
}

#重启服务
[root@ansible-server Dockerfile]# systemctl restart docker

1.4、下载镜像,并打tag
[root@ansible-server Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ea7615425586 About an hour ago 633MB
nginx 1.12.1 c93692656106 About an hour ago 446MB
busybox latest 64f5d945efcc 2 weeks ago 1.2MB
mysql 5.6 73829d7b6139 2 weeks ago 256MB
centos 7 9f38484d220f 2 months ago 202MB
registry latest f32a97de94e1 2 months ago 25.8MB

#打tag
[root@ansible-server Dockerfile]# docker tag busybox:latest 59.47.71.229:5000/busybox:v1

#查看镜像
[root@ansible-server Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ea7615425586 About an hour ago 633MB
nginx 1.12.1 c93692656106 About an hour ago 446MB
59.47.71.229:5000/busybox v1 64f5d945efcc 2 weeks ago 1.2MB
busybox latest 64f5d945efcc 2 weeks ago 1.2MB
mysql 5.6 73829d7b6139 2 weeks ago 256MB
centos 7 9f38484d220f 2 months ago 202MB
registry latest f32a97de94e1 2 months ago 25.8MB

#推送到镜像仓库
[root@ansible-server Dockerfile]# docker push 59.47.71.229:5000/busybox:v1
The push refers to repository [59.47.71.229:5000/busybox]
d1156b98822d: Pushed

#删除本地镜像
[root@ansible-server Dockerfile]# docker rmi -f 59.47.71.229:5000/busybox:v1
Untagged: 59.47.71.229:5000/busybox:v1
Untagged: 59.47.71.229:5000/busybox@sha256:4fe8827f51a5e11bb83afa8227cbccb402df840d32c6b633b7ad079bc8144100

#查看镜像
[root@ansible-server Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ea7615425586 About an hour ago 633MB
nginx 1.12.1 c93692656106 About an hour ago 446MB
busybox latest 64f5d945efcc 2 weeks ago 1.2MB
mysql 5.6 73829d7b6139 2 weeks ago 256MB
centos 7 9f38484d220f 2 months ago 202MB
registry latest f32a97de94e1 2 months ago 25.8MB

#下载镜像
[root@ansible-server Dockerfile]# docker pull 59.47.71.229:5000/busybox:v1
v1: Pulling from busybox
Digest: sha256:4fe8827f51a5e11bb83afa8227cbccb402df840d32c6b633b7ad079bc8144100
Status: Downloaded newer image for 59.47.71.229:5000/busybox:v1

#查看镜像
[root@ansible-server Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> ea7615425586 About an hour ago 633MB
nginx 1.12.1 c93692656106 About an hour ago 446MB
59.47.71.229:5000/busybox v1 64f5d945efcc 2 weeks ago 1.2MB
busybox latest 64f5d945efcc 2 weeks ago 1.2MB
mysql 5.6 73829d7b6139 2 weeks ago 256MB
centos 7 9f38484d220f 2 months ago 202MB
registry latest f32a97de94e1 2 months ago 25.8MB

#列出所有镜像:
[root@ansible-server Dockerfile]# curl http://59.47.71.229:5000/v2/_catalog
{"repositories":["busybox"]}

#列出busybox镜像有哪些tag:
[root@ansible-server Dockerfile]# curl http://59.47.71.229:5000/v2/busybox/tags/list
{"name":"busybox","tags":["v1"]}