spring Tomcat服务器未在Docker中的Sping Boot Web应用程序中启动

h79rfbju  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(130)

**问题描述:**我在Docker容器中尝试使用PostgreSQL运行Sping Boot Web应用时遇到困难。主要问题是嵌入式Tomcat服务器未按预期启动。奇怪的是,Docker容器以0的状态码退出,这表明主进程(Sping Boot 应用)已成功结束或意外终止。
故障排除步骤:

我仔细检查了Sping Boot 应用程序生成的日志,以确定任何错误消息或异常,这些消息或异常可能会解释Tomcat无法启动的原因。
容器退出后,我使用docker logs命令检查了它的日志。我的目标是寻找任何可能解释过早终止的异常行为或错误消息。
我重新查看了我的Dockerfile和Docker Compose配置,以确保它们为运行Sping Boot 应用程序进行了适当的设置。这涉及检查依赖项,入口点规范和其他相关配置。
确认我的Sping Boot 应用程序可以成功建立到PostgreSQL数据库的连接是至关重要的。我仔细检查了在我的application.properties中指定的数据库URL、凭据和驱动程序类配置。
为了防止容器过早关闭,我探索了在应用程序中实施健康检查的选项。这涉及到确保应用程序在容器被认为准备就绪之前处于健康状态。
我正在寻求帮助,以了解为什么Tomcat无法在我的Docker容器中启动,以及为什么容器以状态代码0退出。如果有人遇到过类似的问题或对Docker中的Sping Boot 应用程序故障排除有见解,我将非常感谢您的指导。build.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.4'
}
bootJar{
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
apply plugin: 'java'

springBoot {
    mainClass = 'com.example.fcs.FeedbackCollectionSystemApplication'
    buildInfo()
}
jar {
    manifest {
        attributes 'Main-Class': 'com.example.fcs.FeedbackCollectionSystemApplication'
    }

    archiveFileName = 'app.jar'
    
    destinationDirectory.set(file('src/main/docker'))

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-tomcat'
    implementation 'org.postgresql:postgresql'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

字符串

docker-compose文件:

version: '3.8'

services:
  postgres:
    image: postgres:13-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 11111111Aa
      POSTGRES_DB: feedback
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/feedback
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: 11111111Aa

volumes:
  postgres-data:

Dockerfile:

FROM openjdk:19
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE=/*.jar
ADD ${JAR_FILE} app.jar
CMD ["java","-jar","/app.jar"]
#or entrypoint idk

主类:

package com.example.fcs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FeedbackCollectionSystemApplication {

    public static void main(String[] args) {
            SpringApplication.run(FeedbackCollectionSystemApplication.class, args);
    }

}

密码:

app-1       |  :: Spring Boot ::                (v3.2.0)
app-1       |
app-1       | 16:48:21.928 [main] INFO com.example.fcs.FeedbackCollectionSystemApplication -- Starting FeedbackCollectionSystemApplication using Java 19 with PID 1 (/app.jar started by root in /)
app-1       | 16:48:21.941 [main] INFO com.example.fcs.FeedbackCollectionSystemApplication -- No active profile set, falling back to 1 default profile: "default"
app-1       | 16:48:22.942 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate -- Bootstrapping Spring Data JPA repositories in DEFAULT mode.
app-1       | 16:48:23.022 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate -- Finished Spring Data repository scanning in 70 ms. Found 2 JPA repository interfaces.
app-1       | 16:48:23.484 [main] INFO com.zaxxer.hikari.HikariDataSource -- HikariPool-1 - Starting...
app-1       | 16:48:23.722 [main] INFO com.zaxxer.hikari.pool.HikariPool -- HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@4a34e9f
app-1       | 16:48:23.724 [main] INFO com.zaxxer.hikari.HikariDataSource -- HikariPool-1 - Start completed.
app-1       | 16:48:23.778 [main] INFO org.hibernate.jpa.internal.util.LogHelper -- HHH000204: Processing PersistenceUnitInfo [name: default]
app-1       | 16:48:23.851 [main] INFO org.hibernate.Version -- HHH000412: Hibernate ORM core version [WORKING]
app-1       | 16:48:23.900 [main] INFO org.hibernate.cache.internal.RegionFactoryInitiator -- HHH000026: Second-level cache disabled
app-1       | 16:48:24.136 [main] INFO org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo -- No LoadTimeWeaver setup: ignoring JPA class transformer
app-1       | 16:48:25.238 [main] INFO org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator -- HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
app-1       | 16:48:25.243 [main] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean -- Initialized JPA EntityManagerFactory for persistence unit 'default'        
app-1       | 16:48:25.918 [main] INFO com.example.fcs.FeedbackCollectionSystemApplication -- Started FeedbackCollectionSystemApplication in 4.361 seconds (process running for 4.706)
app-1       | 16:48:25.926 [SpringApplicationShutdownHook] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean -- Closing JPA EntityManagerFactory for persistence unit 'default'
app-1       | 16:48:25.930 [SpringApplicationShutdownHook] INFO com.zaxxer.hikari.HikariDataSource -- HikariPool-1 - Shutdown initiated...
app-1       | 16:48:25.936 [SpringApplicationShutdownHook] INFO com.zaxxer.hikari.HikariDataSource -- HikariPool-1 - Shutdown completed.
app-1 exited with code 0


也许我必须运行tomcat作为一个单独的容器,请,帮助!

k10s72fa

k10s72fa1#

如果您没有指定depends_on并进行适当的健康检查,docker-compose将同时启动两个服务。如果Postgres容器启动的时间比应用程序长,应用程序将崩溃。

services:
  postgres:
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 11111111Aa
      POSTGRES_DB: feedback
    healthcheck:
      test: ["CMD-SHELL", "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"]
      interval: 10s
      timeout: 3s
      retries: 3

  app:
    depends_on:
      - postgres

字符串

注意:depends_onhealthcheck至少需要docker-compose V2。

一个快速的解决方法是在你的应用程序中添加一个restart: always,然后它会在崩溃几次后重新启动,直到Postgres容器允许应用程序成功连接它。这个解决方案会污染你的日志,我宁愿使用健康检查。

app:
  restart: always

相关问题