🍪 This website uses cookies to enhance your experience.

Publish Docker images on private nexus repository using Jib maven plugin.

Introduction

Docker is now a norm in the industry to deploy server-side functionality. Especially in Java/cloud world it is widely adopted base for enabling API in the cloud/on prem. Building a docker image is one part of packaging the application/functionality but at the same time we must publish this as an artifact in the repository for both usage and compliance purpose. From the repository only production builds are fetched and deployed for usage. In this exercise we are going to learn how to publish docker images to a private nexus repository with the help of maven Jib plugin. Till recent time there was a need to install docker daemon on the box from where you want to generate docker images. Jib plugin allows you to build the docker image without installing Docker daemon. This saves us lot of effort/maintenance in the build environment where one need not require Docker daemon. Most of the enterprises publish their artifacts in the private repos instead on publishing on the Docker Hub. Nexus repository manager comes to the rescue here that can host all types of artifacts starting from jar, docker images, npm packages etc.

Prerequisites to take this exercise.

  • Docker Basics
  • Java Basics
  • Familiarity with Build engines like Maven/Gradle

We will first setup nexus repository on local box followed by setting up build process to publish the image to the same nexus repository.

Glossary

Docker

Docker is light weight container used for publishing apps with entire ecosystem packaged in single bundle.

Jib

Jib is a framework by to do build and compile docker images with and without docker daemon available on system. There are two plugins available for this one for maven and other for Gradle. In this exercise we are going to use jib maven plugin to publish docker image.

Nexus

Nexus is a repository manager tool, used for hosting various types of artifacts like jar, npm packages and Docker/OCI images.

Install Nexus

  •  Download Nexus from below location, you need to choose your operating system version https://www.sonatype.com/download-oss-sonatype
  •  Extract it to a location call it install_location for example D:\nexus
  •  Start Nexus by running command as administrator nexus-3.20.1-01\bin\nexus /run
  •  If step three fails due to port conflict, there will be file created by nexus start called nexus.properties at install_location\sonatype-work\nexus3\etc\nexus.properties You need to change the port to any port non-conflicting port 9081, note this file will be created only when nexus starts up successfully
  •  Login to nexus as admin
  • Your password is in admin.password file that gets created after nexus start-up
  • You need to find default password for admin you can find this at below location

Once you login to the system, Click on go to repositories tab from left menu and click create repository button shown below


In the second screen please select recipe as Docker hosted as a recipe type by double clicking it.


Now enter registry name and http port and rest of the things as default and click on create repository button at bottom part of the screen. This Http port used in pom.xml for publish



Steps

Now go to https://start.spring.io/ and download a basic spring boot app with maven as a build tool.


You also need to have maven configured in you class path or use mvnw command. Now open spring initializer project in editor of your choice, I used visual studio code but you can use any one.Open pom.xml of the spring boot project downloaded. Add below code snippet to the pom.xml of project. Jib plugin and right entries for Docker registry that we created in prerequisite section. Config part is very important as this step will make or break this exercise. Observe the URL where you want to publish the created image. It is currently pointing to nexus installation that we did in previous step. In case if the server is not local, you need to replace this localhost URL with the URL of target server where you want to publish.Find plugins section under pom.xml and add below entry to the pom.xml. Note you need to change the password same as what you created when setting up nexus.

         <plugin>

                <groupId>com.google.cloud.toolsgroupId>

                <artifactId>jib-maven-pluginartifactId>

                <version>3.4.0version>

                <configuration>

                <from> 

                    <image>openjdk:alpineimage> 

                from>

                <to> 

                    <image>localhost:10001/repository/firstdockerrepo/demoapp3/tags/newimage>

                <auth>

                    <username>adminusername>

                    <password>enteryourpasswordpassword>

                auth>

                to>

                

                <allowInsecureRegistries>

                true

                allowInsecureRegistries>

                <sendCredentialsOverHttp>

                true

                sendCredentialsOverHttp>

                configuration>

            plugin>

Save the file and run below command from commandline, under directory where you extracted spring boot application.

mvn compile jib:build -DsendCredentialsOverHttp=true -Djib.httpTimeout=0

Above command will build your first Docker image without installing Docker daemon on machine and push the to the nexus repo. Flag httpTimeout is worth mentioning as by default value for this in 2 seconds usually repos take longer than this hence I kept it to infinite. Flag SendCredentialsOverHttp is used for allowing http and authentication needs as Jib use https and credshelper. Here idea is to get things started and see things end to end.

In this exercise you have learnt setting up nexus repository manager running over http. You also learnt how to create a docker repo under nexus. Lastly, we modified pom.xml to publish the docker images during build process to nexus repo. Image built can be used by any docker client to be run his/her machine with the help of docker run command.