第六章 本地镜像发布到私有库

本地镜像发布到私有库流程

在这里插入图片描述

是什么

Docker Registry

1 官方Docker Hub地址:https://hub.docker.com/,中国大陆访问太慢了且准备被阿里云取代的趋势,不太主流。

2 Dockerhub、阿里云这样的公共镜像仓库可能不太方便,涉及机密的公司不可能提供镜像给公网,所以需要创建一个本地私人仓库供给团队使用,基于公司内部项目构建镜像。

Docker Registry是官方提供的工具,可以用于构建私有镜像仓库
将本地镜像推送到私有库

1、下载镜像Docker Registry

docker pull registry
[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete 
0d96da54f60b: Pull complete 
5b27040df4a2: Pull complete 
e2ead8259a04: Pull complete 
3790aef225b9: Pull complete 
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   About an hour ago   176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago        253MB
registry                                                  latest       b8604a3fe854   4 months ago        26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago        72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago       104MB
[root@localhost ~]# 

2、运行私有库Registry,相当于本地有个私有Docker hub

 
docker run -d -p 5000:5000  -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
默认情况,仓库被创建在容器的/var/lib/registry目录下,建议自行用容器卷映射,方便于宿主机联调

说明:
-d 后台守护进程运行
-p 配置端口 (-p 主机端口:容器内端口)
-v 容器卷映射
[root@localhost ~]# docker run -d -p 5000:5000  -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
745bbd33400d0a0f994a86a9d36278bd824f5fc1a1839820ec325293e29ca675
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
745bbd33400d   registry   "/entrypoint.sh /etc…"   13 seconds ago   Up 12 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   keen_hypatia
[root@localhost ~]# 

3、案例演示创建一个新镜像,ubuntu安装ifconfig命令

从Hub上下载ubuntu镜像到本地并成功运行

原始的Ubuntu镜像是不带着ifconfig命令的

[root@localhost ~]# docker run -it ubuntu /bin/bash
root@c1137b7b7116:/# vim a.txt
bash: vim: command not found
root@c1137b7b7116:/# ifconfig
bash: ifcconfig: command not found
root@c1137b7b7116:/# 
docker容器内执行上述两条命令:
apt-get update
apt-get install net-tools
root@c1137b7b7116:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 15111  bytes 22750890 (22.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9083  bytes 494978 (494.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@c1137b7b7116:/# 

安装完成后,commit我们自己的新镜像

公式:
docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]
命令:在容器外执行,记得
docker commit -m="ifconfig cmd add" -a="zzyy" a69d7c825c4f zzyyubuntu:1.2
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
c1137b7b7116   ubuntu     "/bin/bash"              8 minutes ago    Up 8 minutes                                                charming_feynman
745bbd33400d   registry   "/entrypoint.sh /etc…"   12 minutes ago   Up 12 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   keen_hypatia
[root@localhost ~]# docker commit -m="ifconfig cmd add" -a="叆叇似流年" c1137b7b7116 zzyyubuntu:1.2
sha256:04ea4a10f57c5a38cba45e1926fb7df00d8f3c16936db79c7fd70fcb0ae93991
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED              SIZE
zzyyubuntu                                                1.2          04ea4a10f57c   About a minute ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   2 hours ago          176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago         253MB
registry                                                  latest       b8604a3fe854   4 months ago         26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago         72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago        104MB
[root@localhost ~]# 

启动我们的新镜像并和原来的对比

1 官网是默认下载的Ubuntu没有ifconfig命令

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
c1137b7b7116   ubuntu     "/bin/bash"              15 minutes ago   Up 15 minutes                                               charming_feynman
745bbd33400d   registry   "/entrypoint.sh /etc…"   19 minutes ago   Up 19 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   keen_hypatia
[root@localhost ~]# docker stop c1137b7b7116
c1137b7b7116
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED         SIZE
zzyyubuntu                                                1.2          04ea4a10f57c   5 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   2 hours ago     176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago    253MB
registry                                                  latest       b8604a3fe854   4 months ago    26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago    72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago   104MB

2我们自己commit构建的新镜像,新增加了ifconfig功能,可以成功使用。

[root@localhost ~]# docker run -it 04ea4a10f57c /bin/bash
root@ef051f466daa:/# ifcongig
bash: ifcongig: command not found
root@ef051f466daa:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@ef051f466daa:/# exit
exit
[root@localhost ~]# 

4、curl验证私服库上有什么镜像

 curl -XGET http://这里是Linux的IP地址(即主机的IP):5000/v2/_catalog
 
可以看到,目前私服库没有任何镜像上传过。。。。。。

先运行的是我们的私服库,我们要将zzyyubuntu这个具有ifconfig的镜像推送到私服库中

[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
zzyyubuntu                                                1.2          04ea4a10f57c   11 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   2 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
745bbd33400d   registry   "/entrypoint.sh /etc…"   26 minutes ago   Up 26 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   keen_hypatia
[root@localhost ~]# 
[root@localhost ~]# curl -XGET http://192.168.174.133:5000/v2/_catalog
{"repositories":[]}
[root@localhost ~]# 

5、将新镜像zzyyubuntu:1.2修改符合私服规范的Tag

 
按照公式: docker   tag   镜像:Tag   Host:Port/Repository:Tag
自己host主机IP地址,填写同学你们自己的,不要粘贴错误,O(∩_∩)O
使用命令 docker tag 将zzyyubuntu:1.2 这个镜像修改为192.168.111.162:5000/zzyyubuntu:1.2
 
docker tag  zzyyubuntu:1.2  192.168.111.162:5000/zzyyubuntu:1.2
 

[root@localhost ~]# docker tag  zzyyubuntu:1.2 192.168.174.133:5000/zzyyubuntu:1.2
[root@localhost ~]# 
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
192.168.174.133:5000/zzyyubuntu                           1.2          04ea4a10f57c   24 minutes ago   109MB
zzyyubuntu                                                1.2          04ea4a10f57c   24 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   2 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# 

6、修改配置文件使之支持http

由于docker的私服库做了安全加固,不支持http的推送所以我们要做个配置取消这个限制

vim /etc/docker/daemon.json

在这里插入图片描述

 
 别无脑照着复制,registry-mirrors 配置的是国内阿里提供的镜像加速地址,不用加速的话访问官网的会很慢。2个配置中间有个逗号 ','别漏了,这个配置是json格式的。2个配置中间有个逗号 ','别漏了,这个配置是json格式的。2个配置中间有个逗号 ','别漏了,这个配置是json格式的。
 
vim命令新增如下红色内容:
vim /etc/docker/daemon.json{  
"registry-mirrors": ["https://aa25jngu.mirror.aliyuncs.com"],  
"insecure-registries": ["192.168.174.133:5000"]
}
 
上述理由:docker默认不允许http方式推送镜像,通过配置选项来取消这个限制。====> 修改完后如果不生效,建议重启docker
 
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2022-04-09 13:14:44 CST; 27s ago
     Docs: https://docs.docker.com
 Main PID: 5709 (dockerd)
    Tasks: 11
   Memory: 32.7M
   CGroup: /system.slice/docker.service
           └─5709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.255011568+08:00" level=inf...rpc
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.255027608+08:00" level=inf...rpc
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.272865491+08:00" level=inf...y2"
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.312544928+08:00" level=inf...t."
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.472893288+08:00" level=inf...ss"
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.521535039+08:00" level=inf...e."
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.540693194+08:00" level=inf....14
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.540772093+08:00" level=inf...on"
4月 09 13:14:44 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
4月 09 13:14:44 localhost.localdomain dockerd[5709]: time="2022-04-09T13:14:44.592344856+08:00" level=inf...ck"
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# 

重启我们的私服库

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker run -d -p 5000:5000  -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
11dff3d7e4f66e261ec720fb709b5261b93a3a6696110978ed436699f6731b13
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
11dff3d7e4f6   registry   "/entrypoint.sh /etc…"   4 seconds ago   Up 3 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   sweet_davinci
[root@localhost ~]# 

7、push推送到私服库

docker push 192.168.174.133:5000/zzyyubuntu:1.2
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
192.168.174.133:5000/zzyyubuntu                           1.2          04ea4a10f57c   42 minutes ago   109MB
zzyyubuntu                                                1.2          04ea4a10f57c   42 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   2 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# docker push 192.168.174.133:5000/zzyyubuntu:1.2

8、curl验证私服库上有什么镜像2

[root@localhost ~]# curl -XGET http://192.168.174.133:5000/v2/_catalog
{"repositories":["zzyyubuntu"]}
[root@localhost ~]# 

9、pull到本地并运行

docker pull 192.168.174.133:5000/zzyyubuntu:1.2

docker run -it 镜像ID /bin/bash

先删除192.168.174.133:5000/zzyyubuntu和zzyyubuntu确保准确性

[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
192.168.174.133:5000/zzyyubuntu                           1.2          04ea4a10f57c   46 minutes ago   109MB
zzyyubuntu                                                1.2          04ea4a10f57c   46 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   3 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# docker rmi -f 192.168.174.133:5000/zzyyubuntu:1.2
Untagged: 192.168.174.133:5000/zzyyubuntu:1.2
Untagged: 192.168.174.133:5000/zzyyubuntu@sha256:a7082bf7411fbca9d8bb7b682d07a936a533c15f54abd0010f3aee1b60e4347f
[root@localhost ~]# curl -XGET http://192.168.174.133:5000/v2/_catalog
{"repositories":["zzyyubuntu"]}
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
zzyyubuntu                                                1.2          04ea4a10f57c   47 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   3 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# docker rmi -f zzyyubuntu:1.2
Untagged: zzyyubuntu:1.2
Deleted: sha256:04ea4a10f57c5a38cba45e1926fb7df00d8f3c16936db79c7fd70fcb0ae93991
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED         SIZE
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   3 hours ago     176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago    253MB
registry                                                  latest       b8604a3fe854   4 months ago    26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago    72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago   104MB

从私服下载zzyyubuntu:1.2

[root@localhost ~]# curl -XGET http://192.168.174.133:5000/v2/_catalog
{"repositories":["zzyyubuntu"]}
[root@localhost ~]# docker pull 192.168.174.133:5000/zzyyubuntu:1.2
1.2: Pulling from zzyyubuntu
7b1a6ab2e44d: Already exists 
5dd848fd80a9: Already exists 
Digest: sha256:a7082bf7411fbca9d8bb7b682d07a936a533c15f54abd0010f3aee1b60e4347f
Status: Downloaded newer image for 192.168.174.133:5000/zzyyubuntu:1.2
192.168.174.133:5000/zzyyubuntu:1.2
[root@localhost ~]# 
[root@localhost ~]# docker images
REPOSITORY                                                TAG          IMAGE ID       CREATED          SIZE
192.168.174.133:5000/zzyyubuntu                           1.2          04ea4a10f57c   53 minutes ago   109MB
registry.cn-hangzhou.aliyuncs.com/testshanghai/myubuntu   1.3          8d4088598f0b   3 hours ago      176MB
rabbitmq                                                  management   6c3c2a225947   3 months ago     253MB
registry                                                  latest       b8604a3fe854   4 months ago     26.2MB
ubuntu                                                    latest       ba6acccedd29   5 months ago     72.8MB
redis                                                     6.0.8        16ecd2772934   17 months ago    104MB
[root@localhost ~]# 

运行从私服库中拉去下来的192.168.174.133:5000/zzyyubuntu

[root@localhost ~]# docker run -it 192.168.174.133:5000/zzyyubuntu:1.2 /bin/bash
root@061e03ebfbe5:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@061e03ebfbe5:/# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值