If you want to deploy your Dropwizard Application on a Docker server, you can Dockerize your Dropwizard Application. Since a Dropwizard Application is already packaged as an executable Java ARchive file, creating a Docker image for such an application should be easy.
In this blog, you will learn how to Dockerize a Dropwizard Application using 4 easy steps.
Before you start
- You are going to use the Dropwizard-example application, which can be found at the Dropwizard GitHub repository.
- Additionally you need Docker. I used Boot2Docker to run the Dockerized Dropwizard Application on my laptop. If you use boot2Docker, you may need this Boot2Docker workaround to access your Dockerized Dropwizard application.
- This blog does not describe how to create Dropwizard applications. The Dropwizard getting started guide provides an excellent starting point if you like to know more about building your own Dropwizard applications.
Step 1: create a Dockerfile
You can start with creating a Dockerfile. Docker can automatically build images by reading the instructions described in this file. Your Dockerfile could look like this:
FROM dockerfile/java:openjdk-7-jdk
ADD dropwizard-example-1.0.0.jar /data/dropwizard-example-1.0.0.jar
ADD example.keystore /data/example.keystore
ADD example.yml /data/example.yml
RUN java -jar dropwizard-example-1.0.0.jar db migrate /data/example.yml
CMD java -jar dropwizard-example-1.0.0.jar server /data/example.yml
EXPOSE 8080
The Dropwizard Application needs a Java Runtime, so you can start from an base image already available at Docker Hub, for example: dockerfile/java:openjdk-7-jdk.
You must add the Dropwizard Application files to the image, using the ADD instruction in your Dockerfile.
Next, simply specify the commands of your Dropwizard Application, which you want to execute during image build and container runtime. In the example above, the db migrate command is executed when the Docker image is build and the server command is executed when you issue a Docker run command to create a running container.
Finally, the EXPOSE instruction tells Docker that your container will listen on the specified port(s) at runtime.
Step 2: build the Docker image
Place the Dockerfile and your application files in a directory and execute the Docker build command to build an Docker image.
docker@boot2docker:~$ docker build -t dropwizard/dropwizard-example ~/dropwizard/
In the console output you should be able to that the Dropwizard Application db migrate command is executed. If everything is ok, the last line reported informs you that the image is successfully build.
Successfully built dd547483b57b
Step 3: run the Docker image
Use the Docker run command to create a container based on the image you have created. If you need to find your image id use the Docker images command to list your images. It should take around 3 seconds to start the Dockerized Dropwizard example application.
Docker run –p 8080:8080 dd547483b57b
Notice that I included the –p option to include a network port binding, which maps 8080 inside the container to port 8080 on the Docker host. You can verify whether your container is running using the docker ps command.
docker@boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b6fb75adad6 dropwizard/dropwizard-example:latest “/bin/sh -c ‘java -j 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp high_turing
- Test the application
Now the application is ready for use. You can access the application using your Docker host ip address and the forward port 8080. For example, use the Google Advanced Rest Client App to register “John Doe”.