Skip to content

Run an Elixir app

In this guide we create and deploy a simple Elixir-based HTTP web server. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/http-elixir1.16/ directory:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/http-elixir1.16/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. We use fra0 (Frankfurt, 🇩🇪) in this guide:

Terminal window
# Set Unikraft Cloud access token
export UKC_TOKEN=token
# Set metro to Frankfurt, DE
export UKC_METRO=fra0

When done, invoke the following command to deploy this application on Unikraft Cloud:

Terminal window
kraft cloud deploy -p 443:3000 -M 1024 .

The output shows the instance URL and other details:

Terminal window
[] Deployed successfully!
────────── name: elixir-qo9k3
────────── uuid: e5fbf089-b000-4b2d-a827-44a1f5d28f24
───────── state: running
──────── domain: https://small-water-tl8lr8am.dal0.kraft.host
───────── image: sha256:67f5df003758a1180932e931727f8cb7006bbbf6fdd84058e27fe05e4829bba0
──────── memory: 1024 MiB
─────── service: small-water-tl8lr8am
── private fqdn: elixir-qo9k3.internal
──── private ip: 172.16.3.4
────────── args: /usr/bin/wrapper.sh /usr/local/bin/mix run --no-halt

In this case, the instance name is elixir-qo9k3 and the URL is https://small-water-tl8lr8am.dal0.kraft.host. They are different for each run.

Use curl to query the Unikraft Cloud instance of the Elixir-based HTTP web server:

Terminal window
curl https://small-water-tl8lr8am.dal0.kraft.host
Hello, World!

At any point in time, you can list information about the instance:

Terminal window
kraft cloud instance list
NAME FQDN STATE STATUS IMAGE MEMORY ARGS BOOT TIME
elixir-qo9k3 small-water-tl8lr8am.dal0.kraft.host running since 9mins sha256:67f5df0... 1.0 GiB /usr/bin/wrapper.sh /usr/local/bin/mix r... 437.43 ms

When done, you can remove the instance:

Terminal window
kraft cloud instance remove elixir-qo9k3

Customize your Application

To customize the application, update the files in the repository, listed below:

  • lib/, mix.esx: the actual Elixir HTTP server
  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified application filesystem
defmodule Server do
@moduledoc """
A Plug that always responds with a string
"""
import Plug.Conn
def init(options) do
options
end
@doc """
Simple route that returns a string
"""
@spec call(Plug.Conn.t(), any) :: Plug.Conn.t()
def call(conn, _opts) do
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, "Hello, World!\n")
end
end

The following options are available for customizing the application:

  • If only updating the implementation in the lib/server.ex or lib/server/application.ex source code files, no other changes are required. If new source files are added in the lib/ directory, no other change is required.

  • If new files are added, these have to be copied in the application filesystem, using the COPY command in the Dockerfile.

  • More extensive changes may require expanding the Dockerfile with additional Dockerfile commands.

The current source code files and configuration file (mix.exs) are generated using:

Terminal window
mix new --app server . --sup

Use a similar command to create a new application. Then update it and deploy it on Unikraft Cloud using the above instructions.

Learn More

Use the --help option for detailed information on using Unikraft Cloud:

Terminal window
kraft cloud --help

Or visit the CLI Reference.