无法访问tomcat,与docker集成时未部署war文件

0wi1tuuw  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(508)

已使用成功生成war文件 mvn clean install ,然后用 docker compose up --build 在终端中。终端中的日志显示没有创建任何表,并且无法使用postman正确到达端点。由于war文件部署不当,tomcat处于无法访问状态。我需要一个解决方案来连接数据库和服务器,并到达端点。我找不到任何有助于解决此问题的信息。请帮助我实现正确部署。
文件结构
pom.xml

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>EcomApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EcomApplication</name>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <finalName>ecom</finalName>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

    </plugins>
</build>

终端日志:

sowbharnika@SowbharnikasMBP EcomApplication % docker compose up --build 
[+] Building 4.7s (11/11) FINISHED                                                                                                                                     
 => [itomcat-ecom internal] load build definition from Dockerfile                                                                                                 0.0s
 => => transferring dockerfile: 217B                                                                                                                              0.0s
 => [ipostgres-ecom internal] load build definition from Dockerfile                                                                                               0.0s
 => => transferring dockerfile: 137B                                                                                                                              0.0s
 => [itomcat-ecom internal] load .dockerignore                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                   0.0s
 => [ipostgres-ecom internal] load .dockerignore                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                   0.0s
 => [itomcat-ecom internal] load metadata for docker.io/library/tomcat:8.0-jre8                                                                                   3.1s
 => [ipostgres-ecom internal] load metadata for docker.io/library/postgres:10.2                                                                                   3.1s
 => CACHED [ipostgres-ecom 1/1] FROM docker.io/library/postgres:10.2@sha256:7361bae1fbf5642099663d1f02dc949cabde1f86727bf8ff00d0a8806640a617                      0.0s
 => [itomcat-ecom] exporting to image                                                                                                                             0.2s
 => => exporting layers                                                                                                                                           0.2s
 => => writing image sha256:388cf029752f4cac431dfb8eb41170dbd547bdb25e2542222d85355af59d96ed                                                                      0.0s
 => => naming to docker.io/library/ipostgres-ecom                                                                                                                 0.0s
 => => writing image sha256:619f0674184633b5b1c4c81d08fc714b42625b547bd341486f321e706c75f744                                                                      0.0s
 => => naming to docker.io/library/itomcat-ecom                                                                                                                   0.0s
 => [itomcat-ecom internal] load build context                                                                                                                    1.0s
 => => transferring context: 37.56MB                                                                                                                              1.0s
 => CACHED [itomcat-ecom 1/2] FROM docker.io/library/tomcat:8.0-jre8@sha256:268403c3fa09afd9310ced7e83ac021927dca0f04a394a80a0b0220f62e056ed                      0.0s
 => [itomcat-ecom 2/2] ADD target/ecom.war /usr/local/tomcat/webapps                                                                                              0.1s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/2
 ⠿ Network ecomapplication_default        Created                                                                                                                 3.9s
 ⠿ Container ecomapplication_cpostgres_1  Created                                                                                                                 0.0s
 ⠿ Container ecomapplication_ctomcat_1    Created                                                                                                                 0.0s
