You’ve built an app in clojure and you’d like to share it with the world (or at least show it to someone)? Then one of the easiest ways to do it, is with the help of docker.

I assume that you use leiningen for your project. If not you’ll need to adapt your Dockerfile a bit, but you should get an idea how it works.

Inside the root directory of your project, create the following file with the name “Dockerfile”:

FROM clojure
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY project.clj /usr/src/app/
RUN lein deps
COPY . /usr/src/app
RUN mv "$(lein uberjar | sed -n 's/^Created \(.*standalone\.jar\)/\1/p')" app-standalone.jar
CMD ["java", "-jar", "app-standalone.jar"]

So what does this file do? It will first use the clojure image as base container, then it will retrieve all dependencies and finally it will build an uberjar (a jar containing all dependencies of your project.

A few things to note here:

  • if you have specified a name for your uberjar in your project, you must adapat the step where the *.standalone.jar is renamed to app-standalone as it wouldn’t work otherwise.

For example, if you have the following in your project.clj:

  :profiles
  {:uberjar {:omit-source true
             :aot :all
             :uberjar-name "my-awesome-app.jar"
             :source-paths ["env/prod/clj" ]
             :resource-paths ["env/prod/resources"]}

Then you will need to adapt your Dockerfile to:

FROM clojure
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY project.clj /usr/src/app/
RUN lein deps
COPY . /usr/src/app
RUN lein uberjar
CMD ["java", "-jar", "my-awesome-app.jar"]

The next step depends on where you want to deploy your docker app. If you want to deploy it on your own Linux machine, just install Docker with the package manager of your distribution and run inside your project directory:

docker run --name my-app .

On your local Windows or Mac machine, you can do something aquivalent with Docker for Windows or Docker for Mac.

If you want to share your app with someone, I can recommend you to create a droplet on Digital Ocean. There you can setup a virtual machine with Docker installed in a few minutes which will cost you a few cents per hour (you even get $100 free credit when you register the first time). It’s great when you want to deploy your app or if you want to try new technologies like e.g. databases without needing to install them on your machine.