Packaging an Akka-http Application using SBT and Docker: Simple Example
Last updated:- Prerequisites: Docker and SBT
- Clone the sample app from github
- Examining the files
- Packaging and Running the Project
- Testing the app
- Stop the container
This post will walk you through setting up a minimal akka-http example (including Json marshalling/unmarshalling, accessing external resources and other goodies) and making it ready for packaging with docker.
We'll be using sbt-native-packager, which you can use not only to create Docker containers but a variety of other package formats (e.g. deb, rpm, msi and zip).
TL;DR: See sample project here: akka-http-docker-minimal-seed
Prerequisites: Docker and SBT
Clone the sample app from github
$ git clone https://github.com/queirozfcom/akka-http-docker-minimal-seed.git
Examining the files
I won't go into the details of how to put together an akka-http application here; the code is heavily commented so you should be able to pick up what you should do from the example.
There are a couple of files that you need to take special notice of:
build.sbt
The first line is used to configure what type of package you want. For Docker, use
JavaAppPackaging
(even though this is a Scala project!):lazy val root = (project in file(".")).enablePlugins(JavaAppPackaging)
This is where you set the Docker container name:
packageName in Docker := "akka-http-docker-minimal-seed"
The following line is needed to inform Docker we want open a port on the container so we can access from outside (there may be more than one)
dockerExposedPorts := Seq(5000)
project/plugins.sbt
The following line
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.1")
adds the plugin to sbt.
Packaging and Running the Project
To package the app, you need to run a single command:
$ sbt docker:publishLocal
This will package your akka-http app into a Docker container and make it available for running with Docker.
After generating the container (step above), run the container:
$ docker run -dit -p 5000:5000 --name akka-minimal-seed akka-http-docker-minimal-seed:1.0
You will get a response like this:
$ docker run -dit -p 5000:5000 --name akka-minimal-seed akka-http-docker-minimal-seed:1.0
6c3ceda8b0a4dc67633c577bb57dd949e17afbc101fd1b190c2e67efba9c9b7f
This will run in the background (because of the -d
switch) so you won't see
Testing the app
Open address on a web browser: http://localhost:5000/healthcheck
To test using Postman, use this link:
Stop the container
To stop the container, run:
$ docker stop akka-minimal-seed
If you prefer to remove the container rather than just stop it, just run docker rm
instead of docker stop
.