Dropwizard Docker Example
Dropwizard Docker Example
Introduction
I was looking online for a simple example/tutorial on how to dockerize my existing Dropwizard application which I have as a fat jar. Most of the tutorials have complicated examples/complex steps explained. Hence I have come up with this post to explain Docker and its usage in simple steps.
In this tutorial we will be dockerizing an existing Dropwizard fat jar, building it and running it.
Docker
Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries. Docker provides an additional layer of abstraction and automation of operating-system-level virtualization on Windows and Linux. Docker is a software containerization platform.
Advantages of using Docker
- Consistency across environments: An application and all its dependencies can be bundled into a single container. A Docker container is configured to maintain and all configurations and dependencies internally. As a result, the same container can be used from development to production ensuring consistency across environments.
- Portability: The same Docker container can be transferred to any machine that runs Docker and it runs there without any issues. It is highly portable.
- Version Control: Successive versions of a container can be tracked, differences between versions can be checked and roll-back can be done easily to previous versions.
- Isolation: Docker enables applications and resources to be isolated and segregated.
- Lightweight: Docker images are small and lightweight.
More details about Docker can be read from: Docker Website.
Requirements to Run the Dropwizard Application on Docker:
- Dropwizard Application
- Docker
I am assuming that you have a Basic Knowledge of Dropwizard and have a Basic Dropwizard Application running in your machine. If not, please check my blog post on Basic Dropwizard Application by going to the link: Dropwizard Tutorial. This application should be created on your machine before running it on Docker. It should run successfully using the command:
java -jar target\BasicDropwizard-1.0.0.jar src\main\resources\basicdropwizard.yml
Once the above setup is completed, the Dropwizard application can be run on Docker in a few simple steps.
Step 1: Create Dockerfile
Create a file named as Dockerfile to give instructions to Docker how to build the image. This file should be placed in the root of your project directory. Here is a basic Dockerfile that I have created for my Dropwizard application:
FROM openjdk:8-jdk COPY src/main/resources/basicdropwizard.yml /data/BasicDropwizard/basicdropwizard.yml COPY /target/BasicDropwizard-1.0.0.jar /data/BasicDropwizard/BasicDropwizard-1.0.0.jar WORKDIR /data/BasicDropwizard RUN java -version CMD ["java","-jar","BasicDropwizard-1.0.0.jar","basicdropwizard.yml"] EXPOSE 4000-4001Here is a brief explanation for the Dockerfile commands:
FROM openjdk:8-jdk: I am using this as the base image.
COPY: This is used to copy artifacts from my project. This is based on your project structure.
WORKDIR: Set the working directory as a best practice.
RUN: To check Java version.
CMD: The command used to run the jar.
EXPOSE: To expose ports for the Dropwizard service.
Step 2: Docker Build
To start a Docker build, from the root of your project directory, run the following command:
docker build -t Any_Tag_Name_for_your_project .Here is the command that I have used:
docker build -t dropwizard-basic .Here is the sample output for this command:
$ docker build -t dropwizard-basic . Sending build context to Docker daemon 15.47MB Step 1/7 : FROM openjdk:8-jdk ---> 74c95c985a85 Step 2/7 : COPY src/main/resources/basicdropwizard.yml /data/BasicDropwizard/basicdropwizard.yml ---> Using cache ---> f5669098ad53 Step 3/7 : COPY /target/BasicDropwizard-1.0.0.jar /data/BasicDropwizard/BasicDropwizard-1.0.0.jar ---> Using cache ---> 44d19233c211 Step 4/7 : WORKDIR /data/BasicDropwizard ---> Using cache ---> b87827ad1167 Step 5/7 : RUN java -version ---> Using cache ---> 35c802117249 Step 6/7 : CMD ["java","-jar","BasicDropwizard-1.0.0.jar","basicdropwizard.yml"] ---> Using cache ---> 7d94133fe6aa Step 7/7 : EXPOSE 4000-4001 ---> Using cache ---> 401c41ef042a Successfully built 401c41ef042a Successfully tagged dropwizard-basic:latestTo check the image created, you can use the command:
docker images
Step 3: Docker Run
Here is the command to run the container:
docker run -p 4000:4000 dropwizard-basic-p: This is used to specify the port. First port is the port to be exposed on the host machine. Second port is the the port of the Dropwizard service in the container.
To check the containers in your Docker, you can use the command:
docker ps -a
API calls in Dropwizard Application on Docker
Once the container is running, you can use the APIs as below to check if the Dropwizard application is up and running in a Docker container. My Docker uses the IP 192.168.99.100. This IP can be found from the first line in the terminal when Docker starts as per the image below:
API calls:
1. GET API for Ping:
http://192.168.99.100:4000/ping
2. POST API for Ping:
http://192.168.99.100:4000/ping
JSON Request Body:
http://192.168.99.100:4000/ping
2. POST API for Ping:
http://192.168.99.100:4000/ping
JSON Request Body:
{ "input": "ping" }
Some useful Docker Commands
To stop a container:
docker stop CONTAINER_IDTo check the containers in your Docker and to get CONTAINER ID, you can use the command:
docker ps -ae.g.
docker stop a4ee737189f6
To remove dangling images:
docker rmi $(docker images -f dangling=true -q)
To remove all exited containers:
docker rm $(docker ps -a -f status=exited -q)
Docker Hub
Docker Hub is an online repository like GitHub for Docker images. Once you create your own Docker ID, you can host your docker images in it. Docker Hub also has the official images of the most used technologies that you can pull and use. Here is the link for Docker Hub.
Commands to push your image to Docker Hub:
Login to Docker Hub using the command:
docker login --username=YOUR_USERNAME --password=YOUR_PASSWORDGet your IMAGE ID using the command docker images:
docker images REPOSITORY TAG IMAGE ID CREATED SIZE dropwizard-basic latest 401c41ef042a 2 days ago 755MBTag your IMAGE ID:
docker tag 401c41ef042a ajtechdeveloper/dropwizard-basic
Push to Docker Hub:
docker push ajtechdeveloper/dropwizard-basic
Next Steps
Docker Compose is a tool for defining and running multi-container Docker applications. To learn about Docker Compose and steps to dockerize a microservice, please read my blog post: Spring Boot MySQL Docker Compose Example.
To learn about Kubernetes, Minikube and deployment of a microservice on Minikube, please read my blog post: Spring Boot MySQL Kubernetes Minikube Example.
Conclusion, Docker Hub Link and GitHub link:
This tutorial gives a comprehensive overview on running an existing Dropwizard application on Docker. It also gives an overview of some of the most used Docker commands to get you up and running. The code for the Dropwizard application used in this post is available on GitHub. The docker image used in this post is available on Docker Hub.Learn the most popular and trending technologies like Machine Learning, Angular 5, Internet of Things (IoT), Akka HTTP, Play Framework, Dropwizard, Docker, Elastic Stack, Spring Boot and Flask in simple steps by reading my most popular blog posts at Software Developer Central.
If you like my post, please feel free to share it using the share button just below this paragraph or next to the heading of the post. You can also tweet with #SoftwareDeveloperCentral on Twitter. To get a notification on my latest posts or to keep the conversation going, you can follow me on Twitter. Please leave a note below if you have any questions or comments.
Comments
Post a Comment