当前位置: 首页 > news >正文

代码背后的故事:docker容器名生成算法

我们知道容器化最大的好处是软件交付形成了一种标准化,其带来的好处是巨大且深远的,让开发者从解决各种环境差异的痛苦中解放出来,同时大幅简化了部署流程和管理成本。

Pasted image 20251107133021

当我们用docker run运行一个容器时,如果没有指定一个名字,docker将会为给我们自动生成一个名字,通过docker ps可以查看正在运行的容器,例如下面图中的exciting_gates, sad_babbage, reverent_albattani

Pasted image 20251107133939

作为开发者的你有没有好奇过这个名字到底是什么意思?它的生成机制是什么?如果你和我一样也对此好奇,终于在某一天忍不住要去了解一下背后的原理。那么恭喜你,现在有了AI加持,你需要做的只是具有针对性地提问就行。

利用deepwiki工具借助LLM可以帮助你快速从一个大型代码仓库中找到你想要关注的代码实现细节,当然把代码克隆到本地,用Cursor Ask模式也可以帮助我们理解指定的代码仓。

Pasted image 20251115120554

https://deepwiki.com/search/how-docker-generate-the-name-i_4d18ce9d-57b5-49fb-83eb-638162b07d07

可以看到容器名的生成算法实现在了pkg/namesgenerator/names-generator.go文件内,它对外提供了一个函数GetRandomName:

Pasted image 20251115121504

函数非常简单,最重要的部分就是从两个数组字典(形容词数组和人名的姓surname数组)各随机取一个,构成adjective_surname这种格式。值得一提的是函数中有一个硬编码,当匹配到boring_wozniak时直接跳过重新选择,看来当初写这段代码的程序员一定是Steve Woziak(苹果公司创始人之一)的粉丝。例如文章开头截图中的exciting_gatessad_babbage就是这样生成的。

我们看看上面两个数组中分别都有些什么:

左边的形容词数组:

	left = [...]string{"admiring","adoring","affectionate","agitated","amazing",..."wonderful","xenodochial","youthful","zealous","zen",}

几乎都是比较正面、积极的形容词,有好些词汇比较生僻笔者从没使用过(逃)。

