How to Implement Springboot + Docker Setup at the Local Environment

To set up Docker with a Spring Boot application on your local environment, follow these steps. We’ll cover creating a Docker image, running a container, and testing the application locally.

Step 1: Prepare Your Spring Boot Application

Ensure your Spring Boot application is configured and working locally. You’ll need:

  • A working Spring Boot project with a main class.
  • Spring Boot Maven dependencies.
  • An application with an HTTP endpoint (for example, http://localhost:8080/getData).

Step 3: Build the Spring Boot JAR

If you’re using Maven, build your Spring Boot application JAR with:

mvn clean
maven clean
mvn install
maven build
maven target folder

Step 2: Create a Dockerfile for Spring Boot Application

A Dockerfile defines how to package the application into a Docker image. Place this Dockerfile in the root of your Spring Boot project.

Sample Dockerfile for a Spring Boot Application

For a Maven-based Spring Boot application, create a Dockerfile as follows:

FROM openjdk:17-oracle
ARG JAR_FILE=target/*.jar
COPY ./target/SpringbootAPI-0.0.1.jar app.jar
ENTRYPOINT [ "java","-jar","/app.jar" ]
docker file creation

For this Dockerfile:

  • FROM openjdk:17-oracle uses a lightweight OpenJDK image.
  • COPY target/SpringBootAPI-0.0.1.jar app.jar copies the built JAR file into the container.
  • ENTRYPOINT runs the JAR file.

Note: Replace SpringBootAPI-0.0.1.jar with the actual name of your JAR file in target/.

Step 4: Build the Docker Image

Navigate to the project directory where the Dockerfile is located and build the Docker image.

docker images //To list the available docker images
docker build -t springboot-with-docker:1.0 . //To build the docker image 
docker file generation

Step 5: Run the Docker Container

Start a container from the image you just created:

  • -p 8000:8080 maps the container’s port 8000 to your local port 8080.
  • springboot-with-docker is the name of the Docker image you created.

The application should now be running inside a Docker container, accessible on http://localhost:8000.

docker run -p 8000:8080 springboot-with-docker:1.0
docker file run

Step 6: Verify the Application

Open your browser to test the Spring Boot application:

If everything is set up correctly, you should receive a response from the application running inside Docker. http://localhost:8000/getData

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *