关于 Docker Registry (镜像仓库)

什么是镜像仓库

概念

镜像仓库(Docker Registry)负责存储、管理和分发镜像,并提供了登录认证能力,建立了仓库的索引。

镜像仓库管理多个 Repository,Repository 通过命名来区分。每个 Repository 包含一个或多个镜像,镜像通过镜像名称和标签(Tag)来区分(也就是每个镜像的每一个小版本)。

一个容器镜像又包含 元数据 和 blob。

元数据就是由 dockerfile 构建出来的描述文件,主要记录了一个容器镜像有多少层,每一层里有什么内容。

blob 中存储的就是真正的可执行文件,也是真正占有空间的东西。

  • 镜像仓库(Registry):要从哪一个镜像仓库拉取镜像,通常通过 DNS 或 IP 地址来确定一个镜像仓库如 hub.docker.com;一个 Registry 中可以存在多个 Repository,Repository 又可分为顶层仓库和用户仓库(用户仓库名称格式为:用户名/仓库名,每个仓库可以包含多个 Tag,每个 Tag 对应一个镜像)。

  • Repository:由某特定的 docker 镜像的所有迭代版本组成的镜像仓库。

  • 镜像名称(name)+ 标签(tag):如 nginx:latest。

  • 认证能力:提供用户注册、登录、登出能力。

  • 索引:提供镜像的索引信息,方便检索。

镜像仓库分类

按是否对外开放划分:

  1. 公有仓库:如阿里云、dockerhub 等被放到公有网络上,不用登录就可以下载,可免费供大家使用的镜像。

  2. 私有仓库:不对外开放,一般被放在私有网络上,只有内部人员可以使用。

按供应商和面向群体划分:

  1. sponsor registry(赞助):第三方的 registry,供客户和 docker 社区版使用。

  2. mirror registry(镜像):第三方的 registry,只让客户使用,例如阿里云等。

  3. vender registry(供应商):由发布 docker 镜像的供应商提供的 registry,例如 Google 和 Redhat 提供的镜像仓库服务。

  4. private registry:通过没有防火墙和额外的安全层的私有实体提供的 registry,仅供内部使用。

镜像仓库工作机制

镜像仓库使用流程

  • 通过 docker login 登录仓库

  • Docker pull 拉取需要的镜像

  • 通过 dockerfile 或 commit 等方式制作完镜像后通过 docker push 上传到仓库

镜像仓库如何在实际研发中使用

首先要知道,一个项目或系统,从开始研发到上线使用的过程中,要经过几个不同的环境。

  • 开发环境:开发人员使用的一套环境。

  • 测试环境:需求开发完成后,发布到供测试人员进行测试的环境。

  • 预发布环境:版本测试完成后,发布到和生产类似的环境,提前模拟生产发布。

  • 生产环境:真正面向客户的环境。

针对不同的阶段要有不同的环境,才能得到想要的效果,所以也就需要不同的镜像来满足不同阶段对于环境的不同需求。

Docker Registry 中的镜像通常由开发人员制作,然后推送到公有或私有的 Registry 上保存,供其他人员使用,例如部署到生产环境。

镜像仓库的拉取机制

启动容器时,docker daemon 会试图从本地获取相关的镜像。本地镜像不存在时,其将从 Registry 中下载该镜像并保存到本地。

搭建 nginx 服务

了解 web 服务器和 Nginx

Web 服务器,一般是指“网站服务器”,是指驻留于互联网上某种类型计算机的程序。Web 服务器可以向 Web 浏览器等客户端提供文档,也可以防止网站文件,让全世界浏览,更可以放置数据文件,让全世界下载。

Nginx 作为 web 服务器可以向各种浏览器等客户端提供浏览服务,比如通过手机、电脑、平板可以访问百度来实现对 web 服务器的访问。

正向代理

由于防火墙的原因,我们并不能直接访问外网,可以借助 VPN 来实现,这就是一个正向代理的例子。

正向代理“代理”的是客户端,而客户端是知道目标的,目标是不知道客户端是通过 VPN 访问的。

反向代理

当我们在外网访问百度的时候,其实会进行一个转发,代理到内网,这就是所谓的反向代理。

也就是说,反向代理“代理”的是服务器端,而且这个过程对于客户端而言是透明的。

开始搭建 Nginx

本来按理来说,应该先介绍搭建过程中涉及到的各种命令,但是,我懒得写了...读者如果遇到了自己不熟悉的命令,请先跳转百度一下!

先找到一个合适的 nginx

root@hcss-ecs-3f38:~# docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL
nginx                              Official build of Nginx.                        19996     [OK]
unit                               Official build of NGINX Unit: Universal Web …   32        [OK]
nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers fo…   92        
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  154       
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter for NGINX and NGIN…   42        
nginx/nginx-ingress-operator       NGINX Ingress Operator for NGINX and NGINX P…   2         
nginx/unit                         This repository is retired, use the Docker o…   63        
nginxinc/nginx-s3-gateway          Authenticating and caching gateway based on …   6         
nginx/nginx-quic-qns               NGINX QUIC interop                              1         
nginxinc/amplify-agent             NGINX Amplify Agent docker repository           1         
nginxinc/ingress-demo              Ingress Demo                                    4         
nginxproxy/nginx-proxy             Automated nginx proxy for Docker containers …   142       
nginx/unit-preview                 Unit preview features                           0         
bitnami/nginx                      Bitnami container image for NGINX               192       
nginxproxy/acme-companion          Automated ACME SSL certificate generation fo…   135       
ubuntu/nginx                       Nginx, a high-performance reverse proxy & we…   114       
nginxproxy/docker-gen              Generate files from docker container meta-da…   17        
kasmweb/nginx                      An Nginx image based off nginx:alpine and in…   8         
bitnami/nginx-ingress-controller   Bitnami container image for NGINX Ingress Co…   34        
nginxinc/ngx-rust-tool                                                             0         
nginxinc/mra_python_base                                                           0         
nginxinc/mra-fakes3                                                                0         
bitnami/nginx-exporter             Bitnami container image for NGINX Exporter      5         
rancher/nginx                                                                      2         
root@hcss-ecs-3f38:~# 

