
1. 鏡像源配置文件預備知識:TOML 語言
(感興趣的話可以看看這一節,沒興趣的話直接從下一節開始)
TOML 是寫配置文件的一種語言,
1.1 [table]
用來定義“表”,
TOML 中表示鍵值對的集合,類似於字典,
[table](稱為表頭)單獨一行出現,
其下面(直到另外一個表頭出現)的鍵值對都是這個表的鍵值對,如:
[table-1]
key1 = "some string"
key2 = 123
[table-2]
key1 = "another string"
key2 = 456
1.2 [[array_of_table]]
表數組
“表數組”出現的時候,就順帶定義了它的第一個元素,
后續再次出現這個“表數組”的時候,會在該“表數組”中創建新的元素,也就是說:
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # 數組里第二個元素:空表
[[products]] # 數組里的第三個元素
name = "Nail"
sku = 284758393
color = "gray"
json 結構為:
{
"products": [
{ "name": "Hammer", "sku": 738594937 },
{ },
{ "name": "Nail", "sku": 284758393, "color": "gray" }
]
}
1.3 [[ ]] 的 子表
[[fruits]]
name = "apple"
[fruits.physical] # 子表
color = "red"
shape = "round"
[[fruits.varieties]] # 嵌套表數組
name = "red delicious"
[[fruits.varieties]]
name = "granny smith"
[[fruits]]
name = "banana"
[[fruits.varieties]]
name = "plantain"
json 結構
{
"fruits": [
{
"name": "apple",
"physical": {
"color": "red",
"shape": "round"
},
"varieties": [
{ "name": "red delicious" },
{ "name": "granny smith" }
]
},
{
"name": "banana",
"varieties": [
{ "name": "plantain" }
]
}
]
}
2. 鏡像源(倉庫)配置
Podman 引入了一個 registry 的概念,
registry(注冊表、登記處)就是多個容器鏡像源(如,docker.io 就可以是其中之一)
在使用 podman pull 一個鏡像的時候,
如果鏡像使用的是如下的寫法,那么就會從指定的倉庫進行下載。

但是如果使用的是
podman pull httpd-24-rhel7
的時候,也就是沒有指明鏡像源,會使用配置文件中定義的鏡像倉庫。
podman 的鏡像源配置文件為 /etc/containers/registries.conf
/etc/containers/registries.conf
# There are multiple versions of the configuration syntax available,
# where the second iteration is backwards compatible to the first one.
# Mixing up both formats will result in an runtime error.
配置文件最開頭就說:有多個 versions 的配置語法,
第三行表明,兩種寫法不能混用,否則會報錯
Error: ...
"/etc/containers/registries.conf": mixing sysregistry v1/v2 is not supported
2.1 Version 2
/etc/containers/registries.conf

Version 2 在配置文件中的后面部分一點
接前文,當 pull 一個鏡像,沒有指定鏡像源的時候,
下面字段指定的鏡像源順序獲取
unqualified-search-registries = ["docker.io", "registry.access.redhat.com"]
由此,
通常會將鏡像源替換為國內的,已加速訪問下載,
man containers-registries.conf 里面給出了相關的示例:
1 # EXAMPLE
2
3 unqualified-search-registries = ["example.com"]
4
5 [[registry]]
6 prefix = "example.com/foo"
7 insecure = false
8 blocked = false
9 location = "internal-registry-for-example.com/bar"
10
11 [[registry.mirror]]
12 location = "example-mirror-0.local/mirror-for-foo"
13
14 [[registry.mirror]]
15 location = "example-mirror-1.local/mirrors/foo"
16 insecure = true
如上,行 3 - 9
假定在沒有 行 11 之后內容的前提下,
a pull of example.com/foo/image:latest will try:
internal-registry-for-example.net/bar/image:latest
也就是請求 prefix 指定鏡像源的時候,會到 location 指定的地方去找
當不指定 prefix 則默認和 location 一致。
(個人感覺有點 請求重定向 的意思,
prefix 字段對應行 3 列表里面的一個元素,也就是 pull 鏡像時候,鏡像名的前綴,也就是鏡像源
location 則用來指向實際要操作的鏡像源地址)
行 11 - 16
a pull of example.com/foo/image:latest will try:
- example-mirror-0.local/mirror-for-foo/image:latest
- example-mirror-1.local/mirrors/foo/image:latest
- internal-registry-for-example.net/bar/image:latest
in order, and use the first one that exists.
也就是會先嘗試 行 12,接下來行 15,最后才是行 9
2.2 Version 1
(沒興趣就可以 pass 了,使用上面這種方法即可)
配置文件中前面部分,也是默認生效的部分
man containers-registries.conf
VERSION 1 FORMAT - DEPRECATED
(也就是 version 1 格式的配置已經是不推薦使用了)
...
VERSION 1 format is still supported but ...
...
The TOML format is used to build a simple list of registries under three categories:
1. registries.search,
2. registries.insecure,
3. registries.block.
(通過這三個字段((或者應該是說段落))來描述 registry)
/etc/containers/registries.conf
# To ensure compatibility with docker we've included docker.io
# in the default search list.
# (為了保證兼容性,已經將 docker.io 加入到鏡像搜索地址的列表里面)
# ...
[registries.search]
registries = ['registry.access.redhat.com',
'registry.redhat.io',
'docker.io']
unqualified-search-registries = ["registry.fedoraproject.org",
"registry.access.redhat.com",
"registry.centos.org",
"docker.io"]
registries 字段
podman 默認會搜索鏡像的地址,
如上可見,
優先排在前面的,又說 redhat 的地址排在前面,所以優先 redhat下載,但 redhat 的需要賬號登錄相關的步驟,可以把 docker.io 調整到最前面
unqualified-search-registries 字段
unqualified,無資格的;不受限制的;
3. 替換國內鏡像源
3.1 阿里鏡像源地址
使用賬號登錄阿里雲后,
進入【控制台】
在【產品與服務列表】里面找到【容器鏡像服務】

地址通常是 https://xxx.mirror.aliyuncs.com
xxx 根據個人賬號有不同,寫入 location 字段的時候,不用寫 https 前綴
如:
location="rncxm540.mirror.aliyuncs.com"
3.2 修改配置文件
/etc/container/registries.conf
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "2jriwsnk.mirror.aliyuncs.com"
注釋掉 Version 1 的內容,否則會報錯,
雖然配置文件很長,但沒注釋的也就這幾行,如下:

如上,通過 grep 排除掉與 # 開頭的行和 空行
3.3 運行測試

如上,
當 pull 一個鏡像的時候,
雖然表面上是 pull docker.io/bibrary/httpd:latest
但通過 ss 能夠看到,目標 IP 地址為浙江杭州
4. 搜索鏡像
4.1 命令行
podman search xxx
字段 Stars:
類似 GitHub 里面的 star
字段 Official:
是否 docker 官方發布
4.2 網頁
也可以到 docker 的官網查找需要的鏡像
hub.docker.com

