How to Deploy Phoenix Applications to Packet with Nanobox

Phoenix is an incredibly powerful, fast, and flexible web framework built in Elixir. It benefits from Elixir's use of the Erlang VM, known for running low-latency, distributed and fault-tolerant systems. Packet offers performant, affordable, bare-metal servers in data centers around the world.

In this article, I'm going to walk through deploying a Phoenix application to Packet using Nanobox. Nanobox uses Docker to build local development and staging environments, as well as scalable, highly-available production environments on Packet.

Download Nanobox

Go ahead and create a Nanobox account and download Nanobox Desktop, the Nanobox CLI.

Setup Your Phoenix Project

Whether you have an existing Phoenix project or are starting the scratch, the process of configuring it for Nanobox is the same.

Note: In this tutorial, I'll be using Phoenix 1.3. If using 1.2, just change all the phx references to phoenix.

Add a boxfile.yml

Nanobox uses the boxfile.yml to build and configure your app's environment both locally and in production. Create a boxfile.yml in the root of your project with the following:

  engine: elixir
    - nodejs
    - inotify-tools
    - node_modules
    - node_modules/.bin
  fs_watch: true

    - mix phoenix.digest
      - mix ecto.create --quiet
      - mix ecto.migrate

  image: nanobox/postgresql:9.5

  start: node-start mix phx.server

This includes everything Phoenix needs to run. You may need to update a few items specific to your project, but in this walk-through, I'm going to use:

  • An Elixir runtime
  • Node.js packages and settings for Brunch asset compilation
  • Filesystem watching for local code reloading
  • Inotify for local filesystem notifications and code reloading
  • Static asset digest generation on deploy
  • Database creation/migration on deploy
  • A Postgres database
  • A web component that acts as a public webserver when deployed

Start the Local Dev Environment

With the boxfile.yml in place, you can fire up a virtualized local development environment. I recommend adding a DNS alias just so the app will be easier to access from a browser.

# Add a convenient way to access the app from a browser
nanobox dns add local phoenix.local

# Start the dev environment
nanobox run

Nanobox will provision a local development environment, spin up a containerized Postgres database, mount your local codebase into the VM, load your app's dependencies, then drop you into a console inside the VM.

Generate a New Phoenix Project

If you have an existing Phoenix project, you can skip this section. To generate a new Phoenix project from scratch, run the following from inside the Nanobox console:

# cd into the /tmp dir to create an app
cd /tmp

# Install Phoenix
mix archive.install

# Generate a new Phoenix application
mix app #(Do not fetch dependencies at this moment)

# cd back into the /app dir
# Enable the hidden files shell option
# Copy the generated app into the project dir
cd -
shopt -s dotglob
cp -a /tmp/app/* .

Your project's current working directory is mounted into the /app directory in the VM, so all the Phoenix files written there will propagate back down to your machine's filesystem and vice versa.

Update the Database Connection

When Nanobox spins up a Postgres database, it generates environment variables for the necessary connection credentials. Update the database connection in your config/dev.exs, config/test.exs, and config/prod.secret.exs.

# Configure your database
config :app, App.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: System.get_env("DATA_DB_USER"),
  password: System.get_env("DATA_DB_PASS"),
  hostname: System.get_env("DATA_DB_HOST"),
  database: "gonano",
  pool_size: 10

Note: The gonano database is created as a default database when Postgres is provisioned, but you're welcome to create your own and use that.

Fetch Application Dependencies

To load your app's dependencies, run the following from the root of your project:

mix deps.get

Fetch Asset Dependencies

Phoenix uses Brunch for asset compilation, which is installed as a Node.js package. Both npm and yarn are included in the nodejs extra_package. I recommend using yarn, since it's a lot faster than npm.

If you're working with an existing Phoenix project, Brunch will have been installed as your app was built and installed in the dev environment. If you started from scratch, run:

cd assets && yarn install
cd ../

Run Your Database Migration

With a fresh Postgres install, you'll need to create your database structure.

mix ecto.create

Run Phoenix Locally

With your database connection updated and dependencies loaded, you're ready to start Phoenix in your local dev environment. From the /app directory in your Nanobox console:

mix phx.server

You'll then be able to access your running Phoenix app at phoenix.local:4000.

Whenever you exit out of the Nanobox console, it'll shut your VM down and drop you back into your host OS.

Prepare Phoenix for Deploy

Before you deploy the project, make sure the app is listening on port 8080. This is configured in your config/prod.exs:

config :app, AppWeb.Endpoint,
  http: [port: 8080],
  load_from_system_env: true,
  url: [host: "", port: 80],
  cache_static_manifest: "priv/static/manifest.json"

Starting Phoenix in Production

You should already have a web component included in the boxfile.yml above. This tells Nanobox to create a publicly accessible webserver when the app is deployed. Notice that the web component's start command is prepended with node-start.

  start: node-start mix phx.server

node-start is a helper included in Nanobox Elixir engine that ensures nodes are started with credentials sufficient to cluster and attach to after they are running. Using this, you'll be able to scale your web component into a horizontal cluster without any extra configuration.

Alright! Now to the fun part!

Setup Your Packet Account

If you haven't already, create a Packet account.

Add a New API Key

In your Packet portal, click on "API Keys" in the left nav and add a new API key.

Add an API Key

Give your key a description, Read/write privileges, and click "Generate".

API Key Details

Copy your API Key's Token. You'll need this later.

API Key Token

Create a New Project

"Projects" are Packet's way of grouping ordered servers under a single payment method. Go to the "Manage" tab in your left nav and create a new project.

Create a New Project

Give it a name and assign it a payment method, then click "Create Project".

Project Name & Payment Method

Click on the "Settings" tab inside your new Project, and copy your Project ID. You will need it.

Create a New Provider Account

In your Nanobox dashboard, go to the Hosting Accounts section of your account admin and click "Add Account", select Packet, and click "Proceed".

Add a New Packet Provider

Enter the required credentials.

Enter Packet Auth Credentials

Click "Verify & Proceed". Name your provider, select your default region, then click "Finalize/Create".

Name Your Provider & Select a Default Region

Launch a New App

Go to the home page of your Nanobox dashboard and click the "Launch New App" button. Select your Packet provider from the dropdown and choose the region in which you'd like to deploy your app.

Select your Packet provider

Confirm and click "Let's Go!" Nanobox will order an server on Packet under your account. When the server is up, Nanobox will provision platform components necessary for your app to run:

  • Load-Balancer: The public endpoint for your application. Routes and load-balances requests to web nodes.
  • Monitor: Monitors the health of your server(s) and application components.
  • Logger: Streams and stores your app's aggregated log stream.
  • Message Bus: Sends app information to the Nanobox dashboard.
  • Warehouse: Storage used for deploy packages, backups, etc.

Once all the platform components are provisioned and running, you're ready to deploy your app.

Stage Your App Locally

Nanobox provides "dry-run" functionality that simulates a full production deploy on your local machine. This step is optional, but recommended. If the app deploys successfully in a dry-run environment, it will work when deployed to your live environment.

nanobox deploy dry-run

More information about dry-run environments is available in the Dry-Run documentation.


Add Your New App as a Remote

From the root of your project directory, add your newly created app as a remote.

nanobox remote add app-name

This connects your local codebase to your live app. More information about the remote command is available in the Nanobox Documentation.

Deploy to Your Live App

With your app added as a remote, you're ready to deploy.

nanobox deploy

Nanobox will compile and package your application code, send it up to your live app, provision all your app's components inside your live server, network everything together, and boom! Your app will be live.

Manage & Scale

Once your app is deployed, Nanobox makes it easy to manage and scale your production infrastructure. In your Nanobox dashboard you'll find health metrics for all your app's servers/containers. Your application logs are streamed in your dashboard and can be streamed using the Nanobox CLI.

Although every app starts out on a single server with containerized components, you can break components out into individual servers and/or scalable clusters through the Nanobox dashboard. Nanobox handles the deep DevOps stuff so you don't have to. Enjoy!

Posted in Phoenix, Elixir, Packet, Deployment