The steps will be as follows:-
- Setting up a spring-boot app
- Creating a dockerfile
- Building project jar
- Building docker image by using dockerfile
- Running image
Let’s examine the above steps in detail:
- Setting up spring-boot app: So first of all, use spring initializer to have a very basic spring-boot greetings project with the help of a web dependency.
The project includes a simple rest controller with a simple greeting message.
To run this app use command:
- Creating A Dockerfile: A dockerfile is a text document which contains commands read by docker and is executed in order to build a container image.
- FROM: The keyword FROM tells Docker to use a given base image as a build base. In this case Java8 is used as base image with jdk-alpine as tag. A tag can be thought as a version.
- COPY: copying .jar file to the build image inside /usr/app.
- WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow in the Dockerfile. Here the workdir is switched to /usr/app
- RUN: The RUN instruction runs any command mentioned.
- ENTRYPOINT: Tells Docker how to run application. Making array to run spring-boot app as java -jar .jar.
- Building Project Jar: Now run mvn install to build a .jar file in target directory.
- Building Docker Image: Excecute command docker build -t spring-boot-docker-demo .
- Run the image build: Execute command docker run spring-boot-docker-demo
Github repository: Spring Boot Docker Demo
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Spring Boot | How to access database using Spring Data JPA
- How to create a REST API using Java Spring Boot
- How to encrypt passwords in a Spring Boot project using Jasypt
- Spring Boot | How to consume JSON messages using Apache Kafka
- Spring Boot | How to consume string messages using Apache Kafka
- How to create a basic application in Java Spring Boot
- Introduction to Spring Boot
- Spring Boot | How to publish JSON messages on Apache Kafka
- Spring Boot | How to publish String messages on Apache Kafka
- Creating a Dual Boot System with Linux and Windows
- Spring MVC using Java based configuration
- Android | Creating a Calendar View app
- Android | Creating multiple Screen app
- Singleton and Prototype Bean Scopes in Java Spring
- Bean life cycle in Java Spring
- Spring vs. Struts in Java
- Android Boot Process
- Introduction to Spring Framework
- Aspect Oriented Programming and AOP in Spring Framework
- Spring Dependency Injection with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.