Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Browser testing is essential if you have a website or web applications that users interact with. Manual testing can be very helpful to an extent, but given the multiple browsers available, not to mention versions and operating system, testing everything manually becomes time-consuming and repetitive.

To help automate this process, Selenium is a popular choice for developers, as an open-source tool with a large and active community. What's more, we can further scale our automation testing by running on theLambdaTest cloud-based testing platform.

Read more through our step-by-step tutorial on how to set up Selenium tests with Java and run them on LambdaTest:

>> Automated Browser Testing With Selenium

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

1. Introduction

In this tutorial, we’ll explore how to build and run a Micronaut application in a Docker container. We’ll discuss everything from setting up a Micronaut project to creating and running a Dockerized application.

Micronaut is a modern, JVM-based framework designed for building modular, easily testable microservices and serverless applications with minimal memory footprint. Docker simplifies the deployment process by allowing developers to package applications into containers that combine the necessary code, libraries, and dependencies to run uniformly across different environments.

When used together, Micronaut and Docker create a powerful duo for efficiently developing and deploying scalable and resilient applications.

2. Prerequisites

For an effective guide on building and running a Micronaut application in a Docker container, here’s what we’ll need upfront:

  • Java Development Kit (JDK): JDK 21 is recommended for better performance and access to new language features. Micronaut does support JDK 17 as well, but we’ll need to ensure we change the version flags accordingly. In this tutorial, we’ll use JDK 21, as it’s Micronaut’s default version.
  • Micronaut: Micronaut installation is optional since we can create a project on the web too. In this tutorial, we’ll learn how to do it using both the CLI and Micronaut’s website.
  • Gradle or Maven: We’ll automate Docker image creation using Gradle and Maven, leveraging Micronaut’s built-in wrappers without additional installation.
  • Docker: Docker should be installed and configured on our machine to facilitate containerization.

We’ll focus on using these tools to build our Docker container holding a Micronaut app, rather than the initial setup of these environments. We should ensure that we have them prepared to follow along seamlessly.

3. Setting up the Micronaut Application

To start setting up our Micronaut application we’ll first create a new Micronaut project. We can do this via Micronaut’s CLI, the Micronaut Launch website, or a compatible IDE such as IntelliJ IDEA.

3.1. Using the CLI

To install Micronaut CLI, we’ll follow Micronauts’ official documentation, which uses SDKMAN!. Once installed, we can use this command to create the project:

mn create-app com.baeldung.micronautdocker

This creates a basic Micronaut application scaffolding under the com.baeldung.micronautdocker package. By default, Micronaut sets up the project to be used with Java 21 and Gradle as the build tool.

To use Java 17 or Maven, we can include the appropriate flags:

mn create-app com.baeldung.micronautdocker --jdk 17 --build maven

We can see other flags and set-up options in Micronaut’s CLI Guide.

3.2. Using Micronaut Launch

If we want to skip Micronaut’s installation, we can create a new project using their Micronaut Launch Website. Unlike the CLI, the website allows us to download a folder with all the necessary files to get us working without needing any installation.

3.3. Checking the Environment

Once we’ve successfully created our project, we can check if everything is working by starting the application.

Here’s the command to launch the application using Gradle:

./gradlew run

If we use Maven, we can launch the application by invoking the mn:run target:

./mvnw mn:run

We can verify the application is running by navigating to http://localhost:8080 in a browser or using a tool like curl.

This step ensures everything is properly configured and our Micronaut application is set up correctly. Once we verify it, we can move forward and start Dockerizing our application.

4. Dockerizing the Application

With our Micronaut application set up and functioning correctly, the next step is to prepare it for Dockerization.

To create an auto-generated Docker image, we need to ensure that our build tool has some configurations to make it work properly. Both Gradle and Maven have built-in support for the creation of Docker images. Nevertheless, if we wanted to modify trivial Docker features, we’d need to do some setup.

In this section, we’ll change the base image, and exposed ports, add custom instructions, and change the output Docker image name.

4.1. Dockerizing With Gradle

To make modifications in the Dockerfile, we’ll need to capture Gradle tasks and add custom settings:

