Step-by-Step Guide to Install JDK, Maven, Create a Spring Boot Application, and Deploy It with Docker

This tutorial walks through installing JDK and Maven on a CentOS server, cloning a Spring Boot Hello World project, building it with Maven, installing Docker, creating a Dockerfile, building and running the Docker image, and finally managing the container lifecycle.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Step-by-Step Guide to Install JDK, Maven, Create a Spring Boot Application, and Deploy It with Docker

Install JDK

yum -y install java
[root@dockerserver ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

Install Maven

wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

tar zxf apache-maven-3.6.1-bin.tar.gz  -C /usr/local/
echo "export M2_HOME=/usr/local/apache-maven-3.6.1" >> /etc/profile
echo "export PATH=\$PATH:\$M2_HOME/bin" >> /etc/profile
source /etc/profile

mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T15:00:29-04:00)
Maven home: /usr/local/apache-maven-3.6.1
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.el7.x86_64", arch: "amd64", family: "unix"

Create a Spring Boot Project

Project repository: https://github.com/gazgeek/springboot-helloworld.git

yum -y install git wget
git clone https://github.com/gazgeek/springboot-helloworld.git
cd springboot-helloworld
mvn clean install -DskipTests
ls target/

systemctl stop firewalld
java -jar helloworld-0.0.1-SNAPSHOT.jar   # start service
nohup java -jar helloworld-0.0.1-SNAPSHOT.jar &

Install Docker

yum -y install docker
vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'

systemctl start docker
systemctl status docker
systemctl enable docker

yum -y update
reboot

Create Dockerfile

FROM openjdk:8
MAINTAINER zy

COPY ./helloworld-0.0.1-SNAPSHOT.jar helloworld-0.0.1-SNAPSHOT.jar
EXPOSE 8080
CMD ["java","-jar","helloworld-0.0.1-SNAPSHOT.jar"]

Build Docker Image

docker build -t mytest/docker/springbootdemo:latest .

[root@dockerserver ~]# docker build -t mytest/docker/springbootdemo:latest .
Sending build context to Docker daemon 35.98 MB
Step 1/5 : FROM openjdk:8
... (output truncated for brevity) ...
Successfully built 8189ee4c7f86

# View images
[root@dockerserver ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
mytest/docker/springbootdemo   latest              8189ee4c7f86        34 seconds ago      501 MB
docker.io/openjdk              8                   08ded5f856cc        2 weeks ago         488 MB

# Delete image
docker rmi 8189ee4c7f86
Untagged: mytest/docker/springbootdemo:latest
Deleted: sha256:8189ee4c7f86766100db7a1df412c797d460eb18861913f352befdbbc25df859
... (other layers deleted) ...

Run Docker Container

docker run -itd -p 8080:8080  mytest/docker/springbootdemo:latest

docker run -itd mytest/docker/springbootdemo:latest

8c2b9a3b545c021a3f8d06d96e36d55f607e75afdfcffa251e68ed3975c70553
docker ps

CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS               NAMES
8c2b9a3b545c        mytest/docker/springbootdemo:latest   "java -jar hellowo..."   7 seconds ago       Up 6 seconds        8080/tcp            wizardly_brattain

# Exec into container and test
docker exec -it 8c2b9a3b545c /bin/bash
root@8c2b9a3b545c:/# curl 127.0.0.1:8080
Hello from GazGeek!

Stop and Delete Containers

docker stop
docker rm

[root@dockerserver ~]# docker rm $(docker ps -a -q)
e976498656af
2330b6a8875e
7ace91b113ae
f1adee2c6f37
... (other containers) ...
Error response from daemon: You cannot remove a running container 8c2b9a3b545c021a3f8d06d96e36d55f607e75afdfcffa251e68ed3975c70553. Stop the container before attempting removal or use -f

[root@dockerserver ~]# docker ps
... (running container list) ...

[root@dockerserver ~]# docker rm $(docker ps -a -q) -f
8c2b9a3b545c
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaDockerBackend DevelopmentDevOpsmavenSpring Boot
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.