Push a Docker Image of a Micronaut application to Github Packages Container registry

Learn how to push a Docker Image of a Micronaut application to Github Packages Container registry

Authors: Sergio del Amo

Micronaut Version: 4.7.6

1. Getting Started

In this guide, we will create a Micronaut application written in Java.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=gradle --lang=java
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

Github Container Registry

In this guide, we push a docker image of a Micronaut Application to GitHub Container Registry

5. Personal access token (classic) with Packages scopes

You need an access token to push the GitHub Container Registry.

Go to Settings > Developer settings > Personal access tokens and create a personal access token with the delete:packages, write:packages scopes.

personal access token packages scopes

Micronaut Gradle Plugin

6. Registry Credentials Configuration

Modify the build file and configure the registry credentials using the Docker plugin extension.

The following code samples uses the Gradle Kotlin DSL. You will need to set the environment variable GITHUB_PERSONAL_ACCESS_TOKEN with the personal access token and replace githubOrganizationOrUsername with your GitHub personal username or organization name.

var githubOrg = "githubOrganizationOrUsername"
docker {
    registryCredentials {
        url.set("ghcr.io")
        username.set(githubOrg)
        password.set(System.getenv("GITHUB_PERSONAL_ACCESS_TOKEN"))
    }
}

7. Docker Image Configuration

Configure the dockerBuild and dockerPush tasks to set the images.

Gradle Groovy DSL

tasks.named("dockerBuild") {
    images = ["ghcr.io/${githubOrg}/${rootProject.name}"]
}

Gradle Kotlin DSL

tasks.named<DockerBuildImage>("dockerBuild") {
    images.set(listOf("ghcr.io/${githubOrg}/${rootProject.name}"))
}

8. Docker Push

You can push to Docker Hub via the dockerPush Gradle Task.

./gradlew dockerPush

9. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).