Docker and Spring Boot…

Seyed Sahil
4 min readJun 22, 2020

--

Hello friends, in today’s session I will show you how to create a new spring boot web application and deploy the same in docker. Also, we will see how we can connect to the web application and access the web pages.

Downloading the Source Code…

To make things simpler I have created a simple spring boot web application and you can find it here.

Starting the Web Application…

  1. Download Spring Tools 4 for Eclipse from here.
  2. Import the existing Maven project.
  3. Run as Spring Boot Application.

Accessing the Web Page…

If you see the below-given messages in the IDE console, it means your application is started and is working fine.

[2m2020-06-22 21:51:46.054[0;39m [32m INFO[0;39m [35m10464[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path '' [2m2020-06-22 21:51:46.063[0;39m [32m INFO[0;39m [35m10464[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.d.demo.DockerHelloWorldApplication [0;39m [2m:[0;39m Started DockerHelloWorldApplication in 1.749 seconds (JVM running for 2.503)

Now open your favorite web browser and type the below-given URL, you will see a text message “Hello World!”.

http://localhost:8080/hello

Output…

Creating a JAR file…

Create a new Maven build configuration with the goal “clean install” and run it. This will generate an independent jar file that is required for building a Docker image.

Installing Docker…

Install Docker by downloading Docker Desktop for Windows from here.

Post-installation open command prompt and type in the below-given command to make sure that Docker is up and running.

docker version

Creating Dockerfile…

Dockerfile is used to build or create a Docker image. It contains a set of commands which when executed creates a docker image.

FROM java:8
ADD target/docker-hello-world-0.0.1-SNAPSHOT.jar docker-hello-world-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "docker-hello-world-0.0.1-SNAPSHOT.jar"]

For us only these three commands are required for building the image. To run this web application we need a Java layer. Then we copy the web application JAR to Docker image and then set the entry point.

Building Docker Image…

The Dockerfile is already present in the project folder. Open the command prompt and switch to the project directory and then execute the below-given command.

docker build -f .\Dockerfile -t hello_world_app .

This will take some time as it requires downloading the Java Docker image from the Docker repository. Once that is done a new image will be created with the given name.

To see the created image execute the following command.

docker images

This will show you the available / created images.

Running the Docker Image…

Now that we have created our Docker image, its time for us to run it by executing the below-given command.

docker run -p 12345:8080 hello_world_app

Note: Port mapping allows mapping between the host ports with the container’s inner port.

Accessing the Web Page…

If you see the below-given messages in the command prompt, it means your application is started and is working fine.

2020-06-22 17:15:13.144  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-22 17:15:13.176 INFO 1 --- [ main] o.s.d.demo.DockerHelloWorldApplication : Started DockerHelloWorldApplication in 3.568 seconds (JVM running for 4.17)

Now open your favorite web browser and type the below-given URL, you will see a text message “Hello World!”.

http://localhost:12345/hello

Thank You

Seyed Sahil

Originally published at http://sydlabz.wordpress.com on June 22, 2020.

--

--

Seyed Sahil
Seyed Sahil

Written by Seyed Sahil

Coding Since 2011, Software Engineer, Game Developer, Artist, Photographer. Passionate about Security and Web Technologies. Favourites — C, Java, Javascript.

No responses yet