tasks.named<MicronautDockerfile>("dockerfile") {
    baseImage.set("eclipse-temurin:21-jre-alpine")
    exposedPorts.set(arrayListOf(9090))
    instruction("RUN echo 'Hello world!'")
}

In this code, we used baseImage.set() to change the name of the base image, exposedPorts.set() to change the ports that are going to be exposed by Docker, and instruction() to add a custom echo instruction.

If we want to change the output Docker image name, we’ll use:

tasks.named<DockerBuildImage>("dockerBuild") {
    images.set(listOf("micronaut_docker_gradle"))
}

This captures the dockerBuild task and sets the Docker image name to micronaut_docker_gradle. To learn more about modifying the Dockerfile from Gradle, we can go to the official Micronaut Gradle Plugin Docker Support Documentation.

4.2. Dockerizing Using Maven

Since the Micronaut Maven plugin doesn’t have built-in support for creating Docker images, we’ll use Jib from Google Cloud Tools. Jib is a popular plugin for building optimized Docker images directly from Maven without requiring a separate Dockerfile.

First, we’ll need to set up some variables in our properties section:

<properties>
    ...
    <docker.baseImage>eclipse-temurin:21-jre-alpine</docker.baseImage>
    <docker.exposedPort>9090</docker.exposedPort>
    <docker.imageName>micronaut_docker_maven</docker.imageName>
</properties>

Next, we configure the jib dockerBuild process. To do so, we’ll modify our plugins section in the build tag:

<build>
    <plugins>
     ...
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
                <from>
                    <image>${docker.baseImage}</image>
                </from>
                <container>
                    <ports>
                        <port>${docker.exposedPort}</port>
                    </ports>
                    <entrypoint>
                        <shell>sh</shell>
                        <option>-c</option>
                        <arg>
                            java -cp /app/resources:/app/classes:/app/libs/* ${exec.mainClass}
                        </arg>
                    </entrypoint>
                </container>
                <to>
                    <image>${docker.imageName}</image>
                </to>
            </configuration>
        </plugin>
    </plugins>
</build>

As demonstrated, jib doesn’t support custom instructions being written directly in the Dockerfile. We can reference the official Micronaut Maven Plugin Packaging Documentation.

5. Building the Docker Image

With our application packaged and ready, we’ll focus on how to build, check, and run our Docker image.

Here’s the command to build the image using Gradle:

./gradlew dockerBuild

If we use Maven, we can launch the application by invoking the jib:dockerBuild target:

./mvnw jib:dockerBuild

After running the dockerBuild command, we can confirm the image was created by listing local Docker images:

docker images

After running these commands, we should see the Docker image listed with the name micronaut_docker_gradle or micronaut_docker_maven. This confirms that our Docker image is ready to be deployed.

6. Running the Container

With the Docker image built, the next step is to run it as a container.

Here’s the command to start the container using Gradle:

docker run -p 9090:9090 micronaut_docker_gradle:latest

If we use Maven, we can start the container by running the image created for Maven:

docker run -p 9090:9090 micronaut_docker_maven:latest

This command maps port 9090 of the container to port 9090 of our host machine, allowing us to access the application locally via http://localhost:9090.

If the container starts successfully, we should see logs indicating that our application is running. To verify our container is active, we should run:

docker ps

This displays a list of running containers, including their IDs, status, and port mappings. We’ll look for any entry with the name micronaut_docker_gradle or micronaut_docker_maven to get more information.

If we need to do some debugging, we can use the command:

docker logs <container_id>

Running the container ensures our Micronaut application is now fully deployed and operational in a Dockerized environment.

7. Conclusion

In this article, we walked through the process of building and running a Micronaut application in a Docker container. We started by setting up the Micronaut project using the CLI and Micronaut Launch, then explored how to configure Gradle and Maven to automate Docker image creation. We also demonstrated how to customize the Docker image to suit specific requirements, including modifying the base image, exposed ports, and naming conventions. Finally, we built and ran the Docker container, ensuring the application was accessible and fully operational.

By combining the flexibility of Micronaut with the portability of Docker, we can streamline the development and deployment of scalable, lightweight applications. This integration not only simplifies workflows but also ensures consistency across environments, making it an invaluable approach for modern microservice development.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)