Dockerfile for a Spring Boot Application: A Beginner’s Guide

If you’re a software developer or system administrator, you’ve probably heard of Docker – a tool that makes it easy to create and deploy applications in a standardized way. Docker allows you to package an application and all its dependencies into a single “container” that can be run anywhere, regardless of the underlying system.

A Dockerfile is a simple text file that describes how to build a Docker container. It specifies which base image to use, what files to include, which commands to run, and more. By creating a Dockerfile, you can automate the process of building a Docker container for your application, making it easier to deploy and maintain.

In this tutorial, we’ll show you how to create a Dockerfile for a Spring Boot application. Spring Boot is a popular Java-based framework for building web applications. By the end of this tutorial, you’ll have a good understanding of how Dockerfiles work, and you’ll be able to create your own Dockerfiles for your Spring Boot applications. We’ll assume you’re familiar with Java and Spring Boot, but you don’t need any prior experience with Docker.

To learn more about Docker, please check Docker Tutorials page.

Setting up your development environment

Before we begin creating our Dockerfile, we need to make sure that we have everything we need installed and set up on our computer. Here are the three main things you’ll need:

  1. A Spring Boot application: If you don’t already have a Spring Boot application, you can follow the this tutorial that demonstrates How to create a simple Spring Boot application. We’ll use this application in our Dockerfile example.
  2. Docker: You’ll need to have Docker installed on your machine. Docker is available for Windows, Mac, and Linux. Read the following tutorial to learn how to install Docker on your computer.
  3. A text editor or IDE: You’ll need a text editor or an integrated development environment (IDE) to edit your Dockerfile. Some popular options include Visual Studio Code, IntelliJ IDEA, and Eclipse. You can use any editor that you’re comfortable with.

Once you have all of these installed, you’re ready to get started.

Creating a Basic Dockerfile

Now that we have our development environment set up, we can begin creating our Dockerfile. A Dockerfile is a simple text file that contains a set of instructions for building a Docker image. These instructions tell Docker what base image to use, what files to include, which commands to run, and more.

Here are the three basic commands that we’ll be using in our Dockerfile:

  1. FROM: This command specifies the base image that we’ll be building our Docker image on top of. In our case, we’ll be using a base Java image.
  2. COPY: This command copies files from our local machine into the Docker image. We’ll be using this command to copy our Spring Boot application JAR file into the Docker image.
  3. RUN: This command runs a command in the Docker image. We’ll be using this command to run the Java command to start our Spring Boot application.

Here’s an example Dockerfile that you can use as a starting point:

# Use a Java base image
FROM openjdk:17-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the Spring Boot application JAR file into the Docker image
COPY target/mobile-app-ws-0.0.1-SNAPSHOT.jar /app/mobile-app-ws-0.0.1-SNAPSHOT.jar

# Run the Spring Boot application when the container starts
CMD ["java", "-jar", "mobile-app-ws-0.0.1-SNAPSHOT.jar"]

Let’s go through each line of the Dockerfile:

  • The FROM command specifies the base Java image that we’ll be using: openjdk:17-alpine. Alpine is a lightweight Linux distribution, so this image is small and doesn’t include unnecessary components.
  • The WORKDIR command sets the working directory inside the Docker image to /app. This is where we’ll be copying our Spring Boot application JAR file.
  • The COPY command copies the mobile-app-ws-0.0.1-SNAPSHOT.jar file from our local machine to the /app directory in the Docker image.
  • The CMD command specifies the command to run when the Docker container starts. In this case, we’re running the java -jar mobile-app-ws-0.0.1-SNAPSHOT.jar command to start our Spring Boot application.

That’s it! With these few lines of code, we’ve created a basic Dockerfile for our Spring Boot application. We can now use this Dockerfile to build a Docker image and run our application inside a Docker container.

Adding environment variables

In this section, I’ll explain how to use environment variables in your Dockerfile for your Spring Boot application. Environment variables are useful for storing configuration values outside of your code. This makes it easier to change those values without modifying and recompiling your application.

To set environment variables in your Dockerfile, you can use the ENV command. Here’s an example of how to use the ENV command in your Dockerfile:

