How to Dockerize Your Spring Boot Application (Step by Step)

How to Dockerize Spring Boot in 6 steps

If you are learning microservices,then you should also know about docker and container.

In this tutorial, I will teach you how to create a docker image from your Spring Boot application. Then we are going to run this image to create a container.

Note: A container is created when you run an image. That is why a container is sometimes called ‘a running image’.

 

What we would do

  1. Install Docker on Windows/Linux
  2. Create a Dockerfile
  3. Build a Docker Image
  4. Run the image (Create a container)
  5. View, Stop and Delete Containers
  6. Some Important Docker Commands
  7. What Next? Kubernetes!

 

1. Install and Start Docker on Windows/Linux

The interesting thing is that whatever, your operating system is, you can install docker!

  • If your system is Windows 10 64-bit, Pro, Enterprise or Education then download and install Docker from Docker Hub.
  • If your system is Windows 10 Home or any other, then download and install Docker Toolbox here
  • Check that visualization is enabled in your Operating System. Do this by opening Task Manager. Click on performance tab as you can see in the figure below
Visualization Enabled on Windows

Visualization Enabled on Windows 10 64-bit

In this tutorial, we use Docker Toolbox.

 

2. Create a Dockerfile

Now you need to open the Spring Boot application either in Spring Toolsuite or IntelliJ (STS is used here).

Run the application and test it to make sure it works well!

Step 1: Create a file in your spring boot project directory(in sts). Name it Dokerfile

Step 2: Copy the code below and paste in this file and save it.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

You can also get this code from here(Spring Boot with Docker)

Step 3: Start Docker by simply opening the Docker QuickStart Terminal. You’ll see it as a shortcut on your desktop.

It opens and provides a command line environment as shown below:

Docker Toolbox QuickStart Terminal

Docker Toolbox QuickStart Terminal

Notice the ip address of the default docker machine. We would use it later when we run our image. This is because the container has to run in this machine.

 

3. Build a Docker Image

The next step is to build a Docker image of your application. Follow the steps

Step 1: Right-click on your project name and choose ‘Show in Local Terminal’ > Git Bash

If you don’t see Git Bash, just install it easily and restart Spring Tool Suite.

Step 2: Run the command below to build the docker image

docker build -t thymeleaf-image .

This command creates an docker image of your application with name thymeleaf-image. The dot at the end represents the directory where the image is to be create. In this case is the current directory.

Now to you can run the command below to see the images available

docker image ls

 

4. Run the image (Create a container)

Now that you have the image built, you can run the image using the command below

docker run -p8080:8080 thymeleaf-image

Note that you need to provide exactly the same name as the image you created. Also note the port mapping of -p8080:8080, this means the we are mapping port 8080 of the container to port 8080 in the machine.

Now visit http://192.168.9.101:8080

You can see that the application runs perfectly inside a container!.

Thumbs up if you got to this point.

 

5. View, Stop and Delete Containers

If for any reason, you want to delete a container (for any reason), you can do that following two steps:

  • stop the container
  • remove the container

Use the command below to stop a container

docker stop <container-id>

 

The used the command delete a container

docker rm <container-id>

 

Note: To get the container id for your container, run the command below

docker rm <container-id>

 

6. Some Important Docker Commands

By now you have learn the commands the build an image, to run an image as well as to list containers. I’ll teach you  a few more command

To remove an image,

docker rmi <container-name>

To view all images,

docker image ls

Starting a stopped container

docker container start <container-name>

At this point, I think I should make another lesson on docker commands. So we can keep this tutorial simple.

 

7. What Next? Kubernetes!

I’m sure you’ve heard of Kubernetes.

Now what we did in this tutorial is this: we created docker image and hosted it locally. Similarly, we could also push our image to a remote location. For example, Docker Hub.

We can also host our images in Google Cloud. Kubernetes is a feature provided by Google for orchestrating containers. So we can have a repository in Google Cloud and the push our images there. Then we can run the containers from there. Read a little about Kubernetes here.

This is really fun to do! That is what we’ll cover in the next series!