Learn how to package your java application into a docker image

Learn how to package your java application into a docker image

Check this very straight tutorial and learn how you can package your java application into a docker image and run it as docker container.

Now that all cloud providers make the Kubernetes available and standardize docker as the default container, it′s very useful to package your application into a small image and delivery it not only for your cloud provider but also to your development and test environment team. So they can run and test your application without specific environment configuration that can make the behavior different from the production environment. 

Here we will learn how to create a Dockerfile that will build your application using Maven and package it into a docker image together with the JDK, then the image will contain all the dependencies and binaries necessary to run. 

To follow this tutorial the pre-requirements is to have the Docker and Git installed in your machine.

Open the command line console and clone the git repository with java project of the example:

git clone https://github.com/educostadev/poc-java-on-docker-image.git

Take a look into the Docker file:

FROM maven:3.6.0-jdk-8-alpine as builder
COPY  . /root/app/
WORKDIR /root/app
RUN mvn install
FROM openjdk:8-jdk-alpine as jdk8
EXPOSE 8081
COPY --from=builder /root/app/ /home/app/
WORKDIR /home/app
ENTRYPOINT ["java","-jar", "-Xmx15m", "./target/spring-service-0.0.1-SNAPSHOT.jar"]

Look the two FROM statements. This is possible due the Multi-stage feature from docker that allows you to use more than one docker image and copy the output from one to the next one. In this example the first block download a maven image, copy the project from your local machine to the maven image and run a mvn install command to download all the dependencies and compile the project. It′s important to note that all downloaded dependencies and the build result is resided inside the image and not on you local machine.

In the second statement the openjdk image is download and the port 8081 where our application is listening is exposed. Now comes the trick part, all the output content from the maven install result is copied to the openjdk image. The Dockerfile ends specifying how the application should be started. 

Now run the following command:

docker build -t multi-stage:latest . 

This will use the Dockerfile content to build . This can take some time according to you internet connection because the images and all maven dependencies will be downloaded. As a result a new image will be created with the named multi-stage and version latest

Run the spring application with the command:

docker run -p 8081:8081 --name=multi-stage --rm multi-stage:latest

This will run the Docker image into a container. The port 8081 is mapped to you have access from your localhost machine and test.

Check the video below and see how to test the container you ran:


References

要查看或添加评论,请登录

Edu Costa的更多文章

社区洞察

其他会员也浏览了