How to use Alex collins docker maven plugin

The “alexec/docker-maven-plugin” is a maven plugin which is used to build, test and publishing the docker images. Refer this link for more information about this plugin(https://github.com/alexec/docker-maven-plugin).

Check out my sample project @ https://github.com/dkbalachandar/java-rest-docker. This is a simple Hello world REST application running on Docker.

In this post, I am going to show how to use “alexec” docker maven plugin with an example. Let’s go into the details one by one.

1. The first step is to add the com.alexecollins.docker plugin in the build section of the pom.xml. Check below snippet.

java-rest-docker/pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>hello-rest</artifactId>
    <version>1.0</version>
    <properties>
       <docker.image.name>hello-rest</docker.image.name>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.Grizzly</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.Grizzly</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- Add the Alex Collins plugin-->
            <plugin>
                <groupId>com.alexecollins.docker</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>2.11.21</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>clean</goal>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.grizzly</groupId>
            <artifactId>grizzly-http-server</artifactId>
            <version>2.3.22</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.grizzly</groupId>
            <artifactId>grizzly-http-servlet</artifactId>
            <version>2.3.24</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-servlet</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
</project>

2. Next step is to create a folder named ‘docker’ under src/main. Then go into ‘docker’ folder and create an another folder named ‘helloRest’. You can give name any name you want.

3. Next step is to create a conf.yml and give all the configuration information in it. Refer the below snippet.

java-rest-docker/src/main/docker/helloRest/conf.yml:


packaging:
  add:
    - target/${project.build.finalName}-jar-with-dependencies.jar
    - src/main/bin
tags:
    - ${project.artifactId}:latest

I am using the project.artifactId as my image name(hello-rest). If your project has any upper case letter then this plugin wont work. Make sure to give lower case letter always.

Assume that, the project artifactId is “hello-Rest” and when you try to build the project, you will get the below error.


[ERROR] Error during callback
com.alexecollins.docker.orchestration.OrchestrationException: com.github.dockerjava.api.exception.InternalServerErrorException: Error parsing reference: "hello-Rest:latest" is not a valid repository/tag

	at com.alexecollins.docker.orchestration.DockerOrchestrator.build(DockerOrchestrator.java:395)
	at com.alexecollins.docker.orchestration.DockerOrchestrator.build(DockerOrchestrator.java:323)
	at com.alexecollins.docker.orchestration.DockerOrchestrator.build(DockerOrchestrator.java:855)
	at com.alexecollins.docker.mojo.PackageMojo.doExecute(PackageMojo.java:16)
	at com.alexecollins.docker.mojo.AbstractDockerMojo.execute(AbstractDockerMojo.java:164)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: com.github.dockerjava.api.exception.InternalServerErrorException: Error parsing reference: "hello-Rest:latest" is not a valid repository/tag



4. Next step is to create the DockerFile. Check the below snippet which has all the information.

java-rest-docker/src/main/docker/helloRest/Dockerfile



FROM java:8

WORKDIR /opt

ADD hello-rest-${project.version}-jar-with-dependencies.jar /opt/helloRest.jar
ADD bin /opt/bin

RUN chmod +x /opt/helloRest.jar
RUN chmod +x /opt/bin/run.sh

CMD ["/opt/bin/run.sh"]

EXPOSE 8080


5. Run the ‘mvn clean’ command and check the build log and confirm that the image ‘hello-rest’ is built with the configuration information from conf.yml and Dockerfile.
6. Run the ‘docker images’ command to confirm the image availability.
7. Finally, Run the docker image with this command ‘docker run -p 8080:8080 hello-rest’ and test it with this URL http://localhost:8080/api/greeting.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s