Attaching to cpostgres_1, ctomcat_1
cpostgres_1  | The files belonging to this database system will be owned by user "postgres".
cpostgres_1  | This user must also own the server process.
cpostgres_1  | 
cpostgres_1  | The database cluster will be initialized with locale "en_US.utf8".
cpostgres_1  | The default database encoding has accordingly been set to "UTF8".
cpostgres_1  | The default text search configuration will be set to "english".
cpostgres_1  | 
cpostgres_1  | Data page checksums are disabled.
cpostgres_1  | 
cpostgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
cpostgres_1  | creating subdirectories ... ok
cpostgres_1  | selecting default max_connections ... 100
cpostgres_1  | selecting default shared_buffers ... 128MB
cpostgres_1  | selecting dynamic shared memory implementation ... posix
cpostgres_1  | creating configuration files ... ok
cpostgres_1  | running bootstrap script ... ok
cpostgres_1  | performing post-bootstrap initialization ... ok
cpostgres_1  | syncing data to disk ... ok
cpostgres_1  | 
cpostgres_1  | Success. You can now start the database server using:
cpostgres_1  | 
cpostgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
cpostgres_1  | 
cpostgres_1  | 
cpostgres_1  | WARNING: enabling "trust" authentication for local connections
cpostgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
cpostgres_1  | --auth-local and --auth-host, the next time you run initdb.
cpostgres_1  | waiting for server to start....2021-06-17 05:54:27.217 UTC [40] LOG:  listening on IPv4 address "127.0.0.1", port 5432
cpostgres_1  | 2021-06-17 05:54:27.217 UTC [40] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
cpostgres_1  | 2021-06-17 05:54:27.217 UTC [40] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
cpostgres_1  | 2021-06-17 05:54:27.221 UTC [40] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
cpostgres_1  | 2021-06-17 05:54:27.235 UTC [41] LOG:  database system was shut down at 2021-06-17 05:54:26 UTC
cpostgres_1  | 2021-06-17 05:54:27.242 UTC [40] LOG:  database system is ready to accept connections
cpostgres_1  |  done
cpostgres_1  | server started
cpostgres_1  | CREATE DATABASE
cpostgres_1  | 
cpostgres_1  | ALTER ROLE
cpostgres_1  | 
cpostgres_1  | 
cpostgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
cpostgres_1  | 
cpostgres_1  | waiting for server to shut down....2021-06-17 05:54:27.715 UTC [40] LOG:  received fast shutdown request
cpostgres_1  | 2021-06-17 05:54:27.718 UTC [40] LOG:  aborting any active transactions
cpostgres_1  | 2021-06-17 05:54:27.719 UTC [40] LOG:  worker process: logical replication launcher (PID 47) exited with exit code 1
cpostgres_1  | 2021-06-17 05:54:27.723 UTC [42] LOG:  shutting down
cpostgres_1  | 2021-06-17 05:54:27.744 UTC [40] LOG:  database system is shut down
cpostgres_1  |  done
cpostgres_1  | server stopped
cpostgres_1  | 
cpostgres_1  | PostgreSQL init process complete; ready for start up.
cpostgres_1  | 
cpostgres_1  | 2021-06-17 05:54:27.828 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
cpostgres_1  | 2021-06-17 05:54:27.828 UTC [1] LOG:  listening on IPv6 address "::", port 5432
cpostgres_1  | 2021-06-17 05:54:27.832 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
cpostgres_1  | 2021-06-17 05:54:27.845 UTC [67] LOG:  database system was shut down at 2021-06-17 05:54:27 UTC
cpostgres_1  | 2021-06-17 05:54:27.852 UTC [1] LOG:  database system is ready to accept connections
ctomcat_1    | Listening for transport dt_socket at address: 18000
ctomcat_1    | 17-Jun-2021 05:54:32.012 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.53
ctomcat_1    | 17-Jun-2021 05:54:32.017 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 29 2018 14:42:45 UTC
ctomcat_1    | 17-Jun-2021 05:54:32.017 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.53.0
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            5.10.25-linuxkit
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_181-8u181-b13-1~deb9u1-b13
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
ctomcat_1    | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
ctomcat_1    | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
ctomcat_1    | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
ctomcat_1    | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
ctomcat_1    | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
ctomcat_1    | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=18000,server=y,suspend=n
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
ctomcat_1    | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.2.
ctomcat_1    | 17-Jun-2021 05:54:32.021 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
ctomcat_1    | 17-Jun-2021 05:54:32.026 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f  25 May 2017)
ctomcat_1    | 17-Jun-2021 05:54:32.198 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
ctomcat_1    | 17-Jun-2021 05:54:32.212 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
ctomcat_1    | 17-Jun-2021 05:54:32.214 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 863 ms
ctomcat_1    | 17-Jun-2021 05:54:32.270 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
ctomcat_1    | 17-Jun-2021 05:54:32.270 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
ctomcat_1    | 17-Jun-2021 05:54:32.320 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/ecom.war
ctomcat_1    | 17-Jun-2021 05:54:33.034 WARNING [localhost-startStop-1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
ctomcat_1    | 17-Jun-2021 05:54:33.037 WARNING [localhost-startStop-1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
ctomcat_1    | 17-Jun-2021 05:54:35.180 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
ctomcat_1    | 17-Jun-2021 05:54:35.450 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/ecom.war has finished in 3,130 ms
ctomcat_1    | 17-Jun-2021 05:54:35.452 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
ctomcat_1    | 17-Jun-2021 05:54:35.651 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 198 ms
ctomcat_1    | 17-Jun-2021 05:54:35.651 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
ctomcat_1    | 17-Jun-2021 05:54:35.682 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 31 ms
ctomcat_1    | 17-Jun-2021 05:54:35.683 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
ctomcat_1    | 17-Jun-2021 05:54:35.701 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 18 ms
ctomcat_1    | 17-Jun-2021 05:54:35.701 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
ctomcat_1    | 17-Jun-2021 05:54:35.727 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 25 ms
ctomcat_1    | 17-Jun-2021 05:54:35.727 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
ctomcat_1    | 17-Jun-2021 05:54:35.744 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 17 ms
ctomcat_1    | 17-Jun-2021 05:54:35.763 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
ctomcat_1    | 17-Jun-2021 05:54:35.788 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
ctomcat_1    | 17-Jun-2021 05:54:35.799 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 3584 ms

调试tomcat容器日志时,不会显示任何错误,说明到达tomcat服务器时出现的问题。因此,调试非常困难。
Postman
端点状态
docker编写文件

version: '2'
services:
  ctomcat:
    build:
      context: .
      dockerfile: ./docker/tomcat/Dockerfile
    ports:
      - "8080:8080"
      - "18000:18000"
    image: itomcat-ecom
    depends_on:
      - cpostgres

  cpostgres:
    build:
      context: "./docker/postgres"
    restart: always
    image: ipostgres-ecom
    ports:
      - 15432:5432

postgres的docker文件

FROM postgres:10.2
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB ecomdb

tomcat的docker文件

FROM tomcat:8.0-jre8
ADD target/ecom.war /usr/local/tomcat/webapps
ENV JPDA_ADDRESS 18000
ENV JPDA_TRANSPORT=dt_socket
EXPOSE 8080
EXPOSE 18000
CMD ["catalina.sh", "jpda", "run"]

应用程序属性

spring.datasource.url=jdbc:postgresql://cpostgres:5432/ecomdb
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.show_sql=true
hivapdat

hivapdat1#

在docker compose文件中,tomcat的拉取映像具有不同的版本,该版本只支持特定的java版本。因此,您可以更改支持java版本11的tomcat映像版本文件(在pom文件中)或更改java版本,如下所述:
在pom文件的“属性”下进行更改-

<properties>
    <java.version>1.8</java.version>
</properties>

终端日志中显示的错误很难调试,因为它根本不显示任何错误,如果无法部署它,它将完全忽略artifactid。
笔记
我们需要确保三件事…
war应该复制到tomcat as-(/usr/local/tomcat/webapps)中的正确目录。
确保您的java版本与您正在使用的tomcat映像匹配。
确保您的sprint引导应用程序类是正确的,并且已经实现了SpringBootServletilizer
完成上述更改后,项目将成功部署,连接到数据库,并访问端点。

相关问题