# Use a Java base image
FROM openjdk:17-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the Spring Boot application JAR file into the Docker image
COPY target/mobile-app-ws-0.0.1-SNAPSHOT.jar /app/mobile-app-ws-0.0.1-SNAPSHOT.jar

# Set environment variables
ENV SERVER_PORT=8080
ENV LOGGING_LEVEL=INFO

# Run the Spring Boot application when the container starts
CMD ["java", "-jar", "mobile-app-ws-0.0.1-SNAPSHOT.jar"]

In this example, we’re setting two environment variables: SERVER_PORT and LOGGING_LEVEL. SERVER_PORT is set to 8080, which is the default port for Spring Boot applications. LOGGING_LEVEL is set to INFO, which sets the logging level for the application.

You can set any environment variable using the ENV command. Here are some other common environment variables that you might want to set for your Spring Boot application:

  • SPRING_PROFILES_ACTIVE: Specifies which Spring profile to use.
  • SPRING_DATASOURCE_URL: Specifies the URL for the database connection.
  • SPRING_DATASOURCE_USERNAME: Specifies the username for the database connection.
  • SPRING_DATASOURCE_PASSWORD: Specifies the password for the database connection.

With environment variables set, you can customize your Spring Boot application without changing the code or recompiling. This makes it easy to deploy your application to different environments with different configuration values.

In the next section, you’ll learn how to expose ports in your Dockerfile.

Exposing ports

In this section, I’ll explain how to expose ports in your Dockerfile for your Spring Boot application. When you run a Docker container, you can configure it to listen on specific ports. This allows you to access your application running inside the container from your host machine.

To expose ports in your Dockerfile, you can use the EXPOSE command. Here’s an example of how to use the EXPOSE command in your Dockerfile:

# Use a Java base image
FROM openjdk:17-alpine

# Set the working directory to /app
WORKDIR /app

# Copy the Spring Boot application JAR file into the Docker image
COPY target/mobile-app-ws-0.0.1-SNAPSHOT.jar /app/mobile-app-ws-0.0.1-SNAPSHOT.jar

# Set environment variables
ENV SERVER_PORT=8080
ENV LOGGING_LEVEL=INFO

# Expose the port that the Spring Boot application is listening on
EXPOSE 8080

# Run the Spring Boot application when the container starts
CMD ["java", "-jar", "mobile-app-ws-0.0.1-SNAPSHOT.jar"]

In this example, we’re using the EXPOSE command to expose port 8080, which is the port that our Spring Boot application is listening on.

You can expose any port using the EXPOSE command. Here are some common ports that you might want to expose for your Spring Boot application:

  • 80: HTTP traffic
  • 443: HTTPS traffic
  • 5432: PostgreSQL database
  • 3306: MySQL database

It’s important to note that exposing a port in your Dockerfile doesn’t automatically map that port to a port on your host machine. To do that, you’ll need to use the -p option when running your Docker container. For example, to map port 8080 in your container to port 8080 on your host machine, you can use the following command:

docker run -p 8080:8080 my-spring-boot-app

With ports exposed, you can now run your Spring Boot application inside a Docker container and access it from your host machine. But before you can run this application in a Docker container, you will need to build a Docker image.

Building Docker Image

First, let’s build our Spring Boot application to generate the JAR file that we’ll need for our Docker image. In your terminal, navigate to the root folder of your Spring Boot project, and run the following command:

./mvnw clean install

This command will build your project and create the JAR file in the ‘target’ folder. Take note of the JAR file name, as we’ll need it later.

Now that we have our JAR file, it’s time to build the Docker image. Make sure you’re still in the project’s root folder, where the Dockerfile is located. Run the following command:

docker build -t my-spring-boot-app .

Be sure to replace ‘my-spring-boot-app’ with a name of your choice for your Docker image. The -t flag is used to tag your image with a name, which makes it easier to reference later.

The . at the end of the command is essential – it tells Docker to look for the Dockerfile in the current directory.

Docker will now build your image using the instructions provided in the Dockerfile. You’ll see some output in the terminal while it’s working its magic. Once it’s done, you can check that your image has been created by running this command:

docker images

You should see your image listed, along with its name, tag, and some other details.

Now that you have built a Docker image from a Docker file let’s continue, and in the following tutorial, you will learn how to Run the Spring Boot application in a Docker container.