Deploying Micronaut to Docker: A Complete Maven-Based Guide

If you’re building a Micronaut application, the next step is getting it deployed. One of the easiest and most powerful ways to deploy your application is by using Docker. In this guide, we will walk you through the steps of Deploying Micronaut to Docker with Mavenβ€”no Gradle involved.

You’ll build a Micronaut app, write a simple Dockerfile, and run the app inside a container. All code examples use the package com.kscodes.micronaut.docker for consistency.

Deploying Micronaut to Docker

🧰 What You Need

  • Java 17+
  • Docker installed
  • Maven 3.6+
  • Micronaut CLI (optional but helpful)

πŸ— Step 1: Create the Micronaut Project

Create a new project with Micronaut and Maven:


mn create-app com.kscodes.micronaut.docker.helloservice \
  --build maven \
  --jdk 17

Navigate into the project folder:


cd helloservice

πŸ“ Project Structure

Micronaut will generate:


.
β”œβ”€β”€ pom.xml
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ main
β”‚   β”‚   └── java
β”‚   β”‚       └── com
β”‚   β”‚           └── kscodes
β”‚   β”‚               └── micronaut
β”‚   β”‚                   └── docker
β”‚   β”‚                       └── HelloController.java
β”‚   └── resources
β”‚       └── application.yml

πŸ‘¨β€πŸ’» Step 2: Create a Simple REST Endpoint

File: com.kscodes.micronaut.docker.HelloController.java


package com.kscodes.micronaut.docker;

import io.micronaut.http.annotation.*;

@Controller("/hello")
public class HelloController {

    @Get
    public String greet() {
        return "Hello from Micronaut + Docker!";
    }
}

βš™οΈ Step 3: Build the JAR Using Maven

Run this command to generate the JAR file:


./mvnw clean package

This will create a JAR file in target/ like:


target/helloservice-0.1.jar

🐳 Step 4: Create a Dockerfile

In the root of your project, add a file named Dockerfile:


FROM eclipse-temurin:17-jdk-jammy

WORKDIR /app

COPY target/helloservice-*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

βœ… This Dockerfile uses a lightweight JDK 17 base image and runs your Micronaut JAR.

πŸ— Step 5: Build Docker Image

Now, build your Docker image with a custom name:


docker build -t micronaut-docker-app .

▢️ Step 6: Run the Micronaut App in Docker

Start a container from the image:


docker run -d -p 8080:8080 micronaut-docker-app

Now access your endpoint:


curl http://localhost:8080/hello

Output:


Hello from Micronaut + Docker!

πŸ§ͺ Step 7: Dockerize with Build Plugin (Optional)

If you want to automate Docker builds from Maven, add this plugin in pom.xml:



  com.spotify
  docker-maven-plugin
  1.2.2
  
    micronaut-docker-app
    ${project.basedir}
    
      
        /
        ${project.build.directory}
        ${project.build.finalName}.jar
      
    
  


Then run:


./mvnw docker:build

πŸ” Bonus: Production Docker Best Practices

  • Use jlink to reduce image size even more
  • Add health checks in Dockerfile
  • Use micronaut-runtime profile to optimize for containers
  • Don’t include .mvn, .idea, or build directories in image

πŸ“š External Resources

βœ… Conclusion

Deploying Micronaut to Docker is fast, clean, and lightweightβ€”just like Micronaut itself. With only a few lines of code and a simple Dockerfile, you can run your application anywhere: local, cloud, Kubernetes, or CI/CD pipelines.

Using Maven and the com.kscodes.micronaut.docker package, you now have a production-friendly and SEO-friendly Micronaut project that runs beautifully inside containers.