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.
Join the DZone community and get the full member experience.Join For Free
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:
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 .
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
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).
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.
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.
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 .
Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.