查看原文
其他

Linux下Docker私有仓库1:部署

李逸皓 运维book思议 2023-10-21

先放个链接,万一有人关注呢

优质文章推荐

↓ ↓ ↓ ↓ ↓

Linux系统监控工具推荐

CentOS再见!拥抱Rocky ro Alma

一文搞懂Linux下的神级编辑器:Vim

Docker常用命令总结:运维必知必会

Linux系统文本三剑客:Sed命令

Docker镜像通常保存在docker Hub,docker Hub是目前最大的Docker镜像公有仓库,由Docker官方人员进行维护,其中的镜像可供所有用户下载使用。在生产环境中,通常公司会构建一些符合公司业务需求的镜像,却又因为商业机密而不得上传至docker Hub,只供公司内部人员使用。此时就需要在内网搭建一个Docker私有仓库,来存储公司内部的镜像,并且可以不受网络限制,快速地拉取或上传镜像。镜像为Docker容器的运行基础,容器是镜像的具体运行实例,镜像仓库为镜像提供了可靠的存储空间,镜像可以从公有或私有仓库拉取。

私有镜像仓库在企业中占有较高的使用率,而私有镜像仓库搭建技术就显得尤为重要。下面将通过示例讲述私有镜像仓库的搭建方式与过程。
本示例须要两台服务器,一个作为私有镜像仓库,另一个作为使用私有镜像仓库的Docker服务器。此处以Centos系统为例,安装并启动Docker。
docker Hub为用户提供了完美的仓库镜像,且本示例将使用docker Hub中的仓库镜像运行私有仓库。首先从docker Hub中拉取仓库镜像,示例代码如下:
[root@Docker-1 ~]# docker pull registryUsing default tag: latestlatest: Pulling from library/registryc87736221ed0: Pull complete 1cc8e0bb44df: Pull complete 54d33bcb37f5: Pull complete e8afc091c171: Pull complete b4541f6d3db6: Pull complete Digest: sha256:db8e07b1da92e1774458798a018512d71d869887d80b13cf126acda20122e41eStatus: Downloaded newer image for registry:latest[root@Docker-1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEregistry latest f32a97de94e1 5 weeks ago 25.8MB
上述示例中,成功拉取了Docker仓库的镜像。
下面将仓库镜像运行成为容器,示例代码如下:
[root@Docker-1 ~]# docker run -it -d -p 5000:5000 \> --restart=always \> --name registry registry84e48495b8a18b151e128b8698cf776a5e9aab40c57ed15bb055835a407ef1db
上述示例中,将仓库镜像运行成为了仓库容器,并映射了宿主机的5000端口,供Docker镜像的上传与下载。其中,--restart=always表示容器停止时自动重启,这条参数常用于生产环境中。
下面将镜像上传至刚刚创建的镜像仓库中,示例代码如下:
[root@Docker-1 ~]# docker pull busyboxUsing default tag: latestlatest: Pulling from library/busyboxfc1a6b909f82: Pull complete Digest: sha256:577311505bc76f39349a2d389d32c7967ca478de918104126c10aa0eb7f101fdStatus: Downloaded newer image for busybox:latest #下载一个busybox镜像做测试使用 [root@Docker-1 ~]# docker tag busybox 192.168.56.146:5000/busybox:latest #修改镜像的tag,使其指向到私有仓库 [root@Docker-1 ~]# docker push 192.168.56.146:5000/busybox:latestError response from daemon: Get https://192.168.56.146:5000/v2/: http: server gave HTTP response to HTTPS client
上述示例中,拉取了一个busybox镜像,并为其添加了tag标识,在尝试将镜像推送至私有仓库时,发生了报错。
这是因为Docker默认支持HTTPS协议,命令行中使用的是HTTP协议。这就需要修改Docker的启动参数,使之允许以HTTP协议工作,示例代码如下:
[root@Docker-1 ~]# cat /usr/lib/systemd/system/Docker.service ...... [Service]Type=notify# the default is not to use systemd for cgroups because the delegate issues still# exists and systemd currently does not support the cgroup feature set required# for containers run by DockerExecStart=/usr/bin/Dockerd --insecure-registry 192.168.56.146:5000ExecReload=/bin/kill -s HUP $MAINPIDTimeoutSec=0RestartSec=2 Restart=always ...... #修改配置文件,如上代码所示:在ExecStart=/usr/bin/Dockerd后添加--insecure-registry IP:5000代码段[root@Docker-1 ~]# systemctl daemon-reload [root@Docker-1 ~]# systemctl restart Docker
上述示例中,修改了Docker配置文件中的启动参数,并重启Docker。
下面接着尝试将镜像推送至私有仓库,示例代码如下:
[root@Docker-1 ~]# docker push 192.168.56.146:5000/busybox:latestThe push refers to repository [192.168.56.146:5000/busybox]0b97b1c81a32: Pushed latest: digest: sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4 size: 527
上述示例中,成功将镜像推送到私有仓库中。
下面查看私有仓库中的镜像,并拉取其中的镜像,示例代码如下:
[root@Docker-1 ~]# curl -X GET http://192.168.56.146:5000/v2/_catalog{"repositories":["busybox"]} #使用curl工具查看,可以看到仓库中有一个busybox镜像[root@Docker-1 ~]# docker rmi busyboxUntagged: busybox:latestUntagged: busybox@sha256:577311505bc76f39349a2d389d32c7967ca478de918104126c10aa0eb7f101fd[root@Docker-1 ~]# docker rmi 192.168.56.146:5000/busyboxUntagged: 192.168.56.146:5000/busybox:latestUntagged: 192.168.56.146:5000/busybox@sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4Deleted: sha256:af2f74c517aac1d26793a6ed05ff45b299a037e1a9eefeae5eacda133e70a825Deleted: sha256:0b97b1c81a3200e9eeb87f17a5d25a50791a16fa08fc41eb94ad15f26516ccea[root@Docker-1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEregistry latest f32a97de94e1 5 weeks ago 25.8MB#删除本地的busybox镜像,可以看到本地已经没有busybox镜像,下面尝试从私有仓库中拉取busybox镜像[root@Docker-1 ~]# docker pull 192.168.56.146:5000/busyboxUsing default tag: latestlatest: Pulling from busyboxfc1a6b909f82: Pull complete Digest: sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4Status: Downloaded newer image for 192.168.56.146:5000/busybox:latest[root@Docker-1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE192.168.56.146:5000/busybox latest af2f74c517aa 13 days ago 1.2MBregistry latest f32a97de94e1 5 weeks ago 25.8MB
上述示例中,删除了本地原来的镜像,并成功从私有仓库拉取到了镜像。由于使用的是内网环境,所以下载速度很快。
如果想要在其他宿主机上使用该仓库,只需要修改配置文件,重启Docker服务就可以。下面通过另外一台服务器拉取私有仓库中的镜像,先修改Docker配置文件,再重新读取并重新启动Docker,最后拉取镜像,示例代码如下:
[root@Docker-2 ~]# docker pull 192.168.56.146:5000/busyboxUsing default tag: latestlatest: Pulling from busyboxfc1a6b909f82: Pull complete Digest: sha256:f79f7a10302c402c052973e3fa42be0344ae6453245669783a9e16da3d56d5b4Status: Downloaded newer image for 192.168.56.146:5000/busybox:latest[root@Docker-2 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE192.168.56.146:5000/busybox latest af2f74c517aa 13 days ago 1.2MB
上述示例中,成功从私有仓库中下载了busybox镜像。
如此,一个简单私有仓库就搭建好了,但安全系数较低,镜像保存在容器中,容器被删除后,私有仓库以及仓库中的镜像也会一并删除,数据无法保存。

来不及解释了,快上车!(进群看公告)

欢迎新的小伙伴加入!在这里,我们鼓励大家积极参与群内讨论和交流,分享自己的见解和经验,一起学习和成长。同时,也欢迎大家提出问题和建议,让我们不断改进和完善这个平台。

                  ↓↓↓ 点个在看,你最好看!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存