Docker部署SpringBoot启动失败:嵌入式Tomcat无法启动求助
环境:EC2(Ubuntu)、Docker、Nginx(通过Certbot配置HTTPS)、SpringBoot、JPA、SpringSecurity
已排除JRE/JDK问题,核心错误为:Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
底层原因:java.net.BindException: Cannot assign requested address
以下是完整错误日志、Dockerfile及Application.properties配置,请求协助排查。
排查方案
1. 问题根源
java.net.BindException: Cannot assign requested address 说明Tomcat无法绑定到指定的IP地址。Docker容器拥有独立的网络栈,你配置的EC2公网IP(*myserver*)在容器内部无法识别和绑定,导致Tomcat启动失败。
2. 修复步骤
修改Application.properties配置:
将server.address=*myserver*注释掉,改为监听容器内所有网卡地址0.0.0.0,这样容器内的Tomcat才能正常启动并接收外部请求转发。修改后配置如下:# server.address=*myserver* # 注释原配置 server.address=0.0.0.0 server.port=8080确认Docker端口映射:
启动容器时必须将容器端口映射到EC2主机端口,例如:docker run -d -p 8080:8080 your-springboot-image此命令将EC2主机的8080端口转发到容器内的8080端口,结合Nginx反向代理即可实现HTTPS访问。
额外检查:
- 确认EC2安全组已开放80/443端口(供Nginx使用),若需直接访问应用则同时开放8080端口;
- 进入容器检查是否有其他进程占用8080端口:
docker exec -it <容器ID> /bin/bash netstat -tulpn | grep 8080
错误日志
2022-08-07 07:48:41.045 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server 2022-08-07 07:48:41.089 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2022-08-07 07:48:41.091 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2022-08-07 07:48:41.107 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2022-08-07 07:48:41.109 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2022-08-07 07:48:41.123 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-08-07 07:48:41.149 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.22.jar!/:5.3.22] at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_111] at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.22.jar!/:5.3.22] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) [spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) [spring-boot-2.7.2.jar!/:2.7.2] at com.jangbo.BackendApplication.main(BackendApplication.java:10) [classes!/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [app.jar:na] at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) [app.jar:na] at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [app.jar:na] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) [app.jar:na] Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:229) ~[spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:43) ~[spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.22.jar!/:5.3.22] ... 22 common frames omitted Caused by: java.lang.IllegalArgumentException: standardService.connector.startFailed at org.apache.catalina.core.StandardService.addConnector(StandardService.java:238) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:282) ~[spring-boot-2.7.2.jar!/:2.7.2] at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:213) ~[spring-boot-2.7.2.jar!/:2.7.2] ... 24 common frames omitted Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed at org.apache.catalina.connector.Connector.startInternal(Connector.java:1077) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.catalina.core.StandardService.addConnector(StandardService.java:234) ~[tomcat-embed-core-9.0.65.jar!/:na] ... 26 common frames omitted Caused by: java.net.BindException: Cannot assign requested address at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_111] at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_111] at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_111] at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_111] at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:275) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:230) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1227) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1313) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:614) ~[tomcat-embed-core-9.0.65.jar!/:na] at org.apache.catalina.connector.Connector.startInternal(Connector.java:1074) ~[tomcat-embed-core-9.0.65.jar!/:na] ... 28 common frames omitted
Dockerfile
# Start with a base image containing Java runtime FROM java:8 ARG JAR_FILE=build/libs/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar", "/app.jar"] EXPOSE 8080
Application.properties
#it will be set build date by gradle. if this value is @build.date@, front-end is development mode build.date=@build.date@ server.port=8080 server.address=*myserver* #server.address=localhost server.servlet.contextPath=/ # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly. server.servlet.encoding.charset=UTF-8 # Enable http encoding support. server.servlet.encoding.enabled=true # Force the encoding to the configured charset on HTTP requests and responses. server.servlet.encoding.force=true # Swagger springfox.documentation.swagger.use-model-v3=false spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER #database #API ?? ? sql?? ?? spring.jpa.show-sql=true #MySQL ?? ?? spring.datasource.url=jdbc:mysql://*myserver*:3306/jangbo?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true #spring.datasource.url=jdbc:mysql://localhost:3306/*dbnname*?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.hikari.username=root spring.datasource.hikari.password=*password* #MySQL ?? ?? spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=create spring.jpa.hibernate.use-new-id-generator-mappings= false spring.jpa.properties.hibernate.show_sql=true spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy spring.mustache.suffix: .html #logging #logging.level.root=INFO #logging.level.com.samsung.security=DEBUG #logging.level.org.springframework.web=DEBUG #logging.level.org.apache.tiles=INFO #logging.level.org.springframework.boot=DEBUG #logging.level.org.springframework.security=DEBUG # spring.devtools.livereload.enabled=true #logging.level.org.hibernate.SQL=debug #Redis spring.cache.type=redis spring.redis.host=*myserver* #spring.redis.host=localhost spring.redis.port=6379 # JWT Secret Key spring.jwt.secret=*contents of key*
内容的提问来源于stack exchange,提问作者Seol Heo

