Running Java & Scala Apps with Play! Framework in the Cloud: VPS vs Docker Hosting

| June 29, 2017 | DevOps PaaS | , , , , , , ,

play framework cloud hosting Play is a highly popular and widely used framework that facilitates building web applications with Java and Scala. Taking into consideration that this framework is in a great request among developers, today we'll consider several approaches to easily run Play applications with the help of Jelastic Cloud hosting: the first approach is based on another increasingly popular technology - Docker containers, whilst the second option presupposes running your Play project inside a Virtual Private Server (VPS).

Both these solutions provide you with the full root permissions and isolation from other accounts on the hardware. So, make up your mind on a suitable solution and proceed to one of the following instructions to run your Play application inside Jelastic.

Tip: Some steps within the following guide are to be performed via SSH protocol. If you have no experience operating with your environments through Jelastic SSH Gate yet, consider to:

Running Play Framework with Docker Containers Hosting

As a base for running Play Framework with the help of Docker containerization technology, you’ll need an image with the following basic components:

  • Operating System (OS)
  • Java Development Kit (JDK)
  • Simple Build Tool (sbt)
Note: The sbt build tool requires Java 6 or later.

You can either build Docker image on your own or deploy the ready-to-use template we’ve prepared for you and placed within the Jelastic public repository on Docker Hub. So, when you decided on the image to utilize, proceed with the following steps to launch Play.

1. Click New Environment, switch to the Docker tab, and select one of the layers on the left for an image to be placed to.play framework cloud deploymentClick Select Container.

2. In the opened dialog box, switch to one of the following tabs depending on where the preferred base template it stored:

  • Search - to fetch an image from public repository on Docker Hub (specify jelastic/sbt to use the dedicated Jelastic image we’ve mentioned above)
  • Custom - for an image from your custom Docker registry

play framework hostingTick plank with the required template and click Next.

3. Here, set resources limit for a container to be vertically scaled, specify Environment Name and a region it should be placed at. Click Create.install play framework centos4. After your new environment appears at the dashboard, access it via SSH Gate to download and unpack your Play application (we’ll use Play Java Starter Project as an example) within destination directory of created Docker container.

wget https://example.lightbend.com/v1/download/play-java-starter-example -O play-java-starter-example.zip

unzip play-java-starter-example.zipinstall play framework ubuntu5. Launch the Play development console by running the following command inside the directory with unzipped project files.

cd play-java-starter-example

./sbt

When downloading of all the required dependencies is finished, you’ll see your project was set as a current one.Play! Framework in the Cloud6. At this point, the Play console is up and you can run your application in the development mode - let’s start it on port 80 (which is commonly listened by default).

run 80play framework vpsAnd for being able to open your application via web-browser, you need to disable default Play filter for allowed hosts. Access container file system with the Config icon next to the appropriate node and add the following line to the /root/play-java-starter-example/conf/application.conf file:

play.filters.disabled+=play.filters.hosts.AllowedHostsFilter

Save the changes and Open your environment in browser with the appropriate option.dockerize play frameworkAs you can see, the Play Java Starter Project has opened successfully.

And below we’ll consider an alternative approach of running Play Framework on top of Virtual Private Server.

Running Play Framework on top of VPS

To deploy your Play application within an isolated VPS container, proceed with the following steps.

1. Click New Environment, enable Elastic VPS at any wizard tab and select one of the available stacks according to your OS preferences (for this example, we’ll utilize CentOS as a commonly used distribution for dedicated servers).install play framework centos vpsDesignate vertical scaling resources limits for this node, specify hardware region and desired Environment Name. Confirm the set parameters to Create it.

Note: VPS nodes are provisioned with the automatically attached Public IP address by default - a paid option, usually available for billing users only.

Upon the creation is finished, you’ll also receive an email notification with credentials for your Private Server administrating.

2. Now, connect to your VPS container via SSH Gate and install Java SDK by running the following commands.

cd /opt

wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie;" http:// download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.rpm 
rpm -Uvh jdk-8u162-linux-x64.rpm

play framework hosting3. Download your project (we’ll use Play Java Chatroom Project example) and unpack it by running the next commands.

wget https://example.lightbend.com/v1/download/play-java-chatroom-example -O play-java-chatroom-example.zip

unzip play-java-chatroom-example.zip

install play framework4. Start the Play development console by executing the following command inside the directory with unzipped project resources.

cd play-java-chatroom-example

./sbt

As a result, your application will be complemented with the required resources and defined as a current Play project. Play! Framework5. So, the Play console has been launched and now you can run your project in the development mode on the required port - e.g. the commonly used 80th one.

run 80install play framework ubuntu vpsAnd to gain the ability to open your application directly from Jelastic dashboard, return to it and go to the /opt/play-java-chatroom-example/conf/application.conf directory (by selecting the Config option next to the appropriate server). play framework docker imageHere, add automatically attached Public IP address to the list of allowed hosts and Save the changes.

Now you can use this IP address to open your application in a new browser tab.Running Java & Scala Apps with PlayThat’s it! The basic Java Chatroom Project is up and can be accessed through web browser.

In the similar way, you can host any Play application inside Jelastic Cloud with the preferred hosting technology - try it yourself by getting an account at one of our partner’s Cloud Hosting Platforms.

Have any questions on the Play Framework installation? Feel free to ask within the comments below or get in touch with our technical experts at Stackoverflow.