fix udp commands

This commit is contained in:
anlicheng 2025-09-23 16:41:46 +08:00
parent df1741af8a
commit deeffc09d7

View File

@ -21,7 +21,7 @@ test() ->
test_create_container() -> test_create_container() ->
M = #{ M = #{
<<"image">> => <<"docker.1ms.run/library/nginx:latest">>, <<"image">> => <<"docker.1ms.run/library/nginx:latest">>,
<<"container_name">> => <<"my_nginx_new">>, <<"container_name">> => <<"my_nginx_new1">>,
<<"command">> => [ <<"command">> => [
<<"nginx">>, <<"nginx">>,
<<"-g">>, <<"-g">>,
@ -96,7 +96,7 @@ test_create_container() ->
<<"retries">> => 3 <<"retries">> => 3
} }
}, },
create_container(<<"my_nginx_xx">>, "/usr/local/code/efka/", M). create_container(<<"my_nginx_xx3">>, "/usr/local/code/efka/", M).
-spec pull_image(Image :: binary(), Callback :: fun((Msg :: binary()) -> no_return())) -> ok | {error, ExitCode :: integer()}. -spec pull_image(Image :: binary(), Callback :: fun((Msg :: binary()) -> no_return())) -> ok | {error, ExitCode :: integer()}.
pull_image(Image, Callback) when is_binary(Image), is_function(Callback, 1) -> pull_image(Image, Callback) when is_binary(Image), is_function(Callback, 1) ->
@ -117,22 +117,33 @@ check_image_exist(Image) when is_binary(Image) ->
-spec create_container(ContainerName :: binary(), ContainerDir :: string(), Config :: map()) -> {ok, ContainerId :: binary()} | {error, Reason :: any()}. -spec create_container(ContainerName :: binary(), ContainerDir :: string(), Config :: map()) -> {ok, ContainerId :: binary()} | {error, Reason :: any()}.
create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerName), is_list(ContainerDir), is_map(Config) -> create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerName), is_list(ContainerDir), is_map(Config) ->
Url = io_lib:format("/containers/create?name=~s", [binary_to_list(ContainerName)]), Url = io_lib:format("/containers/create?name=~s", [binary_to_list(ContainerName)]),
Image = maps:get(<<"image">>, Config),
%% %%
BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)), BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)),
BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/usr/local/etc/">>],
Options = build_options(Config),
lists:foreach(fun({K, V}) -> %%
lager:debug("~p => ~p", [K, V]) Volumes0 = maps:get(<<"volumes">>, Config, []),
end, maps:to_list(Options)), Volumes = [<<BinContainerDir/binary, ":/usr/local/etc/">>|Volumes0],
NewConfig = Config#{<<"volumes">> => Volumes},
Options = build_options(NewConfig),
lists:foreach(fun({K, V}) -> lager:debug("~p => ~p", [K, V]) end, maps:to_list(Options)),
Body = iolist_to_binary(jiffy:encode(Options, [force_utf8])), Body = iolist_to_binary(jiffy:encode(Options, [force_utf8])),
Res = docker_http:request("POST", Url, Body, []), Headers = [
lager:debug("res is: ~p", [Res]). {<<"Content-Type">>, <<"application/json">>}
],
case docker_http:request("POST", Url, Body, Headers) of
{ok, 201, _Headers, Resp} ->
#{<<"Id">> := ContainerId} = jiffy:decode(Resp, [return_maps]),
{ok, ContainerId};
{ok, _StatusCode, _, ErrorResp} ->
case catch jiffy:decode(ErrorResp, [return_maps]) of
#{<<"message">> := Msg} ->
{error, Msg};
_ ->
{error, ErrorResp}
end
end.
-spec is_container_running(ContainerId :: binary()) -> boolean(). -spec is_container_running(ContainerId :: binary()) -> boolean().
is_container_running(ContainerId) when is_binary(ContainerId) -> is_container_running(ContainerId) when is_binary(ContainerId) ->
@ -232,7 +243,6 @@ build_options(Config) when is_map(Config) ->
<<"Healthcheck">> => build_healthcheck(Config), <<"Healthcheck">> => build_healthcheck(Config),
<<"HostConfig">> => fold_merge([ <<"HostConfig">> => fold_merge([
build_binds(Config), build_binds(Config),
build_volumes(Config),
build_restart(Config), build_restart(Config),
build_privileged(Config), build_privileged(Config),
build_cap_add_drop(Config), build_cap_add_drop(Config),
@ -286,7 +296,7 @@ build_networks(Config) ->
[] -> #{}; [] -> #{};
_ -> _ ->
NetCfg = maps:from_list([{N, #{}} || N <- Nets]), NetCfg = maps:from_list([{N, #{}} || N <- Nets]),
#{<<"NetworkingConfig">> => #{<<"EndpointsConfig">> => NetCfg}} #{<<"EndpointsConfig">> => NetCfg}
end. end.
@ -331,23 +341,6 @@ parse_duration(Bin) ->
end. end.
%% --- --- %% --- ---
build_volumes(Config) ->
Vols = maps:get(<<"volumes">>, Config, []),
case Vols of
[] -> #{};
_ ->
HostBinds = Vols,
VolMap = maps:from_list(lists:map(fun(V) ->
[_Host, Cont] = binary:split(V, <<":">>, []),
{Cont, #{}}
end, Vols)),
#{
<<"HostConfig">> => #{
<<"Binds">> => HostBinds
},
<<"Volumes">> => VolMap
}
end.
build_restart(Config) -> build_restart(Config) ->
case maps:get(<<"restart">>, Config, undefined) of case maps:get(<<"restart">>, Config, undefined) of