Running Java and Scala Apps With Play: VPS and Docker

If you use Play to run your apps, you might wonder how to host it via Docker or through a VPS. Here are step-by-step instructions for the platform of your choice.

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 high 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.

As a base for running Play Framework with the help of Docker's 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 a 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.

Click New Environment, switch to the Docker tab, and select one of the layers on the left for an image to be placed to.

Click Select Container.

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 a 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.

Check the plank with the required template and click Next.

Here, set a resource limit for a container to be vertically scaled, specify your Environment Name, and a region it should be placed in. Click Create.

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 the destination directory of the 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.zip 


Launch the Play development console by running the following command inside the directory with the unzipped project files.

cd play-java-starter-example
 
./sbt

 

After downloading of all the required dependencies is finished, you’ll see your project was set as a current one.

At this point, the Play console is up and you can run your application in development mode – let’s start it on port 80 (which is commonly listened by default).

run 80

And for being able to open your application via browser, switch back to the Jelastic dashboard and add your hoster platform domain name (e.g. .jelastic.com) to the list of allowed hosts within the /app/play-java-starter-example/conf/application.conf file (to access the container file system, click the Config icon next to the appropriate node).

Save the changes and Open your environment in your browser with the appropriate option.

As you can see, the Play Java Starter Project has opened successfully.

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

Running Play Framework on Top of a VPS

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

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).

Designate your vertical scaling resources limits for this node, specify a hardware region and the 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.

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

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

cd /opt
wget –no-cookie –no-check-certificate –header “Cookie:gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk8-downloads-2133151.html; oraclelicense=accept-securebackup-cookie”“http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm“
rpm -Uvh jdk-8u131-linux-x64.rpm


Download your project (we’ll use Play Java Chatroom Project) and unpack it by running the following commands.

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

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 by the required resources and defined as a current Play project.

So, the Play console has been launched and now you can run your project in development mode on the required port – e.g. the commonly used 80th one.

run 80
And to gain the ability to open your application directly from the 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).

Here, add the 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.

That’s it! The basic Java Chatroom Project is up and can be accessed through your web browser.

In a 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.

Source: https://dzone.com/articles/running-java-an...