fix ports

This commit is contained in:
anlicheng 2026-05-10 23:43:01 +08:00
parent 0f57df4726
commit e1501c69d2
2 changed files with 93 additions and 2 deletions

View File

@ -415,6 +415,7 @@ Body = iolist_to_binary(json:encode(Options))
| `memory_reservation` | `HostConfig.MemoryReservation` | 0 时省略。 | | `memory_reservation` | `HostConfig.MemoryReservation` | 0 时省略。 |
| `nano_cpus` | `HostConfig.NanoCpus` | 0 时省略。 | | `nano_cpus` | `HostConfig.NanoCpus` | 0 时省略。 |
| `cpu_shares` | `HostConfig.CpuShares` | 0 时省略。 | | `cpu_shares` | `HostConfig.CpuShares` | 0 时省略。 |
| `port_bindings` | `HostConfig.PortBindings` | 转成 Docker 端口绑定 object空列表时省略。 |
| `ulimits` | `HostConfig.Ulimits` | 空列表时省略。 | | `ulimits` | `HostConfig.Ulimits` | 空列表时省略。 |
| `tmpfs` | `HostConfig.Tmpfs` | 空 map 时省略。 | | `tmpfs` | `HostConfig.Tmpfs` | 空 map 时省略。 |
| `sysctls` | `HostConfig.Sysctls` | 空 map 时省略。 | | `sysctls` | `HostConfig.Sysctls` | 空 map 时省略。 |
@ -516,6 +517,40 @@ efka 自动补丁后输出:
`cgroup_permissions` 为空时默认输出 `rwm` `cgroup_permissions` 为空时默认输出 `rwm`
### PortBindings
输入:
```erlang
[
#{<<"host_ip">> => <<>>, <<"host_port">> => 8080, <<"container_port">> => 80, <<"protocol">> => <<"tcp">>},
#{<<"host_ip">> => <<>>, <<"host_port">> => 443, <<"container_port">> => 443, <<"protocol">> => <<"tcp">>}
]
```
输出:
```json
{
"HostConfig": {
"PortBindings": {
"80/tcp": [
{"HostIp": "", "HostPort": "8080"}
],
"443/tcp": [
{"HostIp": "", "HostPort": "443"}
]
}
}
}
```
规则:
- key 使用容器端口和协议组成:`container_port/protocol`
- `protocol` 为空或 `tcp` 时输出 `tcp`
- `HostPort` 按 Docker API 要求输出为字符串。
### 资源限制 ### 资源限制
输入: 输入:
@ -687,7 +722,15 @@ efka 自动补丁后输出:
<<"host_config">> => #{ <<"host_config">> => #{
<<"binds">> => [<<"/host/data:/data">>], <<"binds">> => [<<"/host/data:/data">>],
<<"restart_policy">> => #{<<"name">> => <<"always">>, <<"maximum_retry_count">> => 0}, <<"restart_policy">> => #{<<"name">> => <<"always">>, <<"maximum_retry_count">> => 0},
<<"memory">> => 536870912 <<"memory">> => 536870912,
<<"port_bindings">> => [
#{
<<"host_ip">> => <<>>,
<<"host_port">> => 8080,
<<"container_port">> => 80,
<<"protocol">> => <<"tcp">>
}
]
}, },
<<"networking_config">> => #{ <<"networking_config">> => #{
<<"endpoints">> => [#{<<"name">> => <<"bridge">>}] <<"endpoints">> => [#{<<"name">> => <<"bridge">>}]
@ -730,7 +773,15 @@ efka 自动补丁后输出:
"RestartPolicy": { "RestartPolicy": {
"Name": "always" "Name": "always"
}, },
"Memory": 536870912 "Memory": 536870912,
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
}
} }
} }
``` ```

View File

@ -91,6 +91,7 @@ build_create_options(Create0) when is_map(Create0) ->
field(HostConfig, <<"nano_cpus">>, 0), field(HostConfig, <<"nano_cpus">>, 0),
field(HostConfig, <<"cpu_shares">>, 0) field(HostConfig, <<"cpu_shares">>, 0)
), ),
build_port_bindings(field(HostConfig, <<"port_bindings">>, [])),
build_ulimits(field(HostConfig, <<"ulimits">>, [])), build_ulimits(field(HostConfig, <<"ulimits">>, [])),
build_tmpfs(field(HostConfig, <<"tmpfs">>, #{})), build_tmpfs(field(HostConfig, <<"tmpfs">>, #{})),
build_sysctls(field(HostConfig, <<"sysctls">>, #{})), build_sysctls(field(HostConfig, <<"sysctls">>, #{})),
@ -245,6 +246,45 @@ build_resources(MemoryBytes, ReservationBytes, NanoCpus, CpuShares) ->
HostConfig3#{<<"CpuShares">> => CpuShares} HostConfig3#{<<"CpuShares">> => CpuShares}
end. end.
-spec build_port_bindings([map()]) -> map().
build_port_bindings(PortBindings) when is_list(PortBindings) ->
case PortBindings of
[] ->
#{};
_ ->
#{<<"PortBindings">> => fold_port_bindings(PortBindings)}
end.
-spec fold_port_bindings([map()]) -> map().
fold_port_bindings(PortBindings) ->
lists:foldl(
fun(PortBinding, Acc) ->
PortKey = port_binding_key(PortBinding),
Binding = port_binding_value(PortBinding),
Existing = maps:get(PortKey, Acc, []),
Acc#{PortKey => Existing ++ [Binding]}
end,
#{},
PortBindings).
-spec port_binding_key(map()) -> binary().
port_binding_key(#{<<"container_port">> := ContainerPort, <<"protocol">> := Protocol}) ->
PortBin = integer_to_binary(ContainerPort),
ProtocolBin = to_binary(Protocol),
case ProtocolBin of
<<>> ->
<<PortBin/binary, "/tcp">>;
<<"tcp">> ->
<<PortBin/binary, "/tcp">>;
_ ->
<<PortBin/binary, "/", ProtocolBin/binary>>
end.
-spec port_binding_value(map()) -> map().
port_binding_value(#{<<"host_port">> := HostPort} = PortBinding) ->
HostIp = to_binary(field(PortBinding, <<"host_ip">>, <<>>)),
#{<<"HostIp">> => HostIp, <<"HostPort">> => integer_to_binary(HostPort)}.
-spec build_ulimits([map()]) -> map(). -spec build_ulimits([map()]) -> map().
build_ulimits(Ulimits) when is_list(Ulimits) -> build_ulimits(Ulimits) when is_list(Ulimits) ->
case Ulimits of case Ulimits of