Category: English


In this tutorial, I will guide you running your Play framework application (named MyProject in our tutorial) via Docker. I assume you have created a Play project with a build.sbt (or equivalent) file and make sure it's running on port 9000 with sbt run command.

Our demo project pulls tweets from Twitter of a specified person so it requires environment variables CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET to be set before running.

Docker Installation

Make sure you have installed the latest version of Docker Toolbox. In order to use docker command, Docker needs some environment variables to be set. To do this, Docker introduces Docker QuickStart Terminal.

Run Docker Quickstart Terminal from Launchpad. A new terminal tab will open either on Terminal or iTerm app (depending on which one you chose at the installation). Check if any docker containers exist:

$ docker ps -a

Now lets check the DOCKER_HOST (we will use this IP instead of localhost when reaching the app).


You will see something like tcp:// This means we will use IP address for reaching our apps.

Building Docker Image with SBT

Now that we have Docker installed and running, we can build a Docker image for our Play App. We can use SBT Native Packager plugin's docker feature to build a Docker image for the Play app (named MyProject). Before using the plugin, we have to add some config parameters to build.sbt of the Play project:

dockerExposedPorts := Seq(9000, 9443)
dockerExposedVolumes := Seq("/opt/docker/logs")

This will add instructions to the Dockerfile to expose 9000 and 9443 ports and let us read logs outside the container. Then build the image:

$ cd MyProject
$ sbt docker:publishLocal

Now check docker images:

$ docker images

you will see a table listing all docker images available.

myproject1.0-SNAPSHOTcd25bf99efa92 minutes ago925.8 MB
javalatest7547e52aac4b3 weeks ago817.5 MB

Creating the Container

We now see the image named myproject created for our app. Let's create a container with this image with environment variables and ports added.

$ docker run --name MyProject -p 9000:9000 -e CONSUMER_KEY=abcd -e CONSUMER_SECRET=abcd -e ACCESS_TOKEN=abcd -e ACCESS_TOKEN_SECRET=abcd myproject:1.0-SNAPSHOT

This command means:

  • use myproject image with 1.0-SNAPSHOT tag.
  • expose port 9000 (right one) of inner Play app to 9000 (left one) of the outer system.
  • name the container as myproject.
  • give environment variables CONSUMER_KEY, CONSUMER_SECRET... with -e parameter for each.

Console will show logs for the app and navigate to (the DOCKER_HOST) to reach the app. You can stop the container with Ctrl/CMD+C.

Restarting the Container

$ docker ps -a

you will see the container has exited.

c9b768dac51emyproject:1.0-SNAPSHOT"bin/myproject"10 minutes agoExited (130) 30 seconds agoMyProject

Now restart the container (initials of the id is sufficient):

$ docker start c9b
$ docker ps -a

And we see the running containers:

c9b768dac51emyproject:1.0-SNAPSHOT"bin/myproject"11 minutes agoUp 13 seconds0.0.0.0:9000->9000/tcp, 9443/tcpMyProject

Stopping the Container

When you restart the container, it will run in the deamon mode. you can stop it via:

$ docker stop c9b

We're done!

Share: FacebookGoogle+Email

Receive notification on my new posts

* required
* Different contents, not direct translation. You may choose both if you wish.
comments powered by Disqus