Sitemap

Dockerized Clojure API + Datomic

3 min readJun 21, 2024

--

Press enter or click to view image in full size

Yes… this is another CRUD using clojure ¯\_(ツ)_/¯ … As I didn't find any github repository with clojure API connecting to a datomic database, I decided to make my own… So I'm gonna write some articles to share my recent studies.

In this first part, how to configure a simple clojure API and to connect it to an dockerized datomic database. I'm going to use my repo as code base to highlight some important aspects, the full github repository is here and if you want to skip all this talk and run the full project, the README should be enough… so let's start.

… Another thing … before the start, install Docker and docker-compose in your machine =P.

Clojure API

First of all you can clone the complete clojure API here, and I will explain some important topics about the code to make things work, let's start with the dependencies that we are using. I'll highlight only com.datomic/perr to connect with our datomic database, and metosin/compojure-api that I'll use here to expose the API and provide a very simple way to expose it with swagger, more info here.

The full project.clj to ilustrate it (you don't have to worry with the other dependencies for now)

Ok.. now let's take a look in our com.code4nimbus.clojureapi.core file (that we configured as the :main namespace in project.clj)

In Clojure, the -main function is the entry point for a program, so all we need to do to ensure application state will start here.

To facilitate the code comprehension I'm using the hexagonal pattern, there's a very nice article here if you are interested.

But for now… I'll highlight two aspects of this project: connecting to the database and run the server.

The database connection

The API endpoints

You can just ignore prometheus registry for now, I'll explain in another article.

Get Felipe Pelichero’s stories in your inbox

Join Medium for free to get updates from this writer.

And to expose with swagger you just need to encapsulate it (as shown below)

Now… Dockerizing it!

I'm using the clojure official base image , as the code bellow

At this point, you should be able to build you dockerfile but before run it, let's we need the datomic database.

To run it you will have to configure the transactor to allow remote access (as the code bellow) , that will allow a container to conect another inside docker's network, and you can read more in oficial documentation if you want more details…

To make things easier you can just clone the project here.

We will use docker-compose, to put thing up run the following command in the projects root folder

docker compose up

Acessing http://localhost:9000/swagger you should be able to access the swagger page of clojure's API already connected to datomic!

Press enter or click to view image in full size

… and accessing http://localhost:9091/browse you should be able to query data from datomic's console.

Press enter or click to view image in full size

I'll try to split the next steps in another articles to show how to extract metrics into a dockerized grafana, connecting to kafka, produce messages, etc… but that's all for now! … The full project is available on github.

--

--