Deploying a Java War in a Docker Container – 在Docker容器中部署一个Java战争

最后修改: 2021年 8月 23日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn to deploy a Java WAR file inside a Docker container.

在本教程中,我们将学习在Docker容器内部署一个Java WAR文件。

We’ll deploy the WAR file on Apache Tomcat, a free and open-source web server that is widely used in the Java community.

我们将在Apache Tomcat上部署WAR文件,这是一个免费的、开源的Web服务器,在Java社区中被广泛使用。

2. Deploy a WAR File to Tomcat

2.将WAR文件部署到Tomcat

WAR (Web Application Archive) is a zipped archive file that packages all the web application-related files and their directory structure.

WAR(网络应用程序档案)是一个压缩的档案文件,它将所有与网络应用程序相关的文件及其目录结构打包。

To make things simple, deploying a WAR file on Tomcat is nothing but copying that WAR file into the deployment directory of the Tomcat server. The deployment directory in Linux is $CATALINA_HOME/webapps. $CATALINA_HOME denotes the installation directory of the Tomcat server.

简单地说,在Tomcat上部署WAR文件,无非是将该WAR文件复制到Tomcat服务器的部署目录。Linux中的部署目录是$CATALINA_HOME/webapps$CATALINA_HOME表示Tomcat服务器的安装目录。

After this, we need to restart the Tomcat server, which will extract the WAR file inside the deployment directory.

在此之后,我们需要重新启动Tomcat服务器,这将提取部署目录内的WAR文件。

3. Deploy WAR in Docker Container

3.在Docker容器中部署WAR

Let’s assume that we have a WAR file for our application, ROOT.war, which we need to deploy to the Tomcat server.

让我们假设我们的应用程序有一个WAR文件,ROOT.war,我们需要将其部署到Tomcat服务器。

To achieve our goal, we need to first create a Dockerfile. This Dockerfile will include all the dependencies necessary to run our application.

为了实现我们的目标,我们需要首先创建一个Docker文件。这个Docker文件将包括运行我们的应用程序所需的所有依赖项。

Further, we’ll create a Docker image using this Dockerfile followed by the step to launch the Docker container.

此外,我们将使用这个Docker文件创建一个Docker镜像,然后是启动Docker容器的步骤。

Let’s now dive deep into these steps one by one.

现在让我们逐一深入了解这些步骤。

3.1. Create Dockerfile

3.1.创建Docker文件

We’ll use the latest Docker image of Tomcat as the base image for our Dockerfile. The advantage of using this image is that all the necessary dependencies/packages are pre-installed. For instance, if we use the latest Ubuntu/CentOS Docker images, then we need to install Java, Tomcat, and other required packages manually.

我们将使用最新的Tomcat的Docker镜像作为我们Docker文件的基础镜像。使用该镜像的好处是,所有必要的依赖项/包都已预先安装。例如,如果我们使用最新的Ubuntu/CentOS Docker镜像,那么我们需要手动安装Java、Tomcat和其他必要的软件包。

Since all the required packages are already installed, all we need to do is copy the WAR file, ROOT.war, to the deployment directory of the Tomcat server. That’s it!

由于所有需要的包都已经安装,我们需要做的就是把WAR文件ROOT.war复制到Tomcat服务器的部署目录。这就是了!

Let’s have a closer look:

让我们仔细看看。

$ ls
Dockerfile  ROOT.war
$ cat Dockerfile 
FROM tomcat

COPY ROOT.war /usr/local/tomcat/webapps/

$CATALINA_HOME/webapps denotes the deployment directory for Tomcat. Here, CATALINA_HOME for the official Docker image of Tomcat is /usr/local/tomcat. As a result, the complete deployment directory would turn out to be /usr/local/tomcat/webapps.

$CATALINA_HOME/webapps 表示Tomcat的部署目录这里,Tomcat的官方Docker镜像的CATALINA_HOME/usr/local/tomcat。因此,完整的部署目录将变成/usr/local/tomcat/webapps

The application that we used here is very simple and does not require any other dependencies.

我们在这里使用的应用程序非常简单,不需要任何其他依赖性。

3.2. Build the Docker Image

3.2.构建Docker镜像

Let’s now create the Docker image using the Dockerfile that we just created:

现在让我们使用刚刚创建的Dockerfile来创建Docker镜像。

$ pwd
/baeldung
$ ls
Dockerfile  ROOT.war
$ docker build -t myapp .
Sending build context to Docker daemon  19.97kB
Step 1/2 : FROM tomcat
 ---> 710ec5c56683
Step 2/2 : COPY ROOT.war /usr/local/tomcat/webapps/
 ---> Using cache
 ---> 8b132ab37a0e
Successfully built 8b132ab37a0e
Successfully tagged myapp:latest

The docker build command will create a Docker image with a tag myapp. 

docker build命令将创建一个带有myapp标签的Docker镜像。

Make sure to build the Docker image from inside the directory where the Dockerfile is located. In our example above, we’re inside the /baeldung directory when we build the Docker image.

确保从Dockerfile所在的目录中构建Docker镜像。在我们上面的例子中,当我们构建Docker镜像时,我们在/baeldung目录中。

3.3. Run Docker Container

3.3.运行Docker容器

So far, we’ve created a Dockerfile and built a Docker image out of it. Let’s now run the Docker container:

到目前为止,我们已经创建了一个Docker文件,并从中建立了一个Docker镜像。现在我们来运行这个Docker容器。

$ docker run -itd -p 8080:8080 --name my_application_container myapp
e90c61fdb4ac85b198903e4d744f7b0f3c18c9499ed6e2bbe2f39da0211d42c0
$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
e90c61fdb4ac        myapp               "catalina.sh run"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->8080/tcp   my_application_container

This command will launch a Docker container with the name my_application_container using the Docker image myapp. 

该命令将使用Docker镜像my_application_container启动一个Docker容器,名称为myapp。

The default port for the Tomcat server is 8080. Therefore, while starting the Docker container, make sure to always bind the container port 8080 with any available host port. We’ve used host port 8080 for simplicity here.

Tomcat服务器的默认端口是8080。因此,在启动Docker容器时,确保始终将容器端口8080与任何可用的主机端口绑定。为了简单起见,我们在这里使用了主机端口8080。

3.4. Verify the Setup

3.4.验证设置

Let’s now verify everything that we’ve done so far. We’ll access the URL http://<IP>:<PORT> in the browser to view the application.

现在让我们验证一下我们到目前为止所做的一切。我们将在浏览器中访问URL http://<IP>:<PORT>,以查看应用程序。

Here, the IP denotes the public IP (or private IP in some cases) of the Docker host machine. The PORT is the container port that we have exposed while running the Docker container (8080, in our case).

这里,IP表示Docker主机的公共IP(或在某些情况下的私有IP)。PORT是我们在运行Docker容器时暴露的容器端口(在我们的例子中,是8080)。

We can also verify the setup using the curl utility in Linux:

我们也可以使用Linux中的curl工具验证设置。

$ curl http://localhost:8080
Hi from Baeldung!!!

In the command above, we’re executing the command from the Docker host machine. So, we are able to connect to the application using localhost. In response, the curl utility prints the raw HTML of the application webpage.

在上面的命令中,我们是从Docker主机上执行的命令。所以,我们能够使用localhost连接到应用程序。作为回应,curl工具打印出应用程序网页的原始HTML。

4. Conclusion

4.总结

In this article, we’ve learned to deploy a Java WAR file in a Docker container. We started by creating the Dockerfile using the official Tomcat Docker image. Then, we built the Docker image and ran the application container.

在这篇文章中,我们已经学会了在Docker容器中部署一个Java WAR文件。我们首先使用官方的Tomcat Docker镜像创建Docker文件。然后,我们构建了Docker镜像并运行了应用容器。

At last, we verified the setup by accessing the application URL.

最后,我们通过访问应用程序的URL验证了设置。