Skip to content

Deploying a Phoenix App

Phoenix is a modern web framework for Elixir, designed for building scalable, high-performance web applications. It features real-time capabilities, a productive developer experience, and leverages the power of the Erlang VM for fault-tolerance and concurrency.

This guide explains how to deploy a Phoenix application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • Elixir & Erlang installed (Install guide)
  • Phoenix installed
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Phoenix

  1. Install Phoenix:
    Terminal window
    mix archive.install hex phx_new
  2. Create a new Phoenix app:
    Terminal window
    mix phx.new my-phoenix-app --no-ecto
    cd my-phoenix-app
  3. Start the development server:
    Terminal window
    mix phx.server
    Your app should be running at http://localhost:4000.

Sample Code (lib/my_phoenix_app_web/controllers/page_controller.ex)

Add a simple controller to your Phoenix project:

defmodule MyPhoenixAppWeb.PageController do
use MyPhoenixAppWeb, :controller
def index(conn, _params) do
text(conn, "Hello from Phoenix on Klutch.sh!")
end
end

Deploying Without a Dockerfile

  1. Push your Phoenix app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Phoenix GitHub repository and branch
    • Set the port to route traffic (usually 4000 for Phoenix)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    mix phx.server
  6. Make sure your app listens on the port provided by Klutch.sh via the PORT environment variable. In config/runtime.exs:
    port = String.to_integer(System.get_env("PORT") || "4000")
    config :my_phoenix_app, MyPhoenixAppWeb.Endpoint, http: [port: port]
  7. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official Elixir image for build
    FROM elixir:1.16-alpine AS builder
    WORKDIR /app
    RUN apk add --no-cache build-base
    COPY . .
    RUN mix deps.get && mix assets.deploy && mix release
    # Use minimal image for running
    FROM alpine:latest
    WORKDIR /app
    RUN apk add --no-cache libstdc++ ncurses-libs openssl
    COPY --from=builder /app/_build/dev/rel/my_phoenix_app .
    # Expose port (match your Phoenix app)
    EXPOSE 4000
    # Start the app
    CMD ["bin/my_phoenix_app", "start"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: Your Phoenix app should always listen on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.