This example creates a Docker
image from a Spring Boot
application using Spotify's Docker Maven plugin
.
The project consist of two modules:
busyunit-email-model
- contains all the models used in the exampledocker-spring-service
- contains Spring Boot example service and related classes
Make sure you have Maven
installed. Execute the following maven command from the directory of the
parent project, busyunit-email
:
mvn clean install
It will create the Spring Boot executable JAR,busyunit-email-service-1.0.jar
, under busyunit-email-service/target
folder.
To run the newly created Spring Boot JAR from the terminal:
java -jar busyunit-email-service-1.0.jar
This should start up the example application at port 8080
. The application can be accessed at https://localhost:8080
Before you build the Docker image, make sure Docker is available in your environment.
Execute the following maven command from the directory of the parent project, busyunit-email
:
mvn clean package docker:build
This should build a Docker image named busyunit-email
.
Run the newly created Docker image, busyunit-email
, by executing the
docker run
command from the terminal:
docker run --rm -p 8080:8080 --name=cheetos busyunit-email
--rm
option automatically clean up the container and remove the file system when the container exit.--name
option names the Docker container ascheetos
. In absence of the--name
option, the Docker generates a random name for your container.-p 8080:8080
option publishes all exposed ports to the host interfaces. In our example, it is port8080
is bothhostPort
andcontainerPort
This should start up the example application and it can be accessed at https://localhost:8080
Run the docker ps
to list all the containers.
To see all running containers, execute the following command:
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d03854fb7779 busyunit-email "java -Djava.security" 7 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp cheetos
bash-3.2$
To see all running containers including the non-running ones, execute the following command:
bash-3.2$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d03854fb7779 busyunit-email "java -Djava.security" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp cheetos
28b2cff9e7e6 busyunit-email "java -Djava.security" About an hour ago Exited (0) About an hour ago indra1
d2720676c932 nginx "nginx -g 'daemon off" 4 months ago Exited (0) 4 months ago webserver
To remove a Docker container, execute docker rm
command. This will remove a non-running container.
bash-3.2$ docker rm indra1
indra1
To forcefully remove a running container
bash-3.2$ docker rm -f cheetos
cheetos
To stop a container, execute docker stop
command:
bash-3.2$ docker stop cheetos
cheetos
We are using Spotify's Docker Maven Plugin
. It's relatively easy to
setup in your Maven pom.xml
. Complexity rises when you specify the plugin in the parent POM. Here are the changes to
the parent pom.xml
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<skipDockerBuild>true</skipDockerBuild>
</configuration>
</plugin>
</plugins>
</build>
The skipDockerBuild
tag is set to true
in order to skip the docker build in the parent pom.
Changes to the child pom.xml
where Spring Boot JAR gets created:
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skipDockerBuild>false</skipDockerBuild>
<imageName>${docker.image.name}</imageName>
<dockerDirectory>${basedir}/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>
Here is the skipDockerBuild
tag is set to false
to override the parent flag.
imageName
specifies the name of our example Docker image, e.g,busyunit-email
dockerDirectory
specifies the location of theDockerfile
. The contents of the dockerDirectory will be copied into${project.build.directory}/docker
. ADockerfile
specfies all the instructions to be read by Docker while building the image.include
specfies the resources to be included, which ion our case isbusyunit-email-service-1.0.jar
Here is the content of the example Dockerfile
.
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD busyunit-email-service-1.0.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Dapp.port=${app.port}", "-jar","/app.jar"]
LABEL maintainer "Ram Ayall"
FROM
instruction sets the Base Image for subsequent instructions. FROM must be the first non-comment instruction in the Dockerfile.VOLUME
instruction creates a mount point with the specified name.ADD
instruction copies from<src>
and adds them to the filesystem of the image at the path<dest>
.RUN
instruction executes the command on top of the current image.EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime.ENV
instruction sets the environment variableENTRYPOINT
allows you to configure a container that will run as an executable.LABEL
instruction adds metadata to an image.
You can find out more about Docker instructions here