This commit is contained in:
anlicheng 2025-09-26 13:53:57 +08:00
parent 74d68340b5
commit a5160ce869
3 changed files with 3 additions and 58 deletions

View File

@ -13,7 +13,7 @@
-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, remove_container/1, kill_container/1]).
-spec pull_image(Image :: binary(), Callback :: fun((Msg :: any()) -> no_return())) -> ok | {error, ExitCode :: integer()}.
-spec pull_image(Image :: binary(), Callback :: fun((Msg :: any()) -> no_return())) -> ok | {error, Reason :: any()}.
pull_image(Image, Callback) when is_binary(Image), is_function(Callback, 1) ->
Url = lists:flatten(io_lib:format("/images/create?fromImage=~s", [binary_to_list(Image)])),
docker_http:stream_request(Callback, "POST", Url, undefined, []).

View File

@ -8,16 +8,13 @@
%%%-------------------------------------------------------------------
-module(docker_deployer).
-author("anlicheng").
-include("efka_tables.hrl").
-include("message.hrl").
-dialyzer([{nowarn_function, normalize_image/1}]).
-behaviour(gen_server).
%% API
-export([start_link/3]).
-export([deploy/1]).
-export([maybe_create_env_file/2]).
-export([test/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -30,49 +27,6 @@
config = #{}
}).
test() ->
M = #{
<<"image">> => <<"docker.1ms.run/library/nginx:latest">>,
<<"container_name">> => <<"my_nginx">>,
<<"command">> => [<<"nginx">>,<<"-g">>,<<"daemon off;">>],
<<"entrypoint">> => [<<"/docker-entrypoint.sh">>],
<<"envs">> => [<<"ENV1=val1">>, <<"ENV2=val2">>],
<<"env_file">> => [<<"./env.list">>],
<<"ports">> => [<<"8080:80">>, <<"443:443">>],
<<"expose">> => [<<"80">>, <<"443">>],
<<"volumes">> => [<<"/host/data:/data">>, <<"/host/log:/var/log">>],
<<"networks">> => [<<"mynet">>],
<<"labels">> => #{ <<"role">> => <<"web">>, <<"env">> => <<"prod">> },
<<"restart">> => <<"always">>,
<<"user">> => <<"www-data">>,
<<"working_dir">> => <<"/app">>,
<<"hostname">> => <<"myhost">>,
<<"privileged">> => true,
<<"cap_add">> => [<<"NET_ADMIN">>],
<<"cap_drop">> => [<<"MKNOD">>],
<<"devices">> => [<<"/dev/snd:/dev/snd">>],
<<"mem_limit">> => <<"512m">>,
<<"mem_reservation">> => <<"256m">>,
<<"cpu_shares">> => 512,
<<"cpus">> => 1.5,
<<"ulimits">> => #{ <<"nofile">> => <<"1024:2048">> },
<<"sysctls">> => #{ <<"net.ipv4.ip_forward">> => <<"1">> },
<<"tmpfs">> => [<<"/tmp">>],
<<"extra_hosts">> => [<<"host1:192.168.0.1">>],
<<"healthcheck">> => #{
<<"test">> => [<<"CMD-SHELL">>, <<"curl -f http://localhost || exit 1">>],
<<"interval">> => <<"30s">>,
<<"timeout">> => <<"10s">>,
<<"retries">> => 3
}
},
TaskId = 1,
RootDir = "/tmp/",
Res = do_deploy(TaskId, RootDir, M),
lager:debug("res is: ~p", [Res]).
%%%===================================================================
%%% API
%%%===================================================================
@ -225,15 +179,6 @@ do_deploy(TaskId, ContainerDir, Config) when is_integer(TaskId), is_list(Contain
end
end.
maybe_create_env_file(_ContainerDir, []) ->
ok;
maybe_create_env_file(ContainerDir, Envs) when is_list(Envs)->
TargetFile = ContainerDir ++ "env",
{ok, IoDevice} = file:open(TargetFile, [write, binary]),
lists:foreach(fun(Env) -> file:write(IoDevice, <<Env/binary, $\n>>) end, Envs),
ok = file:close(IoDevice),
{ok, TargetFile}.
-spec normalize_image(binary()) -> binary().
normalize_image(Image) when is_binary(Image) ->
Parts = binary:split(Image, <<"/">>, [global]),

View File

@ -46,7 +46,7 @@ receive_body(ConnPid, StreamRef, Status, Headers, Acc) ->
end.
%% Unix Socket Docker API
-spec stream_request(Callback :: any(), Method :: iolist(), Path :: string(), Body :: binary() | undefined, Headers :: list()) -> no_return().
-spec stream_request(Callback :: any(), Method :: iolist(), Path :: string(), Body :: binary() | undefined, Headers :: list()) -> ok | {error, Reason :: any()}.
stream_request(Callback, Method, Path, Body, Headers) when is_list(Method); is_binary(Method), is_list(Path), is_binary(Body), is_list(Headers) ->
SocketPath = "/var/run/docker.sock",
case gun:open_unix(SocketPath, #{}) of