How to Dockerize Java Spring Boot Api & Mongo DB
What is Docker?
Docker is a tool which provides a way to package applications with all the necessary configurations and dependencies in a loosely isolated environment called container.
What is Container?
Container is a virtualized environment where the applications are isolated from the underlying systems in runtime. A container is a collection of layered images of both base and application.
Why to use docker?
In early days web applications were deployed using Virtual Machines with some deployment scripts using additional tools. Where Virtual Machines would create completely isolated environments of your main operating system and has a dependency of the guest operating system. The idea here is to spin up a Virtual Machine and set up your web application, install database server, Web Server etc., Every time when you want to deploy your web application, you have to follow all the steps mentioned above as this would be very time consuming and a tedious process. With docker you do not have to install any guest operating system as the docker containers share the host operating system.
Advantages of docker?
One of the biggest advantages of docker is that it is very light weight when compared to Virtual Machines and it saves a lot of money and time as it efficiently uses system resources and helps isolating the applications.
Are there any disadvantages?
The core concept of docker is virtualization such that the infrastructure would still be subjected to limitations when interfacing between containers and the host system. Other limitations include complication of Persistent Data Storage, GUI applications etc.,
Create Spring Boot Api
Start with Spring Initializer as it would generate the boiler plate code easily. The example project that we are using here is a Movie Info Management API.
Dependencies
- Spring Boot Starter Web
- Spring Boot Starter Data MongoDB
- Lombok
Project: https://github.com/karthiksai231/docker-exampleClone: git clone https://github.com/karthiksai231/docker-example.git
Run
To run the application your system should have MAVEN installed. Run the following commands...
// Go to Project Root Directory
cd <PathToRootDirectory>// Install
mvn clean install// Run
java -jar target/<name_of_your_project_jar> // If you have cloned the Project
java -jar target/docker-0.0.1-SNAPSHOT.jar
Once the Project starts running successfully you can access the api using…
http://localhost:8080/api/v1/movie
Create a movie entry using the API Url with Movie Name and Category in the request body.
// Request Body{
"movieName" : "IronMan",
"category" : "Action"
}// ResponseCode: 200 (OK){
"movieName": "IronMan",
"category": "Action"
}
Packaging
The target directory should contain the project JAR file required to build the docker image. If you make any additional changes after running mvn clean install
command make sure to re-run the command to detect the new changes.
mvn clean install
Dockerfile
Building Docker Image
Go to the directory where the Dockerfile exists and run the following command in Command Prompt/Terminal to create the docker image for your application.
docker image build -t <name_of_the__app_image> .
Running Docker Container
You need to run the dependent database container before running your application container.
To get started follow the below steps…
Mongo DB
docker run -d -p 27017:27017 --name <mongo_container_name> --net backend mongo:latest
Movie Application
docker run --rm -p 8080:8080 --name <name_of_app_container> --net backend --link <mongo_container_name>:mongo <name_of_the__app_image>
Bonus
Publish App to Docker Hub
Run the following commands to publish your app image. You need to have a docker account to publish the image.
In your Command prompt/Terminal run the following commands…
// Login to your Docker account
docker login
The docker image should be tagged before publishing the image. In order to tag the image run the following command.
docker image tag <original_image_name> <your_user_name>/<repo_name>:latest
Now, you can publish your image…
docker image push <your_user_name>/<repo_name>:latest
Docker Compose File
Run the following command docker-compose up
to run the Movie Application and Mongo DB containers without running any other docker commands manually.
Note: You need to have the application image already created before running the Docker Compose file.