乐于分享
好东西不私藏

用一个简单项目说明Docker的重要性

用一个简单项目说明Docker的重要性

由于之前学的大而杂,感觉很多场景都有做过,但讲起来很模糊,不如直接动手做一遍,做完后再汇总。

本次通过一个简单的JAVA项目构建,来说明为什么要用Docker去部署。

使用传统方式开发运维

不多闲扯,直接上环境和代码。

基础环境准备

一、JDK安装

~]# dnf -y install java-21-openjdk.x86_64~]# java -versionopenjdk version "21.0.12" 2026-07-21 LTSOpenJDK Runtime Environment (Red_Hat-21.0.12.0.8-1) (build 21.0.12+8-LTS)OpenJDK 64-Bit Server VM (Red_Hat-21.0.12.0.8-1) (build 21.0.12+8-LTS, mixed mode, sharing)

二、Maven安装

~]# curl -o maven-3.9.16.tar.gz https://dlcdn.apache.org/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gz~]# tar xf maven-3.9.16.tar.gz -C /usr/local/~]# ln -s /usr/local/apache-maven-3.9.16 /usr/local/maven~]# vi ~/.bashrc ~]# source .bashrc~]# mvn -vApache Maven 3.9.16 (2bdd9fddda4b155ebf8000e807eb73fd829a51d5)Maven home: /usr/local/mavenJava version: 21.0.12, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdkDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "6.12.0-211.16.1.el10_2.0.1.x86_64"arch"amd64", family: "unix"

三、创建Java项目

# 创建根目录并进入[root@localhost ~]# mkdir hello-webapp[root@localhost ~]# cd hello-webapp/[root@localhost hello-webapp]# mkdir -p src/main/java/com/example[root@localhost hello-webapp]# mkdir -p src/main/webapp/WEB-INF[root@localhost hello-webapp]# vi pom.xml[root@localhost hello-webapp][root@localhost hello-webapp]# vi src/main/webapp/WEB-INF/web.xml[root@localhost hello-webapp]# vi src/main/java/com/example/HelloServlet.java# 在项目根目录执行[root@localhost hello-webapp]# mvn clean package[INFO] Scanning for projects...[INFO[INFO] ----------------------< com.example:hello-webapp >----------------------[INFO] Building hello-webapp 1.0-SNAPSHOT[INFO]   from pom.xml[INFO] --------------------------------[ war ]---------------------------------Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom...[INFO[INFO] --- clean:3.2.0:clean (default-clean) @ hello-webapp ---Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.3.4/maven-shared-utils-3.3.4.pom...[INFO] Packaging webapp[INFO] Assembling webapp [hello-webapp] in [/root/hello-webapp/target/hello][INFO] Processing war project[INFO] Copying webapp resources [/root/hello-webapp/src/main/webapp][INFO] Building war: /root/hello-webapp/target/hello.war[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:  01:33 min[INFO] Finished at: 2026-08-13T18:41:11+08:00[INFO] ------------------------------------------------------------------------# 发现target目录下有hello.war表示成功[root@localhost hello-webapp]# lspom.xml  src  target[root@localhost hello-webapp]# ls -l target/classes/           generated-sources/ hello/             hello.war          maven-archiver/    maven-status/      [root@localhost hello-webapp]# ls -l target/hello.war -rw-r--r--. 1 root root 2960 Aug 13 18:41 target/hello.war

四、部署Tomcat

# 为了安全起见,不建议使用root用户]# groupadd tomcat[root@localhost ~]# useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat[root@localhost ~]# mkdir /opt/tomcatmkdir: cannot create directory ‘/opt/tomcat’: File exists[root@localhost ~]# chown -R tomcat:tomcat /opt/tomca~]# curl -o apache-tomcat-9.0.120.tar.gz https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.120/bin/apache-tomcat-9.0.120.tar.gz[root@localhost ~]# tar xf apache-tomcat-9.0.120.tar.gz -C /opt/tomcat/ --strip-components=1[root@localhost ~]# chown -R tomcat:tomcat /opt/tomcat/[root@localhost ~]# chmod +x /opt/tomcat/bin/*.sh# 使用Tomcat自动脚本启动和停止服务cd /opt/tomcat/bin$ ./startup.shMAINPID# 重启策略Restart=on-failureRestartSec=10StartLimitInterval=60StartLimitBurst=3# 超时设置TimeoutStartSec=120TimeoutStopSec=30[Install]WantedBy=multi-user.target# 启用服务~]# systemctl daemon-reload~]# systemctl enable tomcat~]# systemctl start tomcat~]# systemctl status tomcat

五、测试验证

~]# curl localhost:8080/hello/hello<!DOCTYPE html><html><head><title>Hello</title></head><body><h1style='color:blue;'>Hello, World! 🚀</h1><p>This is a Java Web App deployed on Tomcat.</p></body></html> 

将项目部署到Tomcat

部署有多种方式,此处使用的是配置虚拟主机。

[root@localhost ~]# ll /root/hello-webapp/target/hello.war -rw-r--r--. 1 root root 2960 Aug 13 18:41 /root/hello-webapp/target/hello.war# 查看配置]# grep -A2 -B2 "Context" /opt/tomcat/conf/server.xml | grep -v "^--"      <Hostname="localhost"appBase="webapps"            unpackWARs="true" autoDeploy="true">        <Contextpath="/hello"docBase="/root/" />]# mkdir -pv /opt/tomcat/installmkdir: created directory '/opt/tomcat/install'# 移动安装包到这个目录下~]# cp /root/hello-webapp/target/hello.war /opt/tomcat/install/]# chown tomcat:tomcat /opt/tomcat/install/hello.war~]# vi /opt/tomcat/conf/server.xml<Hostname="www.example.com"appBase="webapps"unpackWARs="true"autoDeploy="true">    <Contextpath="/hello"docBase="/root/hello-webapp/target/hello.war" /></Host># 本地测试可通过修改/etc/hosts映射域名到服务器IP,访问http://www.example.com:8080即可~]# systemctl restart tomcat~]# curl localhost:8080/hello/hello<!DOCTYPE html><html><head><title>Hello</title></head><body><h1style='color:blue;'>Hello, World! 🚀</h1><p>This is a Java Web App deployed on Tomcat.</p></body></html> 

最终部署完成。

使用Docker开发运维

一、下载镜像

]# docker pull docker.io/tomcat:8-jdk8-openjdk]# docker images

二、创建Java项目

在传统方式第三步已有,不再赘述。但现在开发也有更好的Dev方式,很多不像以前那么麻烦了。

二、编写Dockerfile

~]# cd hello-webapp/hello-webapp]# vi DockerfileFROM tomcat:8-jdk8-openjdkRUN rm -rf /usr/local/tomcat/webapps/ROOTCOPY target/hello.war /usr/local/tomcat/webapps/EXPOSE 8080CMD ["catalina.sh""run"]

三、构建镜像

hello-webapp]# docker build -t hello-webapp:v1.0 .

四、运行容器

docker run -d -p 8080:8080 --name hello-app hello-webapp:1.0

五、测试验证

浏览器访问:http://localhost:8080/hello/hello

为什么使用Docker

如果不使用Docker,通常会出现如下问题:

1、开发环境没有问题,测试环境报错,排查后发现是启动脚本里JDK版本跟代码要求的版本不一致。

2、测试环境没问题,生产环境报错,排查发现是Tomcat版本和JDK版本不匹配。

此时就会出现开发、运维、测试同事相互扯皮,影响产品上线效率,最重要的是还影响团队绩效。

使用Docker,可以确保"一次部署,到处运行"。

写在最后

使用Docker后,我终于明白:真正的稳定不是反复排查环境差异,而是让代码从出生就带着它的“生态系统”一起走,从而把精力从扯皮中解放出来,专注于更有价值的创造。