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
- Install Phoenix:
Terminal window mix archive.install hex phx_new - Create a new Phoenix app:
Terminal window mix phx.new my-phoenix-app --no-ectocd my-phoenix-app - Start the development server:
Your app should be running at http://localhost:4000.
Terminal window mix phx.server
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!") endend
Deploying Without a Dockerfile
- Push your Phoenix app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- 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
- Add a start command in your app settings:
Terminal window mix phx.server - Make sure your app listens on the port provided by Klutch.sh via the
PORT
environment variable. Inconfig/runtime.exs
:port = String.to_integer(System.get_env("PORT") || "4000")config :my_phoenix_app, MyPhoenixAppWeb.Endpoint, http: [port: port] - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official Elixir image for buildFROM elixir:1.16-alpine AS builderWORKDIR /appRUN apk add --no-cache build-baseCOPY . .RUN mix deps.get && mix assets.deploy && mix release# Use minimal image for runningFROM alpine:latestWORKDIR /appRUN apk add --no-cache libstdc++ ncurses-libs opensslCOPY --from=builder /app/_build/dev/rel/my_phoenix_app .# Expose port (match your Phoenix app)EXPOSE 4000# Start the appCMD ["bin/my_phoenix_app", "start"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- 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.