我们知道容器化最大的好处是软件交付形成了一种标准化,其带来的好处是巨大且深远的,让开发者从解决各种环境差异的痛苦中解放出来,同时大幅简化了部署流程和管理成本。
当我们用docker run运行一个容器时,如果没有指定一个名字,docker将会为给我们自动生成一个名字,通过docker ps可以查看正在运行的容器,例如下面图中的exciting_gates, sad_babbage, reverent_albattani
作为开发者的你有没有好奇过这个名字到底是什么意思?它的生成机制是什么?如果你和我一样也对此好奇,终于在某一天忍不住要去了解一下背后的原理。那么恭喜你,现在有了AI加持,你需要做的只是具有针对性地提问就行。
利用deepwiki工具借助LLM可以帮助你快速从一个大型代码仓库中找到你想要关注的代码实现细节,当然把代码克隆到本地,用Cursor Ask模式也可以帮助我们理解指定的代码仓。
https://deepwiki.com/search/how-docker-generate-the-name-i_4d18ce9d-57b5-49fb-83eb-638162b07d07
可以看到容器名的生成算法实现在了pkg/namesgenerator/names-generator.go文件内,它对外提供了一个函数GetRandomName:
函数非常简单,最重要的部分就是从两个数组字典(形容词数组和人名的姓surname数组)各随机取一个,构成adjective_surname这种格式。值得一提的是函数中有一个硬编码,当匹配到boring_wozniak时直接跳过重新选择,看来当初写这段代码的程序员一定是Steve Woziak(苹果公司创始人之一)的粉丝。例如文章开头截图中的exciting_gates和sad_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,向那些顶尖科学家和工程师们致敬。