Java / Gradle

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

Sergio del Amo
On this guide
In this section

Getting Started

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

What you will need

To complete this guide, you will need the following:

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.

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
Note
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.

Personal access token (classic) with Packages scopes

You need an access token to push to 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

Registry Credentials Configuration

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

The following code sample 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"))
    }
}

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}"))
}

Docker Push

You can push to GitHub Container Registry via the dockerPush Gradle task.

./gradlew dockerPush

License

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