但是这样命令行的方式查找,不会显示详细的信息,还是建议到官网去查。(更建议使用已经认证过的 nginx)

找到你喜欢的那个镜像,然后拉取:

root@hcss-ecs-3f38:~# docker pull nginx:1.23.3 
1.23.3: Pulling from library/nginx 
3f9582a2cbe7: Pull complete  
9a8c6f286718: Pull complete  
e81b85700bc2: Pull complete  
73ae4d451120: Pull complete  
6058e3569a68: Pull complete  
3a1b8f201356: Pull complete  
Digest:  
sha256:aa0afebbb3cfa473099a62c4b32e9b3fb73ed23f2a75a65ce1d4b4f55a5 
c2ef2 
Status: Downloaded newer image for nginx:1.23.3 
docker.io/library/nginx:1.23.3 

然后查看镜像是否拉取成功:

root@hcss-ecs-3f38:~# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE 
nginx 1.23.3 904b8cb13b93 8 days ago 142MB 
hello-world latest feb5d9fea6a5 17 months ago 13.3kB 

然后运行镜像:

docker run --name nginx1 --rm -it -p 80:80 nginx:1.23.3 bash 

然后查看一下,在容器的 shell 中,执行 cat /etc/"release" 查看操作系统还是不是 Ubuntu。

之后,启动 nginx:

root@hcss-ecs-3f38:~# nginx
2023/03/10 11:22:05 [notice] 7#7: using the "epoll" event method 
2023/03/10 11:22:05 [notice] 7#7: nginx/1.23.3 
2023/03/10 11:22:05 [notice] 7#7: built by gcc 10.2.1 20210110  
(Debian 10.2.1-6)  
2023/03/10 11:22:05 [notice] 7#7: OS: Linux 5.4.0-100-generic 
2023/03/10 11:22:05 [notice] 7#7: getrlimit(RLIMIT_NOFILE):  
1048576:1048576 
root@5f377aec47ce:/# 2023/03/10 11:22:05 [notice] 8#8: start  
worker processes 
2023/03/10 11:22:05 [notice] 8#8: start worker process 9 
172.17.0.1 - - [10/Mar/2023:11:22:15 +0000] "GET / HTTP/1.1" 200  
615 "-" "curl/7.68.0" "-"

然后保持这个 shell 不退出,打开另一个 shell 窗口执行 curl 命令,可以看到 nginx 的欢迎信息返回:

Shell 
root@hcss-ecs-3f38:~# curl 127.0.0.1 
<!DOCTYPE html> 
<html> 
<head> 
<title>Welcome to nginx!</title> 
<style> 
html { color-scheme: light dark; } 
body { width: 35em; margin: 0 auto; 
font-family: Tahoma, Verdana, Arial, sans-serif; } 
</style> 
</head> 
<body> 
<h1>Welcome to nginx!</h1> 
<p>If you see this page, the nginx web server is successfully  
installed and 
working. Further configuration is required.</p> 
<p>For online documentation and support please refer to 
<a href="http://nginx.org/">nginx.org</a>.<br/> 
Commercial support is available at 
<a href="http://nginx.com/">nginx.com</a>.</p> 
<p><em>Thank you for using nginx.</em></p> 
</body> 
</html> 

如果你的服务器的 80 端口外部能访问,则可以用浏览器看到 nginx 的欢迎页面。

注意云厂商一般都屏蔽了 80 端口,需要联系客服开通。

之后,shell 退出观察再 curl 发现已经 ping 不通了,说明 nginx 是容器里的,而不是服务器里的。

root@hcss-ecs-3f38:~# curl 127.0.0.1 
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection  
refused 

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 21:28:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 21:28:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 21:28:01       57 阅读
  4. Python语言-面向对象

    2024-07-16 21:28:01       68 阅读

热门阅读

  1. 数据结构与算法-09贪心算法&动态规划

    2024-07-16 21:28:01       16 阅读
  2. 访问者模式(大话设计模式)C/C++版本

    2024-07-16 21:28:01       17 阅读
  3. Logstash常用的filter四大插件

    2024-07-16 21:28:01       17 阅读
  4. RTOS中断与任务的同步

    2024-07-16 21:28:01       19 阅读
  5. 哈希表(知识点+leetcode)以及字符串哈希

    2024-07-16 21:28:01       20 阅读
  6. 运维检查:mysql表自增id是否快要用完

    2024-07-16 21:28:01       19 阅读
  7. C++设计模式(装饰器模式)

    2024-07-16 21:28:01       17 阅读
  8. 哪些点权衡素材优秀与否

    2024-07-16 21:28:01       18 阅读
  9. 前端反显后端图片、上传预览图片

    2024-07-16 21:28:01       19 阅读
  10. 使用AIOHTTP模块:提高网络请求效率

    2024-07-16 21:28:01       21 阅读
  11. Redis--过期删除策略和数据淘汰策略

    2024-07-16 21:28:01       25 阅读
  12. actual combat 35 —— es

    2024-07-16 21:28:01       21 阅读
  13. 在 Gradle 项目中,排查依赖冲突可以的详细步骤

    2024-07-16 21:28:01       16 阅读
  14. LeetCode题练习与总结:最大间距--164

    2024-07-16 21:28:01       21 阅读