Creating and configuring a Spring Boot application with Docker
Docker is a powerful platform for building, shipping, and running distributed applications. It allows developers to package their applications and dependencies into a single container, which can be easily deployed and run on any environment. In this blog post, we will look at how to create and configure a Spring Boot application with Docker.
First, let's create a basic Spring Boot application. You can use the Spring Initializer to quickly generate a skeleton project. Make sure to include the spring web dependency in your build file (pom.xml for Maven, build.gradle for Gradle).
Next, we need to create a Dockerfile for our application. A Dockerfile is a script that contains instructions for building a Docker image. The basic structure of a Dockerfile looks like this:
FROM openjdk:8-jdk-alpine COPY target/myapp.jar /app/ CMD ["java", "-jar", "/app/myapp.jar"]
The "FROM" instruction specifies the base image to use for the container. In this case, we are using the official openjdk:8-jdk-alpine image, which includes the OpenJDK 8 runtime and the Alpine Linux operating system.
The "COPY" instruction copies the compiled JAR file of our Spring Boot application to the "/app" directory in the container.
The "CMD" instruction specifies the command to run when the container is started. In this case, we are running the "java -jar /app/myapp.jar" command, which starts our Spring Boot application.
The final docker file
# Use an official Java runtime as the base image FROM openjdk:14-jdk-alpine # Set the working directory WORKDIR /app # Copy the executable jar file and the dependencies COPY target/my-spring-boot-app.jar /app/app.jar # Expose the port that the application runs on EXPOSE 8080 # Run the application CMD ["java", "-jar", "app.jar"]
Now that we have a Dockerfile, we can build a Docker image of our application using the "docker build" command:
docker build -t myapp .
This command will create a new image called "myapp" using the instructions in the Dockerfile in the current directory.
Once the image is built, we can run it as a container using the "docker run" command:
docker run -p 8080:8080 myapp
This command will start a new container from the "myapp" image and map port 8080 on the host to port 8080 in the container. This will make our Spring Boot application accessible via http://localhost:8080
You can also pass environment variables to the container using the -e flag, for example:
docker run -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=prod" myapp
This will set the "SPRING_PROFILES_ACTIVE" environment variable to "prod" inside the container.
Please note that the above is a basic example and in your case, the COPY command and the EXPOSE the command might be different depending on where your application jar file is located and on which port it runs.
Also, in a real-world scenario, you might want to use a multi-stage build to first build your application and then copy the built jar file to the final runtime image. This will help to keep your final images smaller and more secure.
In conclusion, Docker is a powerful tool for building and deploying Spring Boot applications. By packaging our application and its dependencies into a single container, we can easily run it in any environment and ensure consistency between development, test, and production environments. With this blog post, you have learned the basics of creating and configuring a Spring Boot application with Docker and running it as a container.