docker注册中心相关操作

相关命令详解

(1)push推送

  • 将镜像推送到由其名称或标签指定的仓库中。与pull命令相对。
[root@docker ~]# docker push --help
Usage:  docker push [OPTIONS选项] NAME仓库名[:TAG标签]
Push an image or a repository to a registry    # 推送一个镜像或仓库到注册中心
Options:
      --disable-content-trust   Skip image signing (default true)     # 跳过镜像签名 

# 例子:
[root@docker ~]# docker push 用户名/testdocker:22.03

(2)commit提交

  • 从容器创建一个新的镜像。
[root@docker ~]# docker commit --help
Usage:  docker commit [OPTIONS选项] CONTAINER容器 [REPOSITORY仓库名[:TAG标签]]
Create a new image from a containers changes
Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)    # 创建镜像过程中容器暂停

(3)login登录 和 logout退出

  • 使用 docker login 命令登录docker注册中心。
# 语法
[root@docker docker]# docker login --help
Usage:  docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.        # 如果server没有指定,默认从daemon获取。
Options:
  -p, --password string   Password                   # 密码
      --password-stdin    Take the password from stdin     # 标准输入输入密码
  -u, --username string   Username    # 用户名

# 示例:
[root@docker ~]# docker login --username=zzz(用户名) registry.cn-hangzhou.aliyuncs.com
  • 使用 docker logout 命令退出注册中心登录。
# 语法
[root@hqs docker]# docker logout --help
Usage:  docker logout [SERVER]
Log out from a Docker registry.
If no server is specified, the default is defined by the daemon.

# 示例:
[root@docker docker]# docker logout
Removing login credentials for https://index.docker.io/v1/

操作示例步骤

(1)docker hub 镜像上传

1.修改镜像镜像加速器

[root@docker ~]# cd /etc/docker/
[root@docker docker]# vi daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
[root@docker docker]# systemctl daemon-reload
[root@docker docker]# systemctl restart docker

2.登录docker hub官网注册账号

https://hub.docker.com/signup

3.登录账户,创建一个仓库

“Create Repository”——> 输入命名空间和仓库名——> “Description”填写描述信息——> “visibility”中选择是创建public仓库还是private仓库——> 点击“Create”创建仓库

4.登录Docker Hub

[root@docker docker]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you dont have a Docker ID, head over to https://hub.docker.com to create one.
Username: zzz             # 输入创建的用户名
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

5.构建镜像

[root@docker docker-hello]# docker build -t zzz/testdocker .
Sending build context to Docker daemon  865.3kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> 2322533b4210
Step 3/3 : CMD ["/hello"]
 ---> Running in 5994e2099b8d
Removing intermediate container 5994e2099b8d
 ---> 32fedb0d83ba
Successfully built 32fedb0d83ba
Successfully tagged zzz/testdocker:latest

6.为已存在的镜像重新设置标签

[root@docker docker-hello]# docker tag 32fedb0d83ba zzz/testdocker:22.03
[root@docker docker-hello]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
hqs2212586/testdocker   22.03               32fedb0d83ba        About a month ago   861kB
hqs2212586/testdocker   latest              32fedb0d83ba        About a month ago   861kB

7.推送镜像

[root@docker docker-hello]# docker push  zzz/testdocker:22.03
The push refers to repository [docker.io/zzz/testdocker]
88dc11111fec: Pushed 
22.03: digest: sha256:c9225ad48cb94c8f160e839187e81ce28f85dbff8316e7443879f5169f41cfb0 size: 527

8.在docker hub上检查推送的仓库