右边的人名surname数组:

	right = [...]string{// Maria Gaetana Agnesi - Italian mathematician, philosopher, theologian and humanitarian. She was the first woman to write a mathematics handbook and the first woman appointed as a Mathematics Professor at a University. https://en.wikipedia.org/wiki/Maria_Gaetana_Agnesi"agnesi",// Muhammad ibn Jābir al-Ḥarrānī al-Battānī was a founding father of astronomy. https://en.wikipedia.org/wiki/Mu%E1%B8%A5ammad_ibn_J%C4%81bir_al-%E1%B8%A4arr%C4%81n%C4%AB_al-Batt%C4%81n%C4%AB"albattani",// Frances E. Allen, became the first female IBM Fellow in 1989. In 2006, she became the first female recipient of the ACM's Turing Award. https://en.wikipedia.org/wiki/Frances_E._Allen"allen",// June Almeida - Scottish virologist who took the first pictures of the rubella virus - https://en.wikipedia.org/wiki/June_Almeida"almeida",...// The Wright brothers, Orville and Wilbur - credited with inventing and building the world's first successful airplane and making the first controlled, powered and sustained heavier-than-air human flight - https://en.wikipedia.org/wiki/Wright_brothers"wright",// Chien-Shiung Wu - Chinese-American experimental physicist who made significant contributions to nuclear physics. https://en.wikipedia.org/wiki/Chien-Shiung_Wu"wu",// Rosalyn Sussman Yalow - Rosalyn Sussman Yalow was an American medical physicist, and a co-winner of the 1977 Nobel Prize in Physiology or Medicine for development of the radioimmunoassay technique. https://en.wikipedia.org/wiki/Rosalyn_Sussman_Yalow"yalow",// Ada Yonath - an Israeli crystallographer, the first woman from the Middle East to win a Nobel prize in the sciences. https://en.wikipedia.org/wiki/Ada_Yonath"yonath",// Nikolay Yegorovich Zhukovsky (Russian: Никола́й Его́рович Жуко́вский, January 17 1847 – March 17, 1921) was a Russian scientist, mathematician and engineer, and a founding father of modern aero- and hydrodynamics. Whereas contemporary scientists scoffed at the idea of human flight, Zhukovsky was the first to undertake the study of airflow. He is often called the Father of Russian Aviation. https://en.wikipedia.org/wiki/Nikolay_Yegorovich_Zhukovsky"zhukovsky",}

人名数组中基本上都是一些比较知名的科学家例如欧拉、爱因斯坦、费曼和图灵等,除了科学家之外还有工程师例如UNIX和C语言之父Ken Thompson、发明Linux和Git的Linus Torvalds和摩尔定律的提出者Moore等。限于篇幅原因,不能在文中一一列出,感兴趣的可以直接去查看源文件。

这个名单中的中国人有两个:屠呦呦和吴健雄。

没想到看起来普通的一个容器名生成算法,翻一番它的变更历史,可以看到很多代码之外的故事。

值得一提的是这个文件中的列表已经在2022年被Moby项目的维护人员标记为冻结不再接受新的名单变更了,主要原因是后面的维护负担越来越大,这里主要体现在两个方面一是adjective_surname这种形式有的时候会引入一些尴尬的组合,其次是名单中的人物会变得有争议不再适合放在名单中了,例如有两位牵扯到爱泼斯坦案件的。

另外,在 docker 0.7.x 之前,容器名的生成算法生成的还是颜色和动物组成color_animal这种组合(例如 red_panda),后来才改成了 adjective_surname,向那些顶尖科学家和工程师们致敬。

看到这里,当下一次你再docker run起来一个容器时,会不会好奇这个容器名又是在向谁致敬呢?
另外,在 Docker 0.7.x 之前,容器名的生成方式其实是 color_animal(例如 red_panda),后来才改成了 adjective_surname,向那些顶尖科学家和工程师们致敬。

http://www.gsyq.cn/news/51444.html

相关文章:

  • 20232428 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • HarmonyOS 5 鸿蒙Context上下文机制与资源管理详解 - 教程
  • 11.16组会
  • MFC + OpenCV 图像预览显示不全中断问题解除:GDI行填充详解
  • 飞牛os打开本机usb摄像头
  • 12306售票系统分析与实战
  • Java StringTokenizer 类 Scanner 类详解
  • Java 断言(Assert) 简介
  • 实用指南:IntelliJ IDEA 2023中为 Spring Boot 项目添加注释模板
  • 量化存储墙(三):GEMM EMA 下限解析解以及硬件静态资源分配设计
  • c# 获取当前时间
  • YOLOv3 深度解析:网络架构、核心改进与目标检测实践 - 指南
  • ai学习机是不是智商税?到底有没有用?2025年学习机推荐指南
  • docker命令提示插件
  • C语言和C++有什么区别
  • Snipe-IT支持Oauth2登录
  • 绝对值的性质
  • 智能硬件利用小聆AI自定义MCP应用开发操作讲解
  • Linux - sudo -i
  • 科学计算复习
  • 2025年11月石笼网厂家最新推荐,聚焦资质、案例、售后的五家企业深度解读!
  • windows安装mingw
  • filebeat + logstash接入OpenStack日志
  • 11 月 13 日
  • Lombok踩了无数次的坑
  • 11 月 11 日
  • 2025年烘干机厂家排行榜前十强推荐:行业精选与选择指南
  • [论文笔记] Lifting On-Demand Analysis to Higher-Order Languages
  • 全国性搬家公司推荐榜:从运费优势、专业度、靠谱口碑、费用便宜划算等综合实力排名
  • Invicti v25.11 发布,新增功能简介