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:
- 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.
- 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.
- 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:
- 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.
- 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.
- 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:
FROMcommand 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.
WORKDIRcommand sets the working directory inside the Docker image to
/app. This is where we’ll be copying our Spring Boot application JAR file.
COPYcommand copies the
mobile-app-ws-0.0.1-SNAPSHOT.jarfile from our local machine to the
/appdirectory in the Docker image.
CMDcommand 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.jarcommand 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 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.
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.
. 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:
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.
Frequently Asked Questions
- Why should I use a Dockerfile?
Using a Dockerfile to build your Docker images allows you to automate the image-building process and make it repeatable. You can version control your Dockerfile, and use it to ensure that your Docker image is built the same way every time.
- Can I use multiple
FROMcommands in my Dockerfile?
No, you can only have one
FROMcommand in your Dockerfile. However, you can use multiple Dockerfiles to build different images and then use Docker Compose to run them together.
- How can I debug issues with my Dockerfile?
You can use the
docker buildcommand with the
--progress plainoption to see the output of each step of your Dockerfile. You can also use the
docker runcommand with the
-itoption to run a shell inside your Docker container and debug issues from there.
- Can I include multiple commands in a single
Yes, you can use
&&to chain multiple commands together in a single
RUNcommand. This can help reduce the size of your Docker image.
- Can I use variables in my Dockerfile?
Yes, you can use variables in your Dockerfile by using the
$variablesyntax. However, these variables must be defined either in the same Dockerfile or in a file included in the Dockerfile using the
- How can I optimize my Dockerfile for size?
You can optimize your Dockerfile for size by using a smaller base image, minimizing the number of layers in your Dockerfile, and removing unnecessary files and dependencies.