[仓库地址](https://hub.docker.com/repository/docker/zzz/testdocker)

(2)容器生成镜像提交docker hub

1.登录官网账户,创建一个仓库

“Create Repository”——> 输入命名空间和仓库名——> “Description”填写描述信息——> “visibility”中选择是创建public仓库还是private仓库——> 点击“Create”创建仓库

2.容器提交生成镜像

[root@docker docker-hello]# docker commit 4decee095428 zzz/ubuntu:22.03
sha256:a4b85b3fb73e23e75440430252477b77aee8927fdf9495160297b86dcf5f917c
[root@docker docker-hello]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
zzz/ubuntu       22.03               a4b85b3fb73e        1 seconds ago       72.8MB

3.推送镜像

[root@docker docker-hello]# docker push zzz/ubuntu:22.03
The push refers to repository [docker.io/zzz/ubuntu]
b954f8dfd459: Pushed 
9f54eef41275: Mounted from library/ubuntu 
22.03: digest: sha256:573bc189eba09934db466a2344edee5f46cd7d98fb765690b4ba7b1e9dd26623 size: 735

4.在docker hub上检查推送的仓库

[仓库地址](https://hub.docker.com/repository/docker/zzz/ubuntu)

(3)第三方Docker注册中心

  • 阿里云除了提供 Docker Hub 的镜像加速服务,还提供自己的仓库注册服务————容器镜像服务。容器镜像服务简化了Registry的搭建运维工作,支持多地域的镜像托管,并联合容器服务等云产品,打造云上使用Docker的一体化体验。
    阿里云容器镜像服务(简称 ACR)是提供安全的应用镜像托管能力,精确的镜像安全扫描功能,稳定的国内外镜像构建服务,便捷的镜像授权功能,方便用户进行镜像全生命周期管理。
  • 阿里云仓库的格式:域名/命名空间/仓库名称:标签。示例:registry.cn-hangzhou.aliyuncs.com/zzz/docker_test:22.03

1.登录aliyun平台,注册登录

[阿里云官网地址](https://www.aliyun.com)

2.在阿里云控制台创建仓库

打开阿里云控制台——> 产品与服务列表——> 弹性计算里的容器镜像服务,进入容器镜像服务控制台
点击“个人实例”——> 点击左侧“镜像仓库”——> 点击“创建镜像仓库”——> 输入仓库名称、选择仓库类型、输入仓库的摘要或描述等信息——> 点击“下一步”——> 选择“本地仓库”——> 点击“创建镜像仓库”

3.设置registry登录密码

使用docker login通过密码登录镜像服务实例,需要先设置或获取临时密码或固定密码。
容器镜像服务——> 个人实例——> 仓库管理菜单里的访问凭证——> 设置固定密码

4.登录阿里云Docker Registry

[root@docker docker-hello]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@docker docker-hello]# docker login --username=zzz 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

5.构建镜像

[root@docker docker-hello]# docker build -t registry.cn-hangzhou.aliyuncs.com/zzz(命名的空间)/docker_test:22.03 .
Sending build context to Docker daemon  865.3kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : COPY hello /
 ---> Using cache
 ---> 846e09a540e6
Step 3/3 : CMD ["/hello"]
 ---> Using cache
 ---> a5dbf1d93254
Successfully built a5dbf1d93254
Successfully tagged registry.cn-hangzhou.aliyuncs.com/zzz/docker_test:22.03

6.将镜像推送到Registry

[root@docker docker-hello]#  docker push registry.cn-hangzhou.aliyuncs.com/zzz/docker_test:22.03
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/zzz/docker_test]
88dc11111fec: Pushed 
22.03: digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b size: 527

7.验证镜像上传

访问容器镜像服务控制台——> 个人实例——> 镜像仓库——> 点击仓库名docker_test——> 点击镜像版本 可以看到我们上传的镜像。

(4)自建Docker注册中心

  • 因为安全和网络问题,用户可以建立自己的注册中心提供镜像仓库注册服务

1.基于容器安装运行Registry

  • 使用的 Docker Registry工具 已经开源,可以在Docker Hub上下载镜像启动容器。
[root@docker ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
3d2430473443: Pull complete 
5bf98bf80c2f: Pull complete 
950c199aa45b: Pull complete 
92504897768b: Pull complete 
c6488f74dce8: Pull complete 
Digest: sha256:65be6503496c34ec234e89a831ca248b18c2e04c800d9d74af73866e3cda8578
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@docker ~]# docker run -d -p 5000:5000 --restart=always --name myregistry -v /opt/data/registry:/var/lib/registry registry
3cac187c798b08d12da5f292a03621e2e27021763481e9313b6f7de57a722354
[root@docker ~]# docker ps -a
CONTAINER ID     IMAGE        COMMAND                  CREATED             STATUS              PORTS                    NAMES
3cac187c798b     registry     "/entrypoint.sh /etc…"   30 seconds ago      Up 29 seconds       0.0.0.0:5000->5000/tcp   myregistry

# 验证刚建立的注册中心
[root@docker ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}     # 此结果说明运行正常,注册中心还没有任何镜像

2.将镜像上传到自建的注册中心

  • 要注意自建的注册中心仓库标签格式:[主机:端口]/仓库名称:标签
# 1.给已有的镜像打标签符合自建注册中心格式
[root@docker ~]# docker tag ubuntu 127.0.0.1:5000/ubuntu:v1
[root@docker ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
zzz/ubuntu                                       22.03               93a86f5131ec        5 hours ago         72.8MB
127.0.0.1:5000/ubuntu                                   v1                  ba6acccedd29        1 months ago        72.8MB
ubuntu                                                  latest              ba6acccedd29        1 months ago        72.8MB
[root@docker ~]# docker tag zzz/testdocker 127.0.0.1:5000/testdocker:v1

# 2.执行镜像上传
[root@docker ~]# docker push 127.0.0.1:5000/ubuntu:v1
The push refers to repository [127.0.0.1:5000/ubuntu]
9f54eef41275: Pushed 
v1: digest: sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 size: 529
[root@docker ~]# docker push  127.0.0.1:5000/testdocker:v1
The push refers to repository [127.0.0.1:5000/testdocker]
88dc11111fec: Pushed 
v1: digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b size: 527

# 3.执行测试
[root@docker ~]# curl http://127.0.0.1:5000/v2/_catalog
{"repositories":["testdocker","ubuntu"]}

3.从自建注册中心下载镜像

[root@docker ~]# docker rmi 127.0.0.1:5000/testdocker:v1
Untagged: 127.0.0.1:5000/testdocker:v1
Untagged: 127.0.0.1:5000/testdocker@sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b
[root@docker ~]# docker pull 127.0.0.1:5000/testdocker:v1
v1: Pulling from testdocker
Digest: sha256:b1d135c19b018cfbbba32085948cb125507b508549a8296568c25a81f5b0709b
Status: Downloaded newer image for 127.0.0.1:5000/testdocker:v1
127.0.0.1:5000/testdocker:v1
[root@docker ~]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
127.0.0.1:5000/testdocker                               v1                  a5dbf1d93254        5 hours ago         861kB

4.配置注册中心地址

本地服务器情况下使用127.0.0.1或者localhost作为注册中心地址都是可以的。
但如果使用主机的域名或IP地址提供服务,会报错,这是因为1.3版本后,docker注册中心默认使用的https,但是搭建私有注册中心默认使用的是http。

解决方法:修改docker客户端/etc/docker/daemon.json文件,将地址添加到insecure-registries列表中,允许不安全的通信即可。

# 示例
{
  "insecure-registries":["192.168.88.101:5000"]
}
# 保存后重启docker
systemctl restart docker
posted @ 2022-03-31 13:43  郑琰  阅读(286)  评论(0编辑  收藏  举报
#

# #