仅在 8080 上运行的 Spring Boot docker?
Posted
技术标签:
【中文标题】仅在 8080 上运行的 Spring Boot docker?【英文标题】:Spring boot docker running on 8080 only? 【发布时间】:2021-05-17 10:53:55 【问题描述】:我已经在 docker 上部署了我的 spring boot 应用程序,但是当我尝试运行它时,它只在 8080 端口上运行,而不是任何其他端口,即来自日志的 3000 我能够推断它与内部 tomcat 有关监听 8080 端口。谁能告诉我里面发生了什么?
以下是我在 8080 上运行时的日志
2021-02-14 05:53:36.156 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : Starting EbiProjectJavaApplication v0.0.1-SNAPSHOT using Java 1.8.0_212 on 8a710deb6884 with PID 1 (/ebiProjectJava.jar started by root in /)
2021-02-14 05:53:36.160 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : No active profile set, falling back to default profiles: default
2021-02-14 05:53:37.426 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-02-14 05:53:37.663 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 224 ms. Found 1 JPA repository interfaces.
2021-02-14 05:53:38.750 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-02-14 05:53:38.770 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-02-14 05:53:38.771 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-14 05:53:38.902 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-02-14 05:53:38.903 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2621 ms
2021-02-14 05:53:39.139 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-02-14 05:53:39.648 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-02-14 05:53:39.732 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-02-14 05:53:39.817 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.27.Final
2021-02-14 05:53:39.990 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.1.2.Final
2021-02-14 05:53:40.149 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-02-14 05:53:40.898 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-02-14 05:53:40.909 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-02-14 05:53:41.299 WARN 1 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-02-14 05:53:41.595 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-14 05:53:42.091 INFO 1 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7b94089b, org.springframework.security.web.context.SecurityContextPersistenceFilter@ee86bcb, org.springframework.security.web.header.HeaderWriterFilter@1f010bf0, org.springframework.security.web.authentication.logout.LogoutFilter@bcef303, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@33308786, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7f132176, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6ed3f258, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7ca20101, org.springframework.security.web.session.SessionManagementFilter@10cf09e8, org.springframework.security.web.access.ExceptionTranslationFilter@19b93fa8, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@71812481]
2021-02-14 05:53:42.410 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-14 05:53:42.424 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : Started EbiProjectJavaApplication in 6.864 seconds (JVM running for 7.751)
2021-02-14 05:54:13.163 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-02-14 05:54:13.164 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-02-14 05:54:13.167 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
以下是我尝试在 3000 上运行时的日志
2021-02-14 05:50:58.993 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : Starting EbiProjectJavaApplication v0.0.1-SNAPSHOT using Java 1.8.0_212 on b0f62c63fde2 with PID 1 (/ebiProjectJava.jar started by root in /)
2021-02-14 05:50:58.997 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : No active profile set, falling back to default profiles: default
2021-02-14 05:51:00.304 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-02-14 05:51:00.566 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 243 ms. Found 1 JPA repository interfaces.
2021-02-14 05:51:01.714 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-02-14 05:51:01.734 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-02-14 05:51:01.734 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-14 05:51:01.816 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-02-14 05:51:01.817 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2706 ms
2021-02-14 05:51:02.060 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-02-14 05:51:02.534 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-02-14 05:51:02.632 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-02-14 05:51:02.723 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.27.Final
2021-02-14 05:51:02.909 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.1.2.Final
2021-02-14 05:51:03.163 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-02-14 05:51:04.045 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-02-14 05:51:04.055 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-02-14 05:51:04.480 WARN 1 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-02-14 05:51:04.799 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-14 05:51:05.330 INFO 1 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7ca20101, org.springframework.security.web.context.SecurityContextPersistenceFilter@177bea38, org.springframework.security.web.header.HeaderWriterFilter@40db2a24, org.springframework.security.web.authentication.logout.LogoutFilter@41709512, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@4f9a2c08, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6bca7e0d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@8ad6665, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@47f9738, org.springframework.security.web.session.SessionManagementFilter@1921ad94, org.springframework.security.web.access.ExceptionTranslationFilter@7e6ef134, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@16ce702d]
2021-02-14 05:51:05.709 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-14 05:51:05.726 INFO 1 --- [ main] com.ebi.uk.EbiProjectJavaApplication : Started EbiProjectJavaApplication in 7.436 seconds (JVM running for 8.28)
下面是我的 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- spring security test -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
下面是我要运行的命令
docker run -p 3000:3000 ebiproject
【问题讨论】:
【参考方案1】:我认为您在这里遗漏了重要的概念。 8080
是 Spring Boot 应用程序的默认端口。但是,在您的 docker 网络配置中,您映射了 3000
-> 3000
这是错误的。
相反,您应该进行如下配置:
docker run -p 3000:8080 ebiproject
【讨论】:
我是从可用的教程中做到的,你能解释一下区别 您的容器化 spring-boot 应用程序运行在 docker 容器之上。并且您的 docker 容器在 docker 主机(可能是您的机器)上运行。如果不指定打开哪些端口,docker 默认禁用所有访问。这意味着您的应用程序将在容器的 8080 端口上提供服务。但是,同样,由于您没有公开容器的 8080 端口,它将无法访问。-p 3000:8080
使您能够将 docker 主机的 3000 端口映射到容器(spring-boot 应用程序运行的地方)的 8080 端口。以上是关于仅在 8080 上运行的 Spring Boot docker?的主要内容,如果未能解决你的问题,请参考以下文章
当配置设置为另一个时,Idea + Spring Boot + Tomcat 在端口 8080 上启动
使用 nginx 代理的带有 keycloak 的 Spring Boot 仅在 redirect_uri localhost 时才有效