Cloud Native 11 min read

Docker Installation, Spring Boot Packaging, and Log Management Guide

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

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Docker Installation, Spring Boot Packaging, and Log Management Guide

Docker Introduction

Docker is an open‑source container engine that packages applications and their dependencies into portable images, enabling consistent deployment on Linux or Windows hosts.

Install Docker

CentOS Installation

Docker requires CentOS 7 or later and a 64‑bit kernel (≥3.10). Ensure no previous Docker installation exists.

Update package source yum update Install utilities

yum install -y yum-utils device-mapper-persistent-data lvm2

Add Docker repo

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Install Docker CE yum install -y docker-ce Start Docker systemctl start docker Enable on boot systemctl enable docker Check status

systemctl status docker

Script Installation (alternative)

curl -fsSL https://get.docker.com/ | sh
wget -qO- https://get.docker.com/ | sh

Pull Java Image

docker pull java:8

Create Spring Boot Project

Generate a new Spring Boot project and add the spring-boot-web dependency.

Add Docker Maven Plugin

<properties>
    <docker.image.prefix>registry.aliyuncs.com/linhuatest</docker.image.prefix>
</properties>

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</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>

Create Dockerfile

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"]
Explanation: FROM specifies the base Java image, VOLUME creates a temporary directory, ADD copies the JAR, and ENTRYPOINT defines the start command.

Build and Run Docker Image

mvn clean package docker:build

After a successful build, verify the image: docker images Run the container:

docker run --name springboot-docker -p 9999:9999 -d <IMAGE_ID>

Check running containers: docker ps Access the test endpoint at http://<span>HOST_IP</span>:9999/test to confirm the application is running.

View Container Logs

Follow console logs: docker logs -f --tail=100 <CONTAINER_ID> To view Log4j2 file logs, enter the container: docker exec -it <CONTAINER_ID> /bin/bash Navigate to the log directory and tail the file:

cd work/spring-boot-docker
tail -100f info.log

Exit the container when done:

exit
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.

Dockercloud-nativecontainerizationSpringBootlog4j2CentOS
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.