fix docker

This commit is contained in:
anlicheng 2025-09-19 16:43:45 +08:00
parent 929a736b17
commit 1411d093da
3 changed files with 18 additions and 29 deletions

View File

@ -59,8 +59,8 @@ create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerNa
Cmd = maps:get(<<"command">>, Config, []), Cmd = maps:get(<<"command">>, Config, []),
%% %%
BinContainerDir = list_to_binary(ContainerDir), BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)),
BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/etc/">>], BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/usr/local/etc/">>],
Options = build_options(Config), Options = build_options(Config),
Args = lists:flatten([Image | BaseOptions ++ Options ++ Cmd]), Args = lists:flatten([Image | BaseOptions ++ Options ++ Cmd]),
@ -122,14 +122,14 @@ check_container_exist(ContainerName) when is_binary(ContainerName) ->
-spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}. -spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
start_container(ContainerName) when is_binary(ContainerName) -> start_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1", ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " 2>&1",
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of
{0, _} -> {0, _} ->
ok; ok;
{_ExitCode, _Error} -> {_ExitCode, Error} ->
{error, <<"启动失败"/utf8>>} {error, Error}
end; end;
_Error -> _Error ->
{error, <<"启动失败"/utf8>>} {error, <<"启动失败"/utf8>>}
@ -138,17 +138,18 @@ start_container(ContainerName) when is_binary(ContainerName) ->
-spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}. -spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
stop_container(ContainerName) when is_binary(ContainerName) -> stop_container(ContainerName) when is_binary(ContainerName) ->
PortSettings = [stream, exit_status, use_stdio, binary], PortSettings = [stream, exit_status, use_stdio, binary],
ExecCmd = "docker stop " ++ binary_to_list(ContainerName), ExecCmd = "docker stop " ++ binary_to_list(ContainerName) ++ " 2>&1",
lager:debug("[docker_commands] cmd : ~p", [ExecCmd]),
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of
{0, _} -> {0, _} ->
ok; ok;
{_ExitCode, _Error} -> {_ExitCode, Error} ->
{error, <<>>} {error, Error}
end; end;
_Error -> _Error ->
{error, <<>>} false
end. end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -165,24 +166,6 @@ gather_output(Port, Acc) ->
{Status, iolist_to_binary(Acc)} {Status, iolist_to_binary(Acc)}
end. end.
extract_sha256(Output) when is_binary(Output) ->
Parts = binary:split(Output, <<$\n>>, [global]),
lager:debug("parts: ~p", [Parts]),
case lists:search(fun(Line) -> starts_with(Line, <<"Digest:">>) end, Parts) of
{value, Digest} ->
Sha256 = lists:last(binary:split(Digest, <<":">>, [global])),
{ok, Sha256};
false ->
error
end.
starts_with(Binary, Prefix) when is_binary(Binary), is_binary(Prefix) ->
PrefixSize = byte_size(Prefix),
case Binary of
<<Prefix:PrefixSize/binary, _Rest/binary>> -> true;
_ -> false
end.
%% %%
build_options(Config) -> build_options(Config) ->
lists:flatten([ lists:flatten([

View File

@ -10,7 +10,7 @@
-author("anlicheng"). -author("anlicheng").
%% API %% API
-export([ensure_dir/2, get_dir/2, ensure_etc_dir/1]). -export([ensure_dir/2, get_dir/2, ensure_etc_dir/1, make_etc_dir_name/1]).
-spec ensure_dir(RootDir :: string(), ContainerName :: binary()) -> {ok, ServerRootDir :: string()}. -spec ensure_dir(RootDir :: string(), ContainerName :: binary()) -> {ok, ServerRootDir :: string()}.
ensure_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName) -> ensure_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName) ->
@ -22,7 +22,7 @@ ensure_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerNam
-spec ensure_etc_dir(ContainerDir :: string()) -> {ok, EtcDir :: string()}. -spec ensure_etc_dir(ContainerDir :: string()) -> {ok, EtcDir :: string()}.
ensure_etc_dir(ContainerDir) when is_list(ContainerDir) -> ensure_etc_dir(ContainerDir) when is_list(ContainerDir) ->
%% %%
EtcDir = ContainerDir ++ "etc/", EtcDir = make_etc_dir_name(ContainerDir),
ok = filelib:ensure_dir(EtcDir), ok = filelib:ensure_dir(EtcDir),
{ok, EtcDir}. {ok, EtcDir}.
@ -36,3 +36,8 @@ get_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName)
false -> false ->
error error
end. end.
-spec make_etc_dir_name(ContainerDir :: string()) -> string().
make_etc_dir_name(ContainerDir) when is_list(ContainerDir) ->
%%
ContainerDir ++ "etc/".

View File

@ -114,6 +114,7 @@ handle_call({start_container, ContainerId}, _From, State) ->
{reply, {error, Reason}, State} {reply, {error, Reason}, State}
end; end;
%% , status字段 %% , status字段
handle_call({stop_container, ContainerId}, _From, State = #state{}) -> handle_call({stop_container, ContainerId}, _From, State = #state{}) ->
case docker_commands:stop_container(ContainerId) of case docker_commands:stop_container(ContainerId) of