リモート開発メインのソフトウェア開発企業のエンジニアブログです

How to setup Scala Play framework on Docker?

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.

← 前の投稿

Puma を systemd のユーザーサービスとして起動する

次の投稿 →

AWS AthenaでINSERTするときに困ったこと

コメントを残す