×
☰ See All Chapters

Docker Spring Boot Example

In this tutorial you will learn the steps to run a sample spring boot application in docker container. Initial steps are to create sample spring boot application war file, next steps includes creating Dockerfile, build and run docker image.

1.  Create Maven Project

Execute the below command to create the maven project:

mvn archetype:generate -DgroupId=com.java4coding -DartifactId=SpringBootDockerExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Execute the below command from inside the generated SpringBootDockerExample project to convert it to eclipse project.

mvn eclipse:eclipse

Now import this project into eclipse workspace.

docker-example-0
 

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>SpringBootDockerExample</artifactId>

        <packaging>jar</packaging>

        <version>1.0-SNAPSHOT</version>

        <name>SpringBootDockerExample</name>

 

        <parent>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-parent</artifactId>

                <version>2.3.0.RELEASE</version>

                <relativePath />

        </parent>

 

        <properties>

                <docker.image.prefix>springio</docker.image.prefix>

                <java.version>1.8</java.version>

        </properties>

 

        <build>

                <plugins>

                        <plugin>

                                <groupId>org.springframework.boot</groupId>

                                <artifactId>spring-boot-maven-plugin</artifactId>

                        </plugin>

                </plugins>

        </build>

        <dependencies>

                <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-starter-web</artifactId>

                </dependency>

        </dependencies>

 

</project>

Application.java

package com.java4coding;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

@SpringBootApplication

@RestController

public class Application {

 

        @RequestMapping("/")

        public String home() {

                return "Hello Docker World";

        }

 

        public static void main(String[] args) {

                SpringApplication.run(Application.class, args);

        }

 

}

2. Build and test the application

Build the maven project

Execute the below command

mvn clean install

docker-example-1
 

Test the application

Execute the below command

java -jar target/SpringBootDockerExample-1.0-SNAPSHOT.jar

 

docker-example-2
 

Open the URL https://localhost:8080/ from browser

docker-example-3
 

3. Create Dockerfile

Create the file with the name Dockerfile without any file extension. And add below lines of commands.

Dockerfile

docker-example-4

FROM openjdk:13-jdk-alpine

ARG JAR_FILE=target/*.jar

COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

4. Create the docker Image

Before this step, make sure spring boot application is packaged and should have created jar file inside target folder.

Now execute below command.

docker build -t springbootapp .

docker-example-5
 

4. Run the docker Image

Execute below command.

docker run -p 8080:8080 springbootapp

docker-example-6
 

Now test the application by opening URL https://localhost:8080/ from browser.

docker-example-7
 

Docker useful commands to investigate if you failed to execute the sample example

1. List all the images available in docker

docker image ls

docker-example-8
 

2. List all the running containers

docker ps

docker-example-9
 

To list container all containers running/not running add –a flag to docker ps command.

docker-example-10
 

3. Stop running containers.

docker stop <containerID>  

To stop the running containers you should provide the container ID. To know the container Id you can execute docker ps command to get the details.

docker-example-11
 
docker-example-12
 

After running container is stopped you should see spring boot has stopped.

docker-example-13
 

When you stop the container, it gets removed. If sometime container is not removed remove it by using below commad:

docker rm <containerID>  

4. Remove the image from docker

If the image created is not correct and if you wish recreate the image by removing the old image you execute below command to remove the image. Before removing image, there should be no containers running on that image. Before removing image you should stop the running containers of that image.

docker rmi springbootapp  -–force

 

You can delete the image by using image Id. Get the image Id by running docker image ls command.

docker rmi 123dad11876g3  -–force

 


All Chapters
Author