k-p

k-p

Connecting Livebook to a node on a remote docker container

I have an Elixir app running on a remote server using Dokku. I’d like to connect to it from my local machine using Livebooks, via a SSH tunnel.

I can’t seem to make the remote container visible to my local Livebook.

I’ve tried the top StackOverflow comments and have tried setting RELEASE_NODE, RELEASE_NAME, and RELEASE_DISTRIBUTION. I have RELEASE_COOKIE configured and am using this.

I have tried using RELEASE_NODE as <app_name>@localhost, <app_name>@127.0.0.1, and <app_name>@<server_ip>.

I gather there’s some shenanigans trying to get EPMD to see the remote node. I’ve tried setting ERL_DIST_PORT to 9000, forwarding Dokku’s host 9000 to the container’s 9000, and opening an SSH tunnel with 9000 open.

Is there a straightforward-ish solution or problem I’m missing? I appreciate there’s a few moving parts.

Most Liked Switch mode

jonatanklosko

jonatanklosko

Creator of Livebook

@_jonas you linked to ERL_EPMD_PORT env var, which indeed sets the port for epmd to listen on. However, the -erl_epmd_port flag sets the node distribution port. The fact that they are named the same is unfortunate, however in recent OTP the flag has been deprecated in favour of kernel application parameter named erl_epmd_node_listen_port, which is more accurate.

jonatanklosko

jonatanklosko

Creator of Livebook

Hey @k-p! There are a few elements

  1. The remote node needs to start distribution on a known port. I believe ERL_DIST_PORT is used by rebar3/relx, not the Elixir releases. To force a specific port you can do ELIXIR_ERL_OPTIONS="-erl_epmd_port 9000" (or pass it in vm.args).

  2. The distribution port needs to be forwarded to a local port, let’s assume it’s the same.

  3. The remote node needs to have 127.0.0.1 hostname (or if it uses a domain name, you can edit your local /etc/hosts to resolve it to 127.0.0.1).

  4. With the above steps, the node appears as if it was running locally. However, the missing piece is that the local EPMD doesn’t know about this node. You can register the node manually by running this script:

    # epmd_register_node.exs
    
    defmodule EPMD do
      def epmd_register_node(node_name, port) do
        # Registers the given node under the given port in EPMD.
        #
        # We open a TCP connection to EPMD and send the registration
        # request. We keep the socket open. The node is automatically
        # unregistered when the calling process terminates.
        #
        # See the EPMD protocol [1] and the reference implementation [2].
        #
        # [1]: https://www.erlang.org/doc/apps/erts/erl_dist_protocol.html#register-a-node-in-epmd
        # [2]: https://github.com/erlang/otp/blob/OTP-27.0/lib/kernel/src/erl_epmd.erl#L403-L433
    
        epmd_host = {127, 0, 0, 1}
        epmd_port = 4369
    
        case :gen_tcp.connect(epmd_host, epmd_port, [:binary, packet: :raw, active: false]) do
          {:ok, socket} ->
            request =
              <<
                # ALIVE2_REQ
                120::8,
                # Node distribution port
                port::16,
                # Node type (normal)
                77::8,
                # Protocol (TCP/IPv4)
                0::8,
                # Highest and lowest version of the distributino protocol,
                # see https://github.com/erlang/otp/blob/OTP-27.0/lib/kernel/include/dist.hrl#L88-L89
                6::16,
                6::16,
                # Node name
                byte_size(node_name)::16,
                node_name::binary,
                # Extra
                0::16
              >>
    
            data = <<byte_size(request)::16, request::binary>>
            :ok = :gen_tcp.send(socket, data)
    
            case :gen_tcp.recv(socket, 0) do
              {:ok, data} ->
                result =
                  case data do
                    # ALIVE2_X_RESP
                    <<118, result::8, _creation::32>> -> result
                    # ALIVE2_RESP
                    <<121, result::8, _creation::16>> -> result
                  end
    
                if result == 0 do
                  :ok
                else
                  :gen_tcp.close(socket)
                  {:error, "failed to register node in EPMD, result code: #{result}"}
                end
    
              {:error, reason} ->
                :gen_tcp.close(socket)
                {:error, "failed to receive response from EPMD, reason: #{inspect(reason)}"}
            end
    
          {:error, reason} ->
            {:error, "failed to connect to EPMD, reason: #{inspect(reason)}"}
        end
      end
    end
    
    
    EPMD.epmd_register_node("mynodename", 9000) |> IO.inspect()
    
    Process.sleep(:infinity)
    

    Make sure to change the node name at the end, it should be the base name, without the hostname part. The registration is kept until you kill the script.

Technically steps 3. and 4. could be done by using a custom EPMD module, but since we are talking about Livebook, the solution assumes no changes to the EPMD module. (In fact, Livebook already uses a custom EPMD for a similar purpose).

jonatanklosko

jonatanklosko

Creator of Livebook

Just to make sure, you are using RELEASE_DISTRIBUTION=name, RELEASE_NODE=app@127.0.0.1 and the same cookie?

If I enter the container, and run epmd -names I can see that epmd is still running on port 4369 and the node (name app at port 9000) is running on port 9000, with the config set in step one. Is that right? The argument to me reads as though epmd should be on port 9000.

This is correct. The argument name is confusing, and in fact it’s going to change in the next OTP version. It is supposed to set the port that the node uses for distribution, not the EPMD port itself. So name app at port 9000 looks good.

If the connection is refused, the only thing I can think of is something with the forwarding. For the ssh forwarding maybe try 127.0.0.1 and not localhost, unless you already do that. Otherwise it could be the port forwarding to Docker.

Last Post!

_jonas

_jonas

Ah, that is good to know!

I thought when running without epmd, there would be some way to manually specify the distribution port of another node to connect to, similar to how you would normally specify the name.
I guess I was mistaken – then I suppose your suggested way of running with a local epmd and registering the remote node manually is indeed the best option, thanks again for explaining.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement