deploy spring boot on docker

person shubham sharmafolder_openJAVA, Spring Bootlocal_offer, access_time November 17, 2024

Deploying a Spring Boot application on Docker involves several steps, from creating a Docker image to running the containerized application. Here’s a step-by-step guide to help you through the process.

Prerequisites

  1. Docker Installed: Ensure Docker is installed on your system. You can download it from the Docker website.
  2. Spring Boot Application: A Spring Boot application ready for deployment.
  3. Maven or Gradle: Ensure you have either Maven or Gradle configured in your Spring Boot project.

Step 1: Create a Dockerfile

A Dockerfile is a script that contains a series of instructions on how to build a Docker image for your application.

  1. Navigate to your Spring Boot project directory.

  2. Create a Dockerfile in the root directory of your project:

    • Replace target/myapp.jar with the actual path to your JAR file.
    • If you’re using a different JDK version, replace openjdk:17-jdk-alpine with your desired base image.

Step 2: Build Your Application

  1. Build the application JAR using Maven or Gradle.

    For Maven:

    For Gradle:

  2. Ensure the JAR file is generated in the target directory (for Maven) or build/libs directory (for Gradle).

Step 3: Build the Docker Image

  1. Open a terminal in your project directory where the Dockerfile is located.

  2. Build the Docker image using the following command:

    • -t my-spring-boot-app tags your Docker image with the name my-spring-boot-app.
    • The . at the end indicates that the Dockerfile is in the current directory.

Step 4: Run the Docker Container

  1. Run the Docker container with the following command:

    • -p 8080:8080 maps port 8080 on your local machine to port 8080 on the Docker container.
    • my-spring-boot-app is the name of the image you just built.
  2. Access your application by navigating to http://localhost:8080 in your web browser.

Step 5: Pushing to Docker Hub (Optional)

If you want to share your Docker image or deploy it to a cloud provider, you can push it to Docker Hub.

  1. Log in to Docker Hub:

  2. Tag your image for Docker Hub:

  3. Push the image to Docker Hub:

  4. Pull the image and run it on any machine with Docker:

Step 6: Clean Up (Optional)

To stop the running container:

To remove the container and image:


Tips and Best Practices

  • Multi-Stage Builds: For smaller image sizes, you can use multi-stage builds in your Dockerfile.

  • Environment Variables: Use Docker environment variables for configuring your application dynamically.

  • Docker Compose: Use Docker Compose to manage multi-container applications (e.g., app container + database container).

  • CI/CD Pipelines: Integrate Docker build and deploy steps into your CI/CD pipeline using tools like Jenkins, GitHub Actions, or GitLab CI.


By following these steps, you can easily deploy your Spring Boot application using Docker, making it more portable and easier to manage in different environments.

warningComments are closed.