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

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

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

  1. <modelVersion>4.0.0</modelVersion>
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.5.0</version>
  6. <relativePath/> <!-- lookup parent from repository -->
  7. </parent>
  8. <groupId>com.demo</groupId>
  9. <artifactId>EcomApplication</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. <name>EcomApplication</name>
  12. <packaging>war</packaging>
  13. <description>Demo project for Spring Boot</description>
  14. <properties>
  15. <java.version>11</java.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-jpa</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.projectlombok</groupId>
  32. <artifactId>lombok</artifactId>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.postgresql</groupId>
  42. <artifactId>postgresql</artifactId>
  43. <scope>runtime</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>junit</groupId>
  47. <artifactId>junit</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. </dependencies>
  51. <build>
  52. <finalName>ecom</finalName>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.apache.maven.plugins</groupId>
  56. <artifactId>maven-surefire-plugin</artifactId>
  57. <version>2.4.2</version>
  58. <configuration>
  59. <skipTests>true</skipTests>
  60. </configuration>
  61. </plugin>
  62. </plugins>
  63. </build>

终端日志:

  1. sowbharnika@SowbharnikasMBP EcomApplication % docker compose up --build
  2. [+] Building 4.7s (11/11) FINISHED
  3. => [itomcat-ecom internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 217B 0.0s
  5. => [ipostgres-ecom internal] load build definition from Dockerfile 0.0s
  6. => => transferring dockerfile: 137B 0.0s
  7. => [itomcat-ecom internal] load .dockerignore 0.0s
  8. => => transferring context: 2B 0.0s
  9. => [ipostgres-ecom internal] load .dockerignore 0.0s
  10. => => transferring context: 2B 0.0s
  11. => [itomcat-ecom internal] load metadata for docker.io/library/tomcat:8.0-jre8 3.1s
  12. => [ipostgres-ecom internal] load metadata for docker.io/library/postgres:10.2 3.1s
  13. => CACHED [ipostgres-ecom 1/1] FROM docker.io/library/postgres:10.2@sha256:7361bae1fbf5642099663d1f02dc949cabde1f86727bf8ff00d0a8806640a617 0.0s
  14. => [itomcat-ecom] exporting to image 0.2s
  15. => => exporting layers 0.2s
  16. => => writing image sha256:388cf029752f4cac431dfb8eb41170dbd547bdb25e2542222d85355af59d96ed 0.0s
  17. => => naming to docker.io/library/ipostgres-ecom 0.0s
  18. => => writing image sha256:619f0674184633b5b1c4c81d08fc714b42625b547bd341486f321e706c75f744 0.0s
  19. => => naming to docker.io/library/itomcat-ecom 0.0s
  20. => [itomcat-ecom internal] load build context 1.0s
  21. => => transferring context: 37.56MB 1.0s
  22. => CACHED [itomcat-ecom 1/2] FROM docker.io/library/tomcat:8.0-jre8@sha256:268403c3fa09afd9310ced7e83ac021927dca0f04a394a80a0b0220f62e056ed 0.0s
  23. => [itomcat-ecom 2/2] ADD target/ecom.war /usr/local/tomcat/webapps 0.1s
  24. Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
  25. [+] Running 3/2
  26. Network ecomapplication_default Created 3.9s
  27. Container ecomapplication_cpostgres_1 Created 0.0s
  28. Container ecomapplication_ctomcat_1 Created 0.0s
  29. Attaching to cpostgres_1, ctomcat_1
  30. cpostgres_1 | The files belonging to this database system will be owned by user "postgres".
  31. cpostgres_1 | This user must also own the server process.
  32. cpostgres_1 |
  33. cpostgres_1 | The database cluster will be initialized with locale "en_US.utf8".
  34. cpostgres_1 | The default database encoding has accordingly been set to "UTF8".
  35. cpostgres_1 | The default text search configuration will be set to "english".
  36. cpostgres_1 |
  37. cpostgres_1 | Data page checksums are disabled.
  38. cpostgres_1 |
  39. cpostgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
  40. cpostgres_1 | creating subdirectories ... ok
  41. cpostgres_1 | selecting default max_connections ... 100
  42. cpostgres_1 | selecting default shared_buffers ... 128MB
  43. cpostgres_1 | selecting dynamic shared memory implementation ... posix
  44. cpostgres_1 | creating configuration files ... ok
  45. cpostgres_1 | running bootstrap script ... ok
  46. cpostgres_1 | performing post-bootstrap initialization ... ok
  47. cpostgres_1 | syncing data to disk ... ok
  48. cpostgres_1 |
  49. cpostgres_1 | Success. You can now start the database server using:
  50. cpostgres_1 |
  51. cpostgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
  52. cpostgres_1 |
  53. cpostgres_1 |
  54. cpostgres_1 | WARNING: enabling "trust" authentication for local connections
  55. cpostgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
  56. cpostgres_1 | --auth-local and --auth-host, the next time you run initdb.
  57. 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
  58. cpostgres_1 | 2021-06-17 05:54:27.217 UTC [40] LOG: could not bind IPv6 address "::1": Cannot assign requested address
  59. 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.
  60. cpostgres_1 | 2021-06-17 05:54:27.221 UTC [40] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
  61. 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
  62. cpostgres_1 | 2021-06-17 05:54:27.242 UTC [40] LOG: database system is ready to accept connections
  63. cpostgres_1 | done
  64. cpostgres_1 | server started
  65. cpostgres_1 | CREATE DATABASE
  66. cpostgres_1 |
  67. cpostgres_1 | ALTER ROLE
  68. cpostgres_1 |
  69. cpostgres_1 |
  70. cpostgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
  71. cpostgres_1 |
  72. cpostgres_1 | waiting for server to shut down....2021-06-17 05:54:27.715 UTC [40] LOG: received fast shutdown request
  73. cpostgres_1 | 2021-06-17 05:54:27.718 UTC [40] LOG: aborting any active transactions
  74. cpostgres_1 | 2021-06-17 05:54:27.719 UTC [40] LOG: worker process: logical replication launcher (PID 47) exited with exit code 1
  75. cpostgres_1 | 2021-06-17 05:54:27.723 UTC [42] LOG: shutting down
  76. cpostgres_1 | 2021-06-17 05:54:27.744 UTC [40] LOG: database system is shut down
  77. cpostgres_1 | done
  78. cpostgres_1 | server stopped
  79. cpostgres_1 |
  80. cpostgres_1 | PostgreSQL init process complete; ready for start up.
  81. cpostgres_1 |
  82. cpostgres_1 | 2021-06-17 05:54:27.828 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
  83. cpostgres_1 | 2021-06-17 05:54:27.828 UTC [1] LOG: listening on IPv6 address "::", port 5432
  84. cpostgres_1 | 2021-06-17 05:54:27.832 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
  85. 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
  86. cpostgres_1 | 2021-06-17 05:54:27.852 UTC [1] LOG: database system is ready to accept connections
  87. ctomcat_1 | Listening for transport dt_socket at address: 18000
  88. ctomcat_1 | 17-Jun-2021 05:54:32.012 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
  89. 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
  90. ctomcat_1 | 17-Jun-2021 05:54:32.017 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0
  91. ctomcat_1 | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
  92. ctomcat_1 | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.10.25-linuxkit
  93. ctomcat_1 | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
  94. 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
  95. 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
  96. ctomcat_1 | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
  97. ctomcat_1 | 17-Jun-2021 05:54:32.018 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
  98. ctomcat_1 | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
  99. 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
  100. 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
  101. ctomcat_1 | 17-Jun-2021 05:54:32.019 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
  102. 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
  103. 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
  104. ctomcat_1 | 17-Jun-2021 05:54:32.020 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
  105. 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
  106. 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
  107. 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
  108. 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.
  109. 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].
  110. 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)
  111. ctomcat_1 | 17-Jun-2021 05:54:32.198 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
  112. ctomcat_1 | 17-Jun-2021 05:54:32.212 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
  113. ctomcat_1 | 17-Jun-2021 05:54:32.214 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 863 ms
  114. ctomcat_1 | 17-Jun-2021 05:54:32.270 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
  115. 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
  116. 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
  117. 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.
  118. 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.
  119. 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.
  120. 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
  121. 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
  122. 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
  123. 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
  124. 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
  125. 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
  126. 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
  127. 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
  128. 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
  129. 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
  130. 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
  131. ctomcat_1 | 17-Jun-2021 05:54:35.763 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
  132. ctomcat_1 | 17-Jun-2021 05:54:35.788 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
  133. 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编写文件

  1. version: '2'
  2. services:
  3. ctomcat:
  4. build:
  5. context: .
  6. dockerfile: ./docker/tomcat/Dockerfile
  7. ports:
  8. - "8080:8080"
  9. - "18000:18000"
  10. image: itomcat-ecom
  11. depends_on:
  12. - cpostgres
  13. cpostgres:
  14. build:
  15. context: "./docker/postgres"
  16. restart: always
  17. image: ipostgres-ecom
  18. ports:
  19. - 15432:5432

postgres的docker文件

  1. FROM postgres:10.2
  2. ENV POSTGRES_USER postgres
  3. ENV POSTGRES_PASSWORD password
  4. ENV POSTGRES_DB ecomdb

tomcat的docker文件

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

应用程序属性

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

hivapdat1#

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

  1. <properties>
  2. <java.version>1.8</java.version>
  3. </properties>

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

相关问题