📜 ⬆️ ⬇️

Packaging a jvm application in a docker image

All the advantages of the docker for the application have already been described many times on Habré, like its architecture.

We will solve the practical task of packaging the jvm application and get a container with a miniature Linux, JDK and our application, which we publish on hub.docker.com and will be able to run anywhere.

Source application


If your jvm application consists of more than one file and the image needs to be generated regularly / automatically, then you can integrate the build process into maven using the com.spotify plugin : docker-maven-plugin . If you use Gradle, then se.transmode.gradle: gradle-docker will help you build the docker image. An example for maven posted on github .

In the publication we use jar from the central maven repository . This is its own groove build, which is notable for aether provide packaged to resolve Grape dependencies and extended protocol support for java.net.URL. For inquisitive minds:
')

For me, this is a handy tool that I often use. As an example, loading a jvm agent into a running JVM , an installer for crate.io , an example of a site parser ...

Now groovy-grape-aether is also available in the suckerukov / docker-groovy docker image. In a similar way, you can pack your jvm application.

Dockerfile


Describes on the basis of which existing image and with the help of which commands to form a new image.

We use the way frolvlad / alpine-oraclejdk8 , with gratitude frol for his work! This image is based on Alpine Linux image + glibc and Oracle JDK 8u102.14. Compact enough compared to debian / ubuntu based images. As an alternative, the popular anapsix / alpine-java image is possible.

FROM frolvlad/alpine-oraclejdk8 EXPOSE 8080 ENV GROOVY_VERSION=2.4.5.4 RUN mkdir "/usr/lib/groovy" && \ wget "http://repo1.maven.org/maven2/com/github/igor-suhorukov/groovy-grape-aether/$GROOVY_VERSION/groovy-grape-aether-$GROOVY_VERSION.jar" -O /usr/lib/groovy/groovy-grape-aether.jar ENTRYPOINT ["java","-jar","/usr/lib/groovy/groovy-grape-aether.jar"] CMD ["--help"] 

Here I indicated that port 8080 will listen to the application inside the container. When starting the container, you can specify the -P parameter and then it will be tied to the dynamic port of the host machine, or the -p parameter and you can specify which port of the host corresponds to the port of the container.

ENTRYPOINT determines which command with parameters will be launched at the start of the container. For the command, it is possible to specify the default parameters using CMD.

We collect image locally


Run in the same directory as our Dockerfile command.

 docker build -t docker-groovy . 

Publish to hub.docker.com


You can skip this section if your project does not openource and you don’t need to share your image with the world.

We publish Dockerfile on github


In your github account, select New Repository and put the prepared Dockerfile there. This example uses the docker-groovy repository.

Customize the assembly in hub.docker.com


We create a free account on the main page of hub.docker by filling out the form and clicking "Sign Up". Be sure to activate it using emails.

In your account settings, go to the “Linked Accounts & Services” tab and configure “Link Github” to your github account (another Bitbucket account option).

In the menu at the top of the page, select Create-> Create Automated Build, then click “Create Auto-build Github”, select the repository and specify where in the Dockerfile repository. At the next push to the repository on github, the automatic build will start. You can also start the build manually.

As a result of all the above actions, such a project on hub.docker.com has turned out.

Container loading and use


Before use, we will receive an image from hub.docker.

 docker pull suhorukov/docker-groovy 

And run the container with the gitblit.groovy script parameter.

 docker run -d -p 8080:8080 suhorukov/docker-groovy https://raw.githubusercontent.com/igor-suhorukov/git-configuration/master/gitblit.groovy 

If an application needs access to resources that are external to the container in the file system (directories of the host machine, NFS, distributed file system), then you need to specify the mount points when creating the image in the VOLUME section of the Dockerfile. If you need distributed execution and container containers, Kubernetes / Mesos / Swarm / fabric8.io / Rancher is more suitable for this technology that works with docker.

Using the jvm application as an example, we created and published a miniature image on hub.docker.com, after which we launched the docker container with the application settings.

Source: https://habr.com/ru/post/310530/


All Articles