Operations 5 min read

Building and Running a Java Application with Dockerfile

This guide explains how to create a Dockerfile for a Java JAR, describes the purpose of each Docker instruction, shows how to build the image, run the container with proper ports and volumes, and view container logs.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Building and Running a Java Application with Dockerfile

This article demonstrates how to containerize a Java application by creating a Dockerfile and using Docker commands.

Step 1: Prepare the Dockerfile – Place the JAR file in an empty directory and create a Dockerfile with the following essential instructions:

FROM <image_name>:<tag>
RUN <linux_command_to_build_or_prepare>
COPY [--chown=<user>:<group>] <source_path>... <destination_path>
CMD nohup java -jar xxxx.jar > xxx.log 2>&1 &

The FROM line defines the base image (e.g., nginx or a JDK image). RUN executes commands during the image build, either in shell form ( RUN <command>) or exec form ( RUN ["executable", "arg1", "arg2"]). COPY copies files from the build context into the image, and CMD specifies the default command that runs when the container starts (it can be overridden at docker run time). Note that if multiple CMD statements exist, only the last one takes effect.

Step 2: Example Dockerfile for the Java app

EXPOSE 8031
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Step 3: Build the image docker build -t haha-shen-standard . Step 4: Run the container

First attempt (may fail if the port is occupied):

docker run -d --restart=always --name haha-shen-standard -v /usr/local/apps/haha-shen-standard/logs:/opt/java/logs -p 8080:8031 haha-shen-standard

If the port is in use, change the host port and container name:

docker run -d --restart=always --name haha-shen-standard01 -v /usr/local/apps/haha-shen-standard/logs/:/opt/java/logs/ -p 8081:8031 haha-shen-standard

After the container starts, you can verify it with: docker ps -a and view its logs: docker logs --tail 100 -f haha-shen-standard01 This workflow provides a complete end‑to‑end example of containerizing a Java JAR, building the image, running it with persistent log storage, and troubleshooting common issues such as port conflicts.

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.

DevOpsContainerDockerfile
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

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.