Simple Set-up guide with Docker & Spring [Windows]

Darryl Leong
4 min readAug 23, 2020

Hi Everyone, if you’re new to Docker and possibly Spring, this is a quick guide on how to get a sample project with Springboot running in a Docker container in just 6 steps (well 7 if you count step 0)!

Step 0: Resources you’ll need

1. Docker (obvious enough).

You can find the download link here — https://docs.docker.com/docker-for-windows/install-windows-home/

Running ‘docker version’ in your cmd prompt should print out the relevant version information

2. IDE — use whatever you like, I’ll be using IntelliJ for this guide

Step 1: Initialising a Sample Spring Project.

Thankfully, Spring has provided us with a simple template generator which bootstraps a bunch of dependencies to make it easy for us to get started.

You can find the Spring initializer here: https://start.spring.io/.

As you can see, I’ll be using version 11 of Java for this demo, but feel free to use whichever suits your requirements. One thing to take note though — add the ‘Spring Web’ dependency as you can see on the right side of the image before hitting GENERATE. Extract your project and open up your project.

Alright! Now that you got your project downloaded, it’s time to move on! (That wasn’t so hard was it?)

Step 2: Adding a Controller to your application

Your Project structure should be as follows after creating the Controller class:

Fill Controller.java with the following code:

@RestController
public class Controller {
@RequestMapping("/greetings")
@ResponseBody
public String greetings(){
return "Is this a JoJo reference?";
}
}

Time to test it out! There are several ways to test whether your application is working as expected. If you’re using IntelliJ simply open up the main class → right click →‘run XXX…main()’. If you’ve set everything up right so far, you should get the following after navigating to http://localhost:8080/greetings:

Step 3: Creating the Dockerfile

Awesome, now its time to move onto the docker side of things. The Dockerfile provides a set of instructions to be executed when building the docker image. Create the Dockerfile in the root of your project folder and add the following:

FROM openjdk:11

# Replace <PROJECT_NAME> with the name of your project - in my case, it would be: spring-docker-example

COPY ./target/<PROJECT_NAME>-0.0.1-SNAPSHOT.jar /

WORKDIR /

RUN sh -c 'touch <PROJECT_NAME>-0.0.1-SNAPSHOT.jar'

ENTRYPOINT ["java","-jar","<PROJECT_NAME>-0.0.1-SNAPSHOT.jar"]

Step 4: Generating a .jar file

Great, now that you have made sure your Spring application is able to run, it’s time to package it up into a .jar file before building the docker image with it.

Run mvn clean install in your root folder. A target folder should be generated in your root folder with a XXX-0.0.1-SNAPSHOT.jar file inside.

Step 5: Building the image

Almost there! Just a couple more commands and you’re set.

Run ‘docker build -t <APP_NAME> .’ Replace <APP_NAME> with whatever name you want!

Once the image has been successfully built, running ‘docker images’ should display the newly created image:

Step 6: Running your application (FINALLY)

The moment we’ve all been waiting for… finally, run: ‘docker run -p 8080:8080 <APP_NAME>’.

The -p flag specifies the port you want to map from your host to the port inside your container. You can read more from the official docker documentation here:

https://docs.docker.com/config/containers/container-networking/

Congratulations! Your application should now be up and running in a Docker container! Go to 192.168.99.100:8080/greetings to test it out! The IP address of your docker machine might differ from mine, but you can simply find this out by running ‘docker-machine ip’.

I hope you enjoyed this simple tutorial on getting started with running your first Springboot application with Docker! If you have any questions, please leave them in a comment below.

GitHub repository can be found here: https://github.com/darryleong95/spring-docker-example

--

--