3
使用Erlang和Docker容器。我在兩個容器中創建了一個玩具應用:客戶端和服務器。這裏的碼頭工人,compose.yml文件:net_adm:從一個容器ping到另一個容器
version: '2'
services:
client:
image: test3_client
links:
- server
server:
image: test3_server
這裏是服務器的Dockerfile
FROM erlang:19.0
ADD . /app
WORKDIR /app
CMD erl -sname server -setcookie abc -noshell -s test3 start
(客戶端是大同小異,唯一的名稱client
和未來的中TEST3的client
功能) 。下面是二郎代碼:
-module (test3).
-compile(export_all).
start() ->
register(greeter, spawn(fun() -> loop() end)) .
loop() ->
io:format("server loop~n"),
receive
{greeting, Sender, Name} ->
io:format("greeting: ~w~n", [Name]),
Sender ! {response, self(), "Yowser " + Name}
end,
loop() .
client() ->
timer:sleep(250),
ok = ping_server(10),
greeter ! {greeting, self, "diego"},
receive
{response, Pid, Greeting} ->
io:format("I was greeted: " + Greeting + "!~n")
end .
ping_server(Count) ->
case Count of
X when X < 1 ->
{error, "unable to reach server"};
_ ->
case net_adm:ping(server) of
pong ->
io:format("found server~n"),
ok ;
pang ->
io:format("unable to reach server~n"),
timer:sleep(1000),
ping_server(Count - 1)
end
end .
當我運行的容器,用docker-compose up
,我得到這個:
docker-compose up
Creating network "test3_default" with the default driver
Creating test3_server_1
Creating test3_client_1
Attaching to test3_server_1, test3_client_1
server_1 | server loop
client_1 | unable to reach server
client_1 | unable to reach server
client_1 | unable to reach server
client_1 | unable to reach server
client_1 | unable to reach server
. . .
最終客戶感到厭煩並放棄。
你能看到我要去哪裏嗎?
我見過Chris Smith的this post,這真的很不錯。但克里斯使用套接字,而我想利用本地Erlang消息傳遞...
我試了兩種方法 - 我使用「服務器@本地主機」,因爲這兩個容器都在同一主機上,應該已經工作... –
我更改了erlang虛擬機啓動參數,使用'erl -name server @ servernode「並相應地更改了ping,結果仍然是」pang「,即使當我打印出服務器的節點名稱時也是如此。 –
如果您在Docker運行時在主機上運行'epmd -names',它會顯示docker Erlang節點嗎? – kjw0188