Play Framework is a web application framework for building scalable and high-performance applications. It follows the model-view-controller (MVC) architectural pattern and is designed to be developer-friendly and efficient. With Play Framework, developers can easily build web applications using Java or Scala. It provides powerful features such as hot code reloading, asynchronous programming, and built-in testing support. Play Framework is widely used for developing modern web applications and APIs.
More about Play Framework: https://www.playframework.com/documentation/2.8.x/Home
If you’re developing a Play application and want to run it in a Docker environment, this guide will walk you through the necessary steps. Docker is a containerization technology that enables developers to package their applications as containers and deploy them on any system that has Docker installed. By using Docker, you can ensure that your application will run consistently across different environments, which can be a big advantage when it comes to deployment.
Install Docker
The first step in setting up a Docker environment for your Play application is to install Docker on your system. Docker is available for Windows, macOS, and Linux, and you can download the installer for your operating system from the official Docker website. Once you’ve downloaded the installer, follow the instructions to install Docker on your system.
Create a Dockerfile
The next step is to create a Dockerfile in the root directory of your Play application. A Dockerfile is a script that contains the necessary instructions to build a Docker image for your application. Here’s an example Dockerfile for a Play application, I’m installing java and Scala and installing the sbt to build and compile Play App:
FROM openjdk:8-jre-alpine
USER root
ENV SCALA_HOME=/usr/share/scala
##java
RUN apk update
RUN apk fetch openjdk8
RUN apk add openjdk8
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
## Scala - 3.1.3
RUN apk add --no-cache --virtual=.build-dependencies wget ca-certificates && \\
apk add --no-cache bash && \\
cd "/tmp" && \\
wget "" && \\
tar xzf "scala3-3.1.3.tar.gz" && \\
mkdir "${SCALA_HOME}" && \\
rm "/tmp/scala3-3.1.3/bin/"*.bat && \\
mv "/tmp/scala3-3.1.3/bin" "/tmp/scala3-3.1.3/lib" "${SCALA_HOME}" && \\
ln -s "${SCALA_HOME}/bin/"* "/usr/bin/" && \\
apk del .build-dependencies && \\
rm -rf "/tmp/"*
## Sbt-1.7.1
RUN \\
apk add --no-cache --virtual=.build-dependencies bash curl bc ca-certificates && \\
cd "/tmp" && \\
update-ca-certificates && \\
scala -version && \\
scalac -version && \\
curl -fsL | tar xfz - -C /usr/local && \\
$(mv /usr/local/sbt-launcher-packaging-1.7.1 /usr/local/sbt || true) && \\
ln -s /usr/local/sbt/bin/* /usr/local/bin/ && \\
apk del .build-dependencies && \\
rm -rf "/tmp/"*
COPY . .
EXPOSE 3000
RUN sbt compile -Dsbt.rootdir=true
CMD [ "sbt","-Dsbt.rootdir=true", "run" ]
This Dockerfile starts with a base image of OpenJDK 8 running on Alpine Linux.
Build a Docker Image
Once you’ve created the Dockerfile, you can use the docker build command to build a Docker image for your Play application. Open a terminal window in the root directory of your Play application and run the following command:
$ docker build -t < your_application_name >
This command tells Docker to build an image with the tag your_application_name
using the Dockerfile in the current directory. Docker will then download any required dependencies, build the image, and tag it with the specified name.
Run the Docker Container
After you’ve built the Docker image, you can use the docker run
command to start a container running your Play application. Run the following command in a terminal window:
$ docker run -p 9000:9000 < your_application_name >
This command maps port 9000 in the container to port 9000 on the host machine, so you can access your Play application by navigating to http://localhost:9000
in your web browser.