{{announcement.body}}
{{announcement.title}}

How to Publish Docker Images on Private Nexus Repository Using Jib Maven Plugin

DZone 's Guide to

How to Publish Docker Images on Private Nexus Repository Using Jib Maven Plugin

In this article, see how to publish Docker images on a private Nexus repository using Jib Maven plugin.

· Cloud Zone ·
Free Resource

Glossary

Docker

Docker is a lightweight container used for publishing apps with an entire ecosystem packaged in single bundle.

Jib

Jib is a framework that builds and compiles Docker images with and without Docker daemon available on the system. There are two plugins available for this one, for Maven, and for Gradle. In this exercise, we are going to use a jib maven plugin to publish a Docker image.

Nexus

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

Prerequisites

There are some prerequisites for this tutorial to be completed.

1. Download Nexus from below location; you need to choose your operating system version

https://www.sonatype.com/download-oss-sonatype

2. Extract it to a location call it install_location for example D:\nexus

3. Start Nexus by running command as administrator

 install_location\nexus-3.20.1-01\bin\nexus /run 

3.1 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

4. Login to nexus as admin

Your password is in admin.password file that gets created after nexus start-up

4.1 You need to find the default password for admin you can find this at below location

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

 

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

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

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 your class path or use mvnw command.

Now open spring initializer project in the editor of your choice. I used visual studio code, but you can use any.

We need to add the below things to pom.xml of the project. Jib plugin and right entries for Docker registry that we created in the prerequisite section. The config part is very important, as this step will make or break this exercise.

Find the plugins section under pom.xml and add the below entry to the pom.xml. Note you need to change the password to the same as what you created when setting up nexus.

Java
 




x
47


 
1
            <plugin>
2
 
          
3
                 <groupId>com.google.cloud.tools</groupId>
4
 
          
5
                 <artifactId>jib-maven-plugin</artifactId>
6
 
          
7
                <version>1.8.0</version>
8
 
          
9
                <configuration>
10
 
          
11
                <from> 
12
 
          
13
                    <image>openjdk:alpine</image> 
14
 
          
15
                </from>
16
 
          
17
                <to> 
18
 
          
19
                    <image>localhost:10001/repository/firstdockerrepo/demoapp3/tags/new</image>
20
 
          
21
                <auth>
22
 
          
23
                    <username>admin</username>
24
 
          
25
                    <password>enteryourpassword</password>
26
 
          
27
                </auth>
28
 
          
29
                </to>
30
 
          
31
                
32
 
          
33
                <allowInsecureRegistries>
34
 
          
35
                true
36
 
          
37
                </allowInsecureRegistries>
38
 
          
39
                <sendCredentialsOverHttp>
40
 
          
41
                true
42
 
          
43
                </sendCredentialsOverHttp>
44
 
          
45
                </configuration>
46
 
          
47
            </plugin>


Save the file and run the below command from command line under the directory where you extracted the Spring Boot application.

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

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

Topics:
deploy cycle, devops, docker, images, jib, maven 3.0, nexus 3, repository, sonatype

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}