Java Spring Boot REST Application Part 3: Docker

Now that we have a running REST application, it would be nice to directly create a docker image to integrate the application into our DevOps infrastructure. Therefor a Maven plugin exists, that provide an easy way to create a docker container packaged with your application. Let’s have a look at the changes in the pom.xml first.

...
<properties>
  ...
 <docker.image.prefix>grisa</docker.image.prefix>
</properties>

<build>
 <plugins>
  ...
  <plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <version>0.4.11</version>
   <configuration>
    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
    <dockerDirectory>src/main/docker</dockerDirectory>
    <resources>
     <resource>
      <targetPath>/</targetPath>
      <directory>${project.build.directory}</directory>
      <include>${project.build.finalName}.jar</include>
     </resource>
    </resources>
   </configuration>
  </plugin>
 </plugins>
</build>

...

We added the docker-maven-plugin plugin and set the docker.image.prefix property.

Then we need to create a so called Dockerfile in src/main/docker (src/main/docker/Dockerfile).

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD rest_example-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Now we are able to build a docker container with the maven command:
mvn package docker:build
And we can see our new container in docker (docker images):
REPOSITORY          TAG     IMAGE ID      CREATED         SIZE
grisa/rest_example  latest  98fc335d1888  59 seconds ago  225.6 MB

Let’s start the container and expose port 8080:
docker run -d -p 8080:8080 grisa/rest_example
spring-boot-docker

The complete sample code can be found here.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.