摘要:本文介绍如何将镜像发布到阿里云镜像仓库和本地私有仓库,以及本地私有仓库部署。

image-20220728165737928

一、发布镜像到阿里云镜像仓库 🍒

1、配置阿里云镜像仓库

(1)配置容器镜像服务

登录阿里云官网,找到容器镜像服务,如果没有则手动创建一个,如下图所示:

image-20220726141408836

(2)创建命名空间

image-20220726142356841

(3)创建镜像仓库和仓库代码源

image-20220726141936519

image-20220726142023303

2、上传本地镜像至阿里云镜像仓库

示例:将本地镜像 myubuntu:v1 推送到阿里云仓库

(1)登录阿里云Docker Regist

1
2
3
4
5
6
7
[root@localhost ~]# docker login --username=whbblog registry.cn-hangzhou.aliyuncs.com
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

(2)制作镜像 tag

1
2
3
4
5
6
7
8
9
10
11
12
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu v1 a7a511fae0fb 4 hours ago 178MB

# 镜像 Tag
[root@localhost ~]# docker tag a7a511fae0fb registry.cn-hangzhou.aliyuncs.com/whb_ns/myubuntu:latest

# 查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/whb_ns/myubuntu latest a7a511fae0fb 4 hours ago 178MB

(3)将镜像推送到 阿里云镜像仓库

1
docker push registry.cn-hangzhou.aliyuncs.com/whb_ns/myubuntu:latest

(4)从阿里云镜像仓库拉取镜像到本地

1
docker pull registry.cn-hangzhou.aliyuncs.com/whb_ns/myubuntu:latest

二、发布镜像到本地私有镜像仓库 🍉

1、私有仓库搭建

(1)私有仓库镜像下载

1
docker pull registry

(2)运行私有仓库镜像

1
docker run -d -p 5000:5000 -v /data/myregistry:/tmp/registry --privileged=true registry

(3)修改配置文件

1
2
3
4
5
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://6l7dxkas.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.6.80:5000"]
}

注意:配置文件格式为json格式,条目之间要有空格分隔。

(4)重启 docker

1
systemctl restart docker

2、上传本地镜像至本地仓库

示例:将本地镜像 nginx:latest 推送到阿里云仓库

(1)制作镜像tag

1
2
3
4
5
6
docker tag nginx:latest 192.168.6.80:5000/mynginx:1.2

# 查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.6.80:5000/mynginx 1.2 605c77e624dd 7 months ago 141MB

(2)推送镜像至本地私有镜像仓库

1
docker push 192.168.6.80:5000/mynginx:1.2

(3)查看本地私有镜像仓库镜像

1
2
[root@localhost ~]# curl 192.168.6.80:5000/v2/_catalog
{"repositories":["mynginx"]}

(4)查看私有仓库镜像 tag

1
2
[root@localhost ~]# curl -X GET http://192.168.6.80:5000/v2/mynginx/tags/list
{"name":"mynginx","tags":["1.2"]}

(5) 从私有仓库拉取镜像到本地

1
docker pull 192.168.6.80:5000/mynginx:1.2