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:

Navigate into the project folder:

πŸ“ Project Structure

Micronaut will generate:

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

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

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

Run this command to generate the JAR file:

This will create a JAR file in target/ like:

🐳 Step 4: Create a Dockerfile

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

βœ… 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:

▢️ Step 6: Run the Micronaut App in Docker

Start a container from the image:

Now access your endpoint:

Output:

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

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

Then run:

πŸ” 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.