Cloud Native 11 min read

Step‑by‑Step Guide to Installing Docker on CentOS, Packaging a Spring Boot Application into a Docker Image, and Managing the Container

This tutorial explains how to install Docker on CentOS, pull a Java base image, create and configure a Spring Boot project, write a Dockerfile, build the image with Maven, run the container with port mapping, and access both console and Log4j2 file logs.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Step‑by‑Step Guide to Installing Docker on CentOS, Packaging a Spring Boot Application into a Docker Image, and Managing the Container

Docker is an open‑source container engine that lets developers package applications and their dependencies into portable images that run on any Linux or Windows host.

For CentOS 7 or later (32‑bit not supported, kernel >= 3.10) ensure no previous Docker installation exists, then update the package repository: yum update Install required utilities and add the stable Docker repository:

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Refresh the repo and install Docker CE:

yum update
yum install -y docker-ce

Start Docker and enable it to start on boot:

systemctl start docker
systemctl enable docker

Other service commands include systemctl restart docker, systemctl stop docker, and systemctl status docker.

Alternatively, install Docker via the official convenience script: curl -fsSL https://get.docker.com/ | sh or wget -qO- https://get.docker.com/ | sh Pull a Java 8 base image for later use: docker pull java:8 Verify the image with docker images. If the image appears, the pull succeeded.

Create a Spring Boot project (e.g., with Spring Initializr), add the docker-maven-plugin configuration in pom.xml to define the image name, Docker directory, and resources to include.

Write a Dockerfile in src/main/docker:

FROM java:8
VOLUME /tmp/tomcat
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar springboot-docker.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-docker.jar"]

Implement a simple TestController that logs to the console and to Log4j2, and configure Log4j2 (e.g., log4j2.xml) and application.yml (server port 9999).

Build the Docker image with Maven: mvn clean package docker:build Run the container, mapping host port 9999 to container port 9999:

docker run --name springboot-docker -p 9999:9999 -d <image-id>

Check the running container with docker ps and test the endpoint in a browser: http://<i>host-ip</i>:9999/test.

View real‑time console logs with docker logs -f <container-id>. To see Log4j2 file logs, exec into the container ( docker exec -it <container-id> /bin/bash), navigate to the log directory (e.g., /work/spring-boot-docker), and tail the info.log file.

The guide concludes with a reminder to exit the container when finished.

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.

mavenSpring Boot
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.