Running a Spring Boot application in a Docker container is a great way to package and deploy your application. By packaging your application in a Docker container, you can easily distribute it to other machines and ensure that it runs in the same environment on each machine. In this blog post, I will show you how to run a Spring Boot application in a Docker container for a specific Spring Boot profile.
To learn more about Docker, please check Docker Tutorials page.
Create Spring Boot Application
First, let’s start by creating a Spring Boot application. You can use the Spring Initializr to generate a new Spring Boot project with the dependencies that you need. For this example, we will use the
Once you have generated your Spring Boot project, open it in your favorite editor and navigate to the
src/main/resources directory. In this directory, create a new file called
application-prod.properties and add the following content to it:
This file contains the configuration for the
prod profile of your Spring Boot application. You can use this file to specify any additional configuration for your
prod profile, such as the database connection details or the location of your logs.
Now that we have our Spring Boot application and the configuration for the
prod profile, we can create a Dockerfile to package our application in a Docker container. Create a new file called
Dockerfile in the root directory of your Spring Boot project and add the following content to it:
FROM openjdk:8-jdk-alpine COPY target/your-application-name.jar /app/your-application-name.jar CMD java -jar /app/your-application-name.jar --spring.profiles.active=prod
This Dockerfile uses the
openjdk:8-jdk-alpine image as the base image for the container. It then copies the
jar file containing your Spring Boot application to the
/app directory in the container and sets the
java command to run the
jar file with the
The last line in a Dockerfile, will run your Java application and will pass Spring Boot Profile name as a command-line argument.
Build Docker Image
Once you have created the Dockerfile, you can build the Docker image for your Spring Boot application by running the following command:
$ docker build -t your-application-name .
This command will build the Docker image for your Spring Boot application and tag it with the name
Run Application in Docker Container
You can then run the Docker image by using the
docker run command:
$ docker run -p 8080:8080 your-application-name
This command will run the Docker image and map the container’s
8080 port to the host’s
8080 port. You can then access your Spring Boot application by visiting
http://localhost:8080 in your web browser.
Pass Different Spring Boot Profile Name at Runtime
In conclusion, running a Spring Boot application in a Docker container is a great way to package and deploy your application. By using the
--spring.profiles.active command line argument, you can easily run your Spring Boot application with a specific Spring Boot profile. This can be useful for running different versions of your application, such as a development version and a production version.
You can pass a different Spring Boot profile name to your application in Docker container using command-line argument:
$ docker run -p 8080:8080 -e spring.profiles.active=dev your-application-name
I hope this tutorial was helpful to you! To learn more about building Spring Boot applications, check other Spring Boot tutorials.