Howto deploy Clojure DigitalOcean App Platform

Howto deploy Clojure DigitalOcean App Platform

Developing an application in Clojure is fun and productive, but deploying the application can be a challenge for people who have not studied infrastructure (DevOps). DigitalOcean has a product called App Platform, which is a PaaS (Platform as a Service), a platform that makes it easy to deploy applications in various languages, including Clojure.

DigitalOcean Referral Badge

In this tutorial, we will share how to deploy an application using Docker on DigitalOcean’s App Platform. In this case, we will have a Docker image for Clojure - you can follow the same tutorial to deploy other languages by writing the Docker image.

Using Docker

App Platform supports deploying applications in Docker containers. To deploy a Clojure application, you need to create a Dockerfile using the official Clojure Docker image (docker.io/clojure:latest), install your application’s dependencies, and run the .jar.

To do this, simply create a file called Dockerfile at the root of the repository, which the App Platform will use to build and deploy the application.

The example below shows how we do it at moclojer.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM docker.io/clojure:temurin-21-tools-deps-alpine AS jar-build
RUN apk add git
WORKDIR /app
COPY . .
RUN clojure -M:dev --report stderr -m com.moclojer.build --uberjar
ENV PORT="8000"
ENV HOST="0.0.0.0"
ENV CONFIG="/app/moclojer.yml"
EXPOSE ${PORT}
VOLUME ${CONFIG}
ENTRYPOINT "java" "-jar" "/app/moclojer.jar"

Creating a basic HTTP server in Clojure

It might be a bit confusing, but don’t worry

To simplify understanding, we will create a basic HTTP server in Clojure (reitit + ring) and deploy it on the App Platform - this way we can ensure that you understand how the whole process works.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
(ns sample-app.core
  (:gen-class)
  (:require [reitit.ring :as ring]
            [ring.adapter.jetty :as jetty]))

(def app
  "ring router app"
  (ring/ring-handler
   (ring/router
    ["/" {:get (fn [_]
                 {:status 200, :body "http server running!"})}]
    ["/ping" {:get (fn [_]
                     {:status 200, :body "pong!"})}])
   (ring/create-default-handler)))

(defn -main
  "software entry point"
  [& _]
  (jetty/run-jetty #'app {:port 3000, :join? false})
  (println "server running in port 3000"))

[[2024_06_18]]

Creating the application on App Platform

1. Access the App Platform and click on Create App

create in app platform

2. Select the Github repository that you created with the Clojure project (if you haven’t done so, you can fork our repository)

select repository in app platform

3. Select the project branch, we recommend main and the directory that contains the application (if you work with a monorepo, this is where you select the application directory)

select branch/dir in app platform

4. Click Next and select the Region where the application will run

select region in app platform

5. Review the information, click Create Resources and wait for the application to deploy

review and create in app platform deploy in app platform done

Done! Your Clojure application is running on DigitalOcean’s App Platform

Here is the domain of your application running on the App Platform:

link your in app platform done