23 August 2017
Docker is one of the most popular container technologies in the IT world nowadays. It provides many benefits such as portability and component isolation along some other features. For more info: Docker
In this example we are going to dockerize a sample REST API which is built using Scala 2.11.11 and Play Framework 2.6.3.
Sample Play Docker app - how to run
Clone the project
compile and run
Check your app at localhost:9000
Sample JSON output (http://localhost:9000/v1/posts):
How to run an app instance inside a Docker container
In order to dockerize your play rest api example run the following steps:
Generate secret key - since docker will host our production app we need to generate a secret key.
copy the secret key (for example: "YtvWptxIlpf@Q[_dPvE[Z6NLAh_KU0YlGvwnN7sV8DKSk>PSBNbzarVp8j?=;l/y")
Create a new prod dist
Generate a new local Docker image
Run the play app inside a docker container
- replace with your generated key
To check the running docker containers run:
Output:
Misc commands
to clean the image using sbt-native-packager
- In order to stop all running docker containers
Docker configuration using sbt-native-packager
Add to build.sbt the sbt native docker configuration:
// use ++= to merge a sequence with an existing sequence dockerCommands ++= Seq( ExecCmd("RUN", "mkdir", s"/opt/docker/${packageName.value}"), ExecCmd("RUN", "mkdir", s"/opt/docker/${packageName.value}/run"), ExecCmd("RUN", "chown", "-R", "daemon:daemon", s"/opt/docker/${packageName.value}/") )
// exposing the play ports dockerExposedPorts in Docker := Seq(9000, 9443)
This will generate a
In our conf/application.conf file we need to change secret key to read from an ENV var:
This will generate a docker image based on openjdk In our conf/secure.conf we need to modify trusted proxies in order to load balance in cloud environments: