Deploying Clojure API with docker compose on Azure App Service.

Mohammed
2 min readAug 26, 2022

--

Preface

This post assumes that you have some basic understanding of Docker, Docker Compose, and the key terms used in the ecosystem. Should you need to get up to speed, the “Get Started” section of Docker Docs is a great place to start.

The Building Blocks

Building Docker Compose config can be divided into the 6 main steps:

  1. Create an Image for your API using Dockerfile.
  2. Create docker compose config.
  3. Build & Push to Azure container registry (ACR).
  4. Deploy docker compose config on App Service.

1. Create an Image for your API using Dockerfile.

Assuming we have a sample API written in clojure, the following could be a simple Dockerfile for it:

to build docker image use command.

$: docker build -t . clojure-api:0.0.1

2. Create docker compose config.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

You would likely have custom Dockerfiles to build an image from and this will require specifying a build context. This defines the path which the compose file will look at to build the service image. This path must contain the Dockerfile.

Following is the example of docker-compose.yml

To run container using docker compose use following command. this will build docker image using Dockerfile and then create container.

$: IMAGE=clojure-api:0.0.1 docker compose -f docker-compose.yml up -d

Run curl on local to test api it should return pong in reponse.

$: curl -s http://localhost:4000/api/ping

3. Build & Push to Azure container registry (ACR).

We’re using Azure container registry to store our docker images. tag the image with acr url and push it.

$: docker tag clojure-api:0.0.1 yourrepo.azureacr.io/clojure-api:0.0.1
$: az acr login -n yourrepo
$: docker push yourrepo.azureacr.io/clojure-api:0.0.1

4. Deploy docker compose config on App Service.

Before creating app service you need to create a copy of docker-compose.yml to docker-compose-app-service.yml.

App service by default runs on https so we have to expose 443 port on container.

Now, create app service and push docker-compose-app-service.yml to app service

Once deployed, run curl on app service URL.

$: curl https://yourapp.azurewebsite.com/api/ping

For sample clojure code and docker files please visit following git repo.

--

--