Dockerfile Spring Boot

Learn via video courses
Topics Covered

Overview

Containers have grown in popularity in the recent few years. Every organization wants to containerize the application and leverage its benefits. The container is a lightweight and portable package that contains all the dependencies and code needed to run a specific application. Containers are built on top of Docker, an open-source engine that allows users to create, manage, and deploy containers on a variety of platforms.

Pre-requisites

Readers are expected to have

  • Knowledge of virtualization
  • Docker should be installed on your machine.

Introduction

You are not a good techie if you haven't spoken or heard the phrase. It works on my machine!!.

It Works on my Machine

We will ship your machine to the customer.

For an application to run, it needs.

  • Operating system
  • Runtime.
  • Libraries
  • System Tools
  • Application Binaries

Docker packages software into standardized units called containers with everything the binaries need to run. Containers can be placed and run in any env and operating system where docker is installed.

It works on all machines now.

Docker and Containers

What is a Container?

Containers are executable units of software in which application code is packaged, along with its libraries and dependencies, in common ways so that it can be run anywhere.

Containers implement software-level virtualization in which features of OS are leveraged to isolate the process and control the number of resources to use from the host operating system.

Container vs OS Virtulization

Dockerfile

Dockerfile is a simple text file containing instructions for how to build the Docker container image. It’s essentially a sequence of command-line instructions that Docker Engine will run to build the image.

Sample dockerfile

Let's understand the important keywords.

  • FROM ubuntu - It tells docker, which base image we want to use for our image. In our example, we are creating an image from the Ubuntu image.

  • MAINTAINER - The person or organization maintaining this image.

  • RUN - Run instructions against the image.

Dockerize a Spring Boot Application

Simple Spring Boot Application

Let's dockerize one existing application on github.

Create Dockerfile

Create a DockerFile in the parent folder with the below content.

Create Dockerfile

DockerFile contains the following information.

  1. From - We want to build our image from openjdk17 as runtime.
  2. Maintainer - Maintainer of the image.
  3. Copy- This is important step. This command copies generated bootable jar from the maven target folder into the docker container. This is the binary we want to execute.
  4. Entrypoint. This will be executable to start when the container is starting.

Build Docker Image

We now have two artifacts:

  1. Bootable jar
  2. DockerFile

Let's use these two artifacts to assemble the image. To create an image from our Dockerfile, the docker build command is used.

The above command will build a docker image with the name springbootcachingsample and tag 0.0.1, which can be verified from the docker images command. It lists all the images.

Run the Docker Container

Let's run the application using the docker run command.

The above command should run the application, which can be verified from the docker ps command

Verify

Let's verify by hitting the /fact/ API endpoint.

We see this error because the application is running inside a docker container on port 8080, not inside our host operating system. We must map the port from the container to the host operating system.

Let's rerun the app with the -p8080:8080 argument, which maps the port to the host OS.

This time operation is successful.

The source code for the application is available at github under the branch dockerized_application.

Conclusion

In this article, we have covered the following:

  • Containers are built upon OS-level virtualization
  • Docker is an open platform for developing, shipping and running applications as containers.
  • A docker image contains everything an application need to run.
  • To create a docker image developer need to create dockerFile which contains instruction to build the image.
  • We have created dockerFile for a spring boot application to create a container image.