January Gnarly Learnings #1: Local services with asdf and foreman
Tags: webdev, postgres, tutorial, docker
Published: 2022-01-19 23:20:20


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewlfuo72wsudua2lfg67.png)

Upon learning of <code>asdf</code> plugins for backing services like Postgres, Redis, Elasticsearch and others, my first question was whether and to what extent it could replace Docker for providing those services in local development, where asdf already shines for versioning.

The first obstacle for most users will be that <code>asdf</code> has no equivalent of <code>docker compose up</code> for booting and monitoring an entire local environment. (As of January 2022 <a href="https://github.com/asdf-vm/asdf/discussions">the <code>asdf</code> discussions page is recently dark</a> but this was an active topic at the time of its disappearance.) Brainstorming the matter with a colleague, he pointed out we were essentially proposing Foreman. A few moments later he found this:

<a href="https://kassioborges.com/2022/03/01/rails-project-development-dependencies.html_">Managing Ruby on Rails development dependencies with <code>asdf</code> and <code>foreman</code></a>

[Note: We're talking about <a href="https://github.com/ddollar/foreman">Foreman</a>, not <a href="https://github.com/theforeman/foreman">The Foreman</a>.]

From the oldie-but-a-goodie department: Foreman is a simple tool that reads a list of command line tasks from a file, starts them, feeds their output to <code>stdout</code> with some nice formatting, and eventually shuts them down when you exit the process; i.e., exactly what <code>asdf</code> doesn't provide. As outlined above, there isn't even really any "integration" necessary for <code>asdf</code> and Foreman to play nicely. They just do their own things correctly.

I've been using the approach above where possible for a couple of weeks now, and it's nice. (Be sure to note the trick in the blog post for running Postgres under Foreman control!) Docker is, among other things, a significant resource commitment for local development. On a Mac, you're spinning up an entire separate Linux instance to run software that MacOS is quite capable of running itself. This might be a good tradeoff; for many the benefit of standardizing developer experience or duplicating an idiosyncratic production environment will outweigh other considerations. But for individual developers looking for lightweight solutions—or to avoid duplicating functionality already provided by the kernel, the shell, <code>asdf</code> and various language-specific dependency managers—this solution is very much worth a look.

<h2>Streamlining</h2>
I created the following in <code>~/bin/app</code>, which will probably get (a little) more clever over time:

<pre><code>#!/usr/bin/env zsh

foreman start -f Procfile.local</code></pre>
<p></p>
I used <code>Procfile.local</code> to prevent conflicts with applications that already have a <code>Procfile</code> for production. In this case we're booting a Phoenix project using Postgres and Redis:

<pre><code>postgres: mkdir -p log/ && postgres-local
redis: redis-server
api: mix phx.server</code></pre>
<p></p>
Meanwhile in <code>.tool-versions</code>:

<pre><code>elixir main-otp-24
erlang 24.2
postgres 14.1
redis 6.2.6</code></pre>
<p></p>
Booting the application is nice and simple:
<pre><code>$ app
17:05:22 postgres.1 | started with pid 12345
17:05:22 redis.1    | started with pid 12346
17:05:22 api.1      | started with pid 12347
[...logging continues, ctrl-c to exit...]</code></pre>
<p></p>
<h2>Future possibilities, difficulties, etc.</h2>
The main pain point I see is running multiple applications simultaneously. Docker virtualizes an entire operating system, so if you have two applications, A and B, which both use the same version of (say) Postgres, they still get entirely separate installations, data directories, port 5432 to bind, and so on. With this approach, you have one version of Postgres.

One answer is to separate out the runtime specifics of the service from the codebase, which will presumably depend on the nature of the service itself, but follow a general shape:

<ul>
	<li>Environment variables</li>

	<li>Configuration files</li>

	<li>Persistent data storage (your database)</li>

	<li>Temporary data storage (logging, lock files)</li>
</ul>

<p></p>
If you can specify those uniquely for each instance in <code>Procfile.local</code>, it's likely feasible to run multiple instances of the same service simultaneously. My own research into that will occur when/if I need to run several unrelated applications simultaneously, which is not usually how I work.

<h2>Credit where credit is due</h2>
Big h/t to Kassio Borges for having thought of this before any of us over here. That link again: <a href="https://kassioborges.com/2021/03/01/rails-project-development-dependencies.html">Managing Ruby on Rails development dependencies with <code>asdf</code> and <code>foreman</code></a>