Skip to main content

How to run Spring Boot application inside docker container

Step by Step procedure in creating and running a simple Spring Boot application inside docker container

This tutorial will show you on how to run simple Spring Boot application inside docker container.
If you have not installed docker already, please install it first. I'm using Linux Mint 18.2. You can follow these steps on How To install Docker in Linux Mint 18.2
After you've successfully installed docker in your machine, let's create a docker image that will run inside the container.
  • Create a simple Spring Boot project from here. Select Web as a dependency.
  • Import the project into you IDE, I'm using Spring Tool Suite.
How to run Spring Boot application inside docker container
  • Create a controller and name it HelloController. You can name your controller anything you like.
  • Create a method that will map the request and return a response.
How to run Spring Boot application inside docker container
  • Create Dockerfile at the root of the project. The name is important here, only the letter D is capital
  • Make the following entries inside the Dockerfile
#Enter all the required docker image. In this example, we'll use openjdk
FROM <docker image name:port number >
#add the jar and define the location of the created jar
ADD <jar source> <target source>
#Expose the application port
EXPOSE <port number>
#command to execute the application
ENTRYPOINT ["java", "-jar", "<jar name.>"]

#Enter all the required tools like java, mysql, redis etc inside Dockerfile
FROM openjdk:8
ADD target/docker-boot-demo.jar docker-boot-demo.jar
EXPOSE 9000
ENTRYPOINT ["java", "-jar", "docker-boot-demo.jar"]
How to run Spring Boot application inside docker container
  • Change the port number in application.properties file
server.port=9000
  • Define simple application name inside pom.xml using <finalName> tag
<build>
    <plugins>
 <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
    </plugins>
    <finalName>docker-boot-demo</finalName>
</build>

Build the docker image
#In terminal, navigate to the project folder and build the image using the following commands
docker build -f Dockerfile -t <docker image name> <location>
docker build -f Dockerfile -t docker-boot-demo .
Check if docker image is created by entering the following command
docker images
How to run Spring Boot application inside docker container
Run the application
docker run -p <port defined in application>:<port exposed from machine> <image name>
docker run -p 8081:9000 docker-boot-demo
How to run Spring Boot application inside docker container
Open the web browser and check if everything is working fine.
How to run Spring Boot application inside docker container

Comments

Popular posts from this blog

Live video streaming with ffmpeg and ffserver

Live video and audio streaming with ffmpeg and ffserver This is a guide on how to stream video using ffserver and ffmpeg in ubnutu. The ffmpeg ffserver version used is 3.3-1~16.04.york1. You can download ffserver from ffmpeg.org . I'm using linux mint 18. So, I've downloaded ffmpeg from the repository. You can follow the link below to install the latest version of ffmpeg. Install ffmpeg in linux mint 18 using command line . First, we need to setup our ffserver.conf to stream the audio and video properly. ffserver.conf file is usually located in /etc/ffserver.conf. You can either edit the ffserver.conf or create your own .conf file. I'll be creating my own .conf file named livestream.conf In console, type sudo nano /etc/livestream.conf Blank .ffm file will be created. Now, enter the required properties for streaming live video using ffserver #Default port HTTPPort 8090 HTTPBindAddress 0.0.0.0 MaxHTTPConnections 2000 MaxClients 1000 MaxBandwidth 100000 Custo

Handling unexpected runtime exception inside Thread. thread.setUncaughtExceptionHandler

Create and set UncaughtExceptionHandler before starting new thread to catch unexpected runtime exception in Multithreading application. package demo; public class ExceptionHandler {     public static void main(String[] args) {         Thread thread = new Thread(new Runnable() {             @Override             public void run() {                 throw new RuntimeException("Critical Error");             }         });         System.out.println("Starting new thread " + Thread.currentThread().getName());         //Throw this exception if any exception has occurred inside running thread but was not caught anywhere else         //This should be set before starting the new thread         thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {             @Override             public void uncaughtException(Thread thread, Throwable throwable) {                 System.out.println("Error occurred on thread: " + thread.getName() + ". Reason :