Deploy a Clojure application in Under 1 Minute

On By Puja Abbassi in tech

After having deployed a Meteor application in under 1 minute, I thought I’d try myself at another deployment. This time I wanted to go for something dynamic and having watched Simple Made Easy by Rich Hickey, the author of Clojure, I thought I’d try myself at deploying that. Again for the impatient, here the 1 minute fix:

Prerequisites

Quick Steps

  1. Put [com.palletops/uberimage "0.4.1"] into the :plugins vector of your :user profile.
  2. Add :uberimage {:tag "registry.giantswarm.io/<username>/<repo>:<tag>"} to your project’s project.clj.
  3. Open the project root and run lein uberimage. You will get back an image uuid.
  4. Run docker push registry.giantswarm.io/<username>/<repo>:<tag>.
  5. Create a simple swarm.json using the image you pushed above.
  6. swarm up
  7. Profit

Clojure mug

The Basics

One cool thing with Clojure is that with Leiningen, you get a pretty cool tool for automating projects and builds. There’s tons of plugins for Leiningen, incl. tools for development, testing, and deployment. Luckily, I stumbled upon the lein-uberimage plugin, which basically builds a Docker image that runs your Clojure project’s uberjar.

For getting this plugin running you first need a Java JDK, which you should have anyway, if you’re developing Clojure apps. You also need Leiningen installed, which again should be quite common for Clojure developers. You also need Docker to build, run, and push your image.

Building and Testing Your Application Locally

If you don’t have an own Clojure project, yet, you can clone this helloworld repo that Dennis made. Once you have a Clojure project, you need to add [com.palletops/uberimage "0.4.1"] to the :plugins vector of your :user profile. A minimal ~/.lein/profiles.clj should look like:

{:user {:plugins [[com.palletops/uberimage "0.4.1"]]}}

For more information check out the lein-uberimage GitHub repo.

Now, open the project root and run lein uberimage. You will get back an image uuid. You can test the image locally with

$ docker run -d -p 3000:8080 <image-uuid>

, where 8080 is the port that your application listens on and 3000 the port that the container exposes. Open your browser at http://<docker-host>:3000 to see if it worked.

Preparing and Pushing Your Docker Image to the Registry

However, the image we built above, does not have any name or tag. To make lein ubermage add those to the image it builds, add :uberimage {:tag "registry.giantswarm.io/<username>/<repo>:<tag>"} to your project’s project.clj.

Now you can run lein uberimage again to build an image with your desired name (<repo>) and tag. To push this image to your Giant Swarm private registry you just run

$ docker push registry.giantswarm.io/<username>/<repo>:<tag>

Running your Application on the Swarm

Having your image on the registry makes running it on the Swarm a breeze.

First, you create a short swarm.json using the image you pushed above. In my example it looks like following:

{
  "name": "clojure-test",
  "components": {
    "clojure-test": {
      "image": "registry.giantswarm.io/puja/clojure-test:1.0",
      "ports": [
        8080
      ],
      "domains": {
        "8080/tcp": [
          "clojure-puja.gigantic.io"
        ]
      }
    }
  }
}

Note: You should change the image entry to reflect your own username instead of puja.

Now, a simple swarm up and you’re there. You just deployed your Clojure project to production (or testing/staging). If everything goes right, this process shouldn’t take you more than 1 minute. If you think that’s cool, but you don’t have a Giant Swarm account, yet, be sure to request an invite. If you need help with using the plugin, you can find all details in the according GitHub repo. And if you have anything else you want to tell us, feel free to comment below or contact us directly.

Picture of Puja Abbassi
Puja Abbassi
Developer Advocate @ Giant Swarm

Let’s start your journey towards microservices

Contact us Now