This commit is contained in:
anlicheng 2025-09-23 16:57:45 +08:00
parent deeffc09d7
commit 24bf90778c

View File

@ -12,12 +12,17 @@
%% API %% API
-export([pull_image/2, check_image_exist/1]). -export([pull_image/2, check_image_exist/1]).
-export([create_container/3, check_container_exist/1, is_container_running/1, start_container/1, stop_container/1]). -export([create_container/3, check_container_exist/1, is_container_running/1, start_container/1, stop_container/1]).
-export([test/0, test_create_container/0]). -export([test/0, test_create_container/0, test1/0]).
test() -> test() ->
Image = <<"docker.1ms.run/library/nginx:latest">>, Image = <<"docker.1ms.run/library/nginx:latest">>,
pull_image(Image, fun(Msg) -> lager:debug("msg is: ~p", [Msg]) end). pull_image(Image, fun(Msg) -> lager:debug("msg is: ~p", [Msg]) end).
test1() ->
Id = <<"redpanda-console">>,
is_container_running(Id).
test_create_container() -> test_create_container() ->
M = #{ M = #{
<<"image">> => <<"docker.1ms.run/library/nginx:latest">>, <<"image">> => <<"docker.1ms.run/library/nginx:latest">>,
@ -100,7 +105,7 @@ test_create_container() ->
-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) ->
Url = io_lib:format("/images/create?fromImage=~s", [binary_to_list(Image)]), Url = lists:flatten(io_lib:format("/images/create?fromImage=~s", [binary_to_list(Image)])),
docker_http:stream_request(Callback, "POST", Url, undefined, []). docker_http:stream_request(Callback, "POST", Url, undefined, []).
-spec check_image_exist(Image :: binary()) -> boolean(). -spec check_image_exist(Image :: binary()) -> boolean().
@ -116,7 +121,7 @@ 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 = lists:flatten(io_lib:format("/containers/create?name=~s", [binary_to_list(ContainerName)])),
%% %%
BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)), BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)),
@ -147,18 +152,10 @@ create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerNa
-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) ->
PortSettings = [stream, exit_status, use_stdio, stderr_to_stdout, binary], case inspect_container(ContainerId) of
ExecCmd = "docker inspect -f '{{.State.Running}}' " ++ binary_to_list(ContainerId), {ok, #{<<"State">> := #{<<"Running">> := Running}}} ->
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of Running;
Port when is_port(Port) -> {error, _} ->
case gather_output(Port) of
{0, Val0} ->
Val = string:trim(Val0),
Val =:= <<"true">>;
_ ->
false
end;
_Error ->
false false
end. end.
@ -212,6 +209,25 @@ stop_container(ContainerName) when is_binary(ContainerName) ->
false false
end. end.
-spec inspect_container(ContainerId :: binary()) -> {ok, Json :: map()} | {error, Error :: any()}.
inspect_container(ContainerId) when is_binary(ContainerId) ->
Url = lists:flatten(io_lib:format("/containers/~s/json", [binary_to_list(ContainerId)])),
Headers = [
{<<"Content-Type">>, <<"application/json">>}
],
case docker_http:request("GET", Url, undefined, Headers) of
{ok, 200, _Headers, Resp} ->
Json = jiffy:decode(Resp, [return_maps]),
{ok, Json};
{ok, _StatusCode, _Header, ErrorResp} ->
case catch jiffy:decode(ErrorResp) of
#{<<"message">> := Msg} ->
{error, Msg};
_ ->
{error, ErrorResp}
end
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% helper methods %%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%