Table of Contents
Step-By-Step Guide To Create A Spring Boot MVC Microservice With Docker
SpringBoot is a popular spring framework capable of running as a standalone executable. It fits well in microservices architecture where each service is supposed to be running independently. In Docker, each Microservice(Spring Boot rest service) can be installed in a separate container and accessed independently.
In this tutorial, we will
- Create Spring Boot Project from Maven archetype
- Run and Test standalone application
- Create a Docker image from Dockerfile
- Run Docker container
- Test the application
Create Spring Boot Project from Maven archetype
- Create a new Maven Project by going to File -> New -> Others (Maven Project)
- Click Next, as shown below
- Select the archetype org.springframework.boot (Group ID) and spring-boot-sample-data-rest-archetype(artifact-id). You need to add remote archetype catalogue to eclipse.
- Enter the Group ID and Artifact id
- If everything went fine, you should end up with this.
Run and Test standalone application
- Open Class SampleDataRestApplication and Run as Java Application, wait for couple of seconds (5-10) and in your console tab, you should see some output coming in
- Check the Port number at the end of the console, usually, it is 8080. If you see the above output Congratulations you have Spring Boot MVC running on your system. Open localhost:8080 in your browser and you should see some JSON output.
- Run your project as Maven install so you do get a jar file in target folder of your project
- In your target folder, you should see a jar file called microservice-0.0.1-SNAPSHOT.jar
Create a Docker image from Dockerfile
- Now let us add a docker file in Project ( This needs to be placed under root directory). The File name should be Dockerfile without any extensions.
1 2 3 4 5 6 7 8 | FROM java:8 VOLUME /tmp ADD target/microservice-0.0.1-SNAPSHOT.jar user.jar RUN bash -c 'touch /user.jar' ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /user.jar"] |
- In above file what we are saying is that
- use Java 8 version
- write into /tmp directory of docker container
- add the microservice jar and rename it as user.jar
- then run touch command to update the timestamp of the jar
- the last line is for Tomcat optimisation.
- Now either we can add a maven plugin to build docker image or build it using console commands. For this tutorial to keep things simple let us install it through docker commands.
- Open Terminal/command Prompt and then navigate to the project root directory and build the docker image ( You need to have docker installed on your system)
1 2 3 | docker build -t microservice:latest . |
(don’t miss “.” , as it tells docker file is in current directory)
If the above command is successful then use docker images to see if the image has been created
1 2 3 4 5 | REPOSITORY TAG IMAGE ID CREATED SIZE microservice latest 324be107d90b 5 seconds ago 691 MB |
Run Docker container
- Now we will run this docker image
1 2 3 | docker run -d -v /tmp:/tmp -p 9090:8080 --name mymicroservice microservice |
- We are asking docker here to run our application on localhost port 9090, and since we know that our Tomcat started on port 8080 so we are pointing to that port within docker. The docker assigns some random name each time docker is restarted so it’s good practice to name the docker image.
- If above command was successful.Use <code>docker ps -a </code>, it should show the running images in terminal/command prompt.
1 2 3 4 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0444703d0da microservice "sh -c 'java $JAVA..." 5 seconds ago Up 3 seconds 0.0.0.0:9090->8080/tcp mymicroservice |
Test the application
- Now if you open your browser and type localhost:9090, you should get the same JSON which you got earlier when you were running the application as Java application.
The source code for above project can be found
Why not connect this application to MongoDB
Follow these 2 videos
Believe me. It works just awesomely.
Don’t know how many tutorials, I went through. But none of those completed either due to missing instructions or due to docker issues on Windows Environment.
Many Thanks …!