codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

44 / 62 topics
44Docker Basics for Spring Boot Applications45Creating a Dockerfile for Spring Boot46Using Docker Compose for Multi-Container Applications
Tutorials/Spring Boot/Docker Basics for Spring Boot Applications
🍃Spring Boot

Docker Basics for Spring Boot Applications

Updated 2026-04-20
3 min read

Docker Basics for Spring Boot Applications

Introduction

Docker is a platform that allows developers to package their applications and dependencies into containers, ensuring consistency across different environments. This tutorial will guide you through the basics of using Docker with Spring Boot applications, including setting up your environment, creating Dockerfiles, building images, and running containers.

Prerequisites

Before you begin, ensure you have the following installed:

  • Docker: Download and install Docker Desktop from docker.com.
  • Spring Boot CLI: Install the Spring Boot CLI to quickly create new projects.
  • Java Development Kit (JDK): Ensure JDK 8 or higher is installed.

Setting Up Your Environment

  1. Create a New Spring Boot Project: Use the Spring Initializr to generate a new project. You can do this online at start.spring.io or use the CLI:

    spring init --dependencies=web my-spring-boot-app
    cd my-spring-boot-app
    
  2. Add a Simple Controller: Open src/main/java/com/example/demo/DemoApplication.java and add a simple REST controller:

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    @RestController
    class HelloController {
    
        @GetMapping("/hello")
        public String sayHello() {
            return "Hello, Docker!";
        }
    }
    
  3. Run the Application Locally: Ensure your application runs locally by executing:

    ./mvnw spring-boot:run
    

    Visit http://localhost:8080/hello to see the output "Hello, Docker!".

Creating a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here’s how you can create one for your Spring Boot application:

  1. Create a Dockerfile in the root of your project: Add the following content to your Dockerfile:

    # Use an official OpenJDK runtime as a parent image
    FROM openjdk:17-jdk-slim
    
    # Set the working directory in the container
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    COPY target/*.jar app.jar
    
    # Make port 8080 available to the world outside this container
    EXPOSE 8080
    
    # Define environment variable
    ENV NAME World
    
    # Run app.jar when the container launches
    CMD ["java", "-jar", "app.jar"]
    
  2. Build Your Application: Before building the Docker image, ensure your application is built:

    ./mvnw clean package
    
  3. Build the Docker Image: Run the following command to build your Docker image:

    docker build -t my-spring-boot-app .
    

Running Your Docker Container

Once you have built your Docker image, you can run it as a container:

  1. Run the Container: Use the following command to start your Spring Boot application in a Docker container:

    docker run -p 8080:8080 my-spring-boot-app
    
  2. Access Your Application: Open a web browser and navigate to http://localhost:8080/hello. You should see "Hello, Docker!" displayed.

Best Practices

  • Use Multi-Stage Builds: For better performance and security, use multi-stage builds to separate build-time dependencies from runtime dependencies.

    # Stage 1 - Build the application
    FROM maven:3.8-jdk-17 AS builder
    WORKDIR /app
    COPY pom.xml .
    COPY src ./src
    RUN mvn package
    
    # Stage 2 - Create the final image
    FROM openjdk:17-jdk-slim
    WORKDIR /app
    COPY --from=builder /app/target/*.jar app.jar
    EXPOSE 8080
    CMD ["java", "-jar", "app.jar"]
    
  • Optimize Image Size: Use lightweight base images and clean up unnecessary files to reduce the image size.

  • Environment Variables: Use environment variables for configuration settings to make your application more flexible.

  • Security: Regularly update your Docker images and follow security best practices to protect your applications.

Conclusion

This tutorial has covered the basics of using Docker with Spring Boot applications. You learned how to create a Dockerfile, build a Docker image, and run a containerized Spring Boot application. By following these steps and best practices, you can ensure that your Spring Boot applications are consistent and easily deployable across different environments.

Feel free to explore more advanced topics such as Docker Compose for multi-container setups, Kubernetes for orchestration, or CI/CD pipelines with Docker integration.


PreviousPerformance Testing with JMeterNext Creating a Dockerfile for Spring Boot

Recommended Gear

Performance Testing with JMeterCreating a Dockerfile for Spring Boot