Recently I learnt how to use docker, and went ahead dockerizing a Play application which I have been developing at Groupon.
Docker is basically a kind of LXC container which provides a separate environment for your application to run. Thus we can have an App running Java 8 in a machine running Java 6, without creating any issues with the apps already running. Also, getting away from the virtualenv
in Python (which I don't personally like) to switch between versions of Python and creating custom packages and eggs
is possible with ease. I will start off with a new Java Play application. The code is on Github at https://github.com/hyades/docker-play. So we create a new Play application from the Java template -
First we get docker on our system. I installed the boot2docker on my MAC. In MAC and other non-Linux OS, docker will be running inside a VirtualBox VM. In Linux it will run natively, so only a apt-get install docker
or yum install docker
is enough to get it running.
There are two ways of creating a container. First is open up an interactive terminal like -
This will fetch a ubuntu container which will be an empty one. And then we get a prompt into the container. Here we can install whatever we want, and the finally come out and say
The second way is a better way. It is through a Dockerfile
. In the dockerfile, we can provide only the build instructions. We cannot start off any process using the Dockerfile. My Dockerfile looks like this -
All the commands in RUN
will be run when the docker container is built. The CMD
is run when the container is run. Hence it is a two step process. We also expose the port 9000 of the container. This will be used by our play application to communicate through the docker to the rest of the world! Here we install some Java into the container, and some basic things before to get our app running.
So we finally build our container.
Wait some time for all stuff to be downloaded and installed to the system. Here we use the -t
option to tag the container and give it a name, which can be easily used in further steps.
Now, once the build is done, we have a container ready. But its still not running. So we now run it
The -d
option will run the docker run
process as a daemon, the -p
option will connect to an opened port on the container. Here we connect the 9000
port of our system to the 9000
port of the container. We can check this by running
Additionally, if we are using OSX, we should open up the boot2docker's port to OSX.
This will open up the browser for you, with an IP address. Mine looks like http://192.168.59.103/.Now, if we point to http://192.168.59.103:9000, we can see the welcome message from the Play application!
So the Play application is up and running from inside the docker container!
Comments Section
Feel free to comment on the post but keep it clean and on topic.
blog comments powered by Disqus