How to setup Scala Play framework on Docker?

How to setup Scala Play framework on Docker?

  • Post Author:

In the previous article we showed you How to setup Scala and SBT on Docker? and in this article we are going to show you how to setup Scala Play framework in docker container.

What is Play framework?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

Prerequisite Installation

Assume that we already installed Scala, Sbt, Docker and Docker Compose.

Steps for Implementing Docker Compose

There are 4 steps for implementing docker compose:

  1. Create a Play Scala project using sbt new
  2. Create a Dockerfile for running commands
  3. Define services to run applications in docker-compose.yml file
  4. Start and run project with the help of docker-compose.yml file

1. Create a Play Scala project using sbt new

Run command below to create a new project:

$ sbt new playframework/play-scala-seed.g8

name project as my-app:

This template generates a Play Scala project 

name [play-scala-seed]:

2. Create a Dockerfile for running commands

Create a file named Dockerfile in the root of project with following content:

# Install Java and set the JAVA_HOME variable
FROM openjdk:8
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $PATH:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
 
ENV SBT_VERSION 1.3.3
 
# Install curl and vim
RUN \
  apt-get update && \
  apt-get -y install curl && \
  apt-get -y install vim
 
# Install both scala and sbt
RUN \
  curl -L -o sbt-$SBT_VERSION.deb https://repo.scala-sbt.org/scalasbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get -y install sbt

WORKDIR /var/www
COPY ./my-app /var/www

Create a new directory for your new application and configure your sbt build script with following in project/plugins.sbt:

// Typesafe snapshots
resolvers += "Typesafe Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
 
// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.1")

Create a file build.sbt in the root of project with following content:

name := "my-app"
 
version := "1.0.0-SNAPSHOT"
 
lazy val root = (project in file(".")).enablePlugins(play.sbt.PlayScala)

To ensure the proper sbt version is used, make sure you have the following in project/build.properties:

sbt.version=0.13.11

3. Define services to run applications in docker-compose.yml file

Create a file named docker-compose.yml in the root of project with following content:

version: '3.1'

services:
  app:
    build:
      context: ./
    volumes:
      - "./:/root/build"
      - ~/.sbt:/root/.sbt
      - ~/.ivy2:/root/.ivy2
      - ~/.m2:/root/.m2

4. Start and run project with the help of docker-compose.yml file

Run command below to up docker container:

$ docker-compose up

To use sbt commands we need to bash into container using docker-compose command below:

$ docker-compose run --service-ports sbt /bin/bash

Run command below to download dependencies and start the system:

$ sbt run

In a browser, enter http://localhost:9000 to view the welcome page.

we are hiring

優秀な技術者と一緒に、好きな場所で働きませんか

株式会社もばらぶでは、優秀で意欲に溢れる方を常に求めています。働く場所は自由、働く時間も柔軟に選択可能です。

現在、以下の職種を募集中です。ご興味のある方は、リンク先をご参照下さい。

コメントを残す