简体   繁体   English

如何将war文件正确部署到docker-compose中运行的码头?

[英]How to correctly deploy war file to jetty running in docker-compose?

I am trying to run a java servlet on jetty in docker-compose.我正在尝试在 docker-compose 的码头上运行 java servlet。

This is my docker-compose.yml:这是我的 docker-compose.yml:

version: "3.2"

services: 
    app:
        image: jetty:9.4-jre8
        ports: 
        - "8080:8080"
        volumes:
        - /target/example.war:/var/lib/jetty/webapps/root.war

If I docker exec into the container and change into /var/lib/jetty/webapps/ directory (as specified in the docker-compose) I can see the root.war file is present.如果我将 docker exec 进入容器并更改为/var/lib/jetty/webapps/目录(如 docker-compose 中所指定),我可以看到root.war文件存在。

I use the following docker command to do that after starting with docker-compose up -d :docker-compose up -d开始后,我使用以下docker命令执行此操作:

docker exec -it <CONTAINER> /bin/bash/

Then cd into the webapps directory and ls .然后cd进入 webapps 目录和ls

However on localhost:8080/ I get a 404 response.但是在 localhost:8080/ 我得到一个 404 响应。

This is the HelloServlet.java I am using:这是我正在使用的HelloServlet.java

package org.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello Servlet</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

And this is the configuration in src/main/webapp/WEB-INF/web.xml :这是src/main/webapp/WEB-INF/web.xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="false" version="3.1">

  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>retro.server.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>

</web-app>

If I run jetty through the maven plugin I get the Servlet responding.如果我通过 maven 插件运行码头,我会得到 Servlet 响应。 Only run in docker-compose or also docker I get only 404.仅在 docker-compose 或 docker 中运行,我只得到 404。

This is the error I receive in browser: 404 jetty in docker-compose这是我在浏览器中收到的错误: docker-compose 中的 404 jetty

Is there any additional configuration to be done?是否有任何额外的配置要做? Is there something obviously wrong with the setup?设置明显有问题吗?

What I am ultimately trying to achieve is run a GraphQl servlet on jetty in docker-compose!我最终想要实现的是在 docker-compose 的码头上运行 GraphQl servlet!

This is the maven pom.xml :这是 maven pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org</groupId>
  <artifactId>server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>server Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jettyVersion>9.4.30.v20200611</jettyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-java</artifactId>
      <version>15.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java-tools -->
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-java-tools</artifactId>
      <version>4.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java-servlet -->
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-java-servlet</artifactId>
      <version>4.7.0</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jettyVersion}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>example</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>${jettyVersion}</version>
        </plugin>
        <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Finally I found out that a.最后我发现了。 was missing in the volumes section of the docker-compose.yml like so: -./target/example.war:/var/lib/jetty/webapps/root.war docker-compose.yml的卷部分中缺少,如下所示:-./target/example.war: -./target/example.war:/var/lib/jetty/webapps/root.war

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM