简体   繁体   English

Spring 在 docker 容器中运行的应用程序无法访问

[英]Spring application running in docker container is not reachable

I've built a spring-boot application.我构建了一个 spring-boot 应用程序。 I create a docker image with the following docker file:我使用以下 docker 文件创建一个 docker 图像:

Dockerfile Dockerfile

FROM openjdk:17 as builder
WORKDIR /app
COPY . .
RUN microdnf install findutils
RUN ./gradlew build
COPY build/libs/*.jar app.jar

FROM openjdk:17
COPY --from=builder /app/app.jar .
ENTRYPOINT ["java", "-jar", "/app.jar"]
EXPOSE 8080

I run the application together with a Postgres database我将应用程序与 Postgres 数据库一起运行

docker-compose.yaml docker-compose.yaml

version: "3.8"

services:    
  db:
    image: postgres
    container_name: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: example
    expose:
      - "1999"
    ports:
      - "1999:1999"
    command: -p 1999

  api:
    image: api
    depends_on:
      - db
    ports:
      - 8080:8080
    environment:
      - SPRING_PROFILES_ACTIVE=docker

networks:
  default:

This works perfectly on windows, but as soon as I move this to my linux (Zorin OS) machine, I'm not able to reach the application on localhost:8080 , even though the application in the container is active.这在 windows 上完美运行,但是一旦我将其移动到我的 linux (Zorin OS) 机器上,我就无法访问localhost:8080上的应用程序,即使容器中的应用程序处于活动状态。

docker container logs docker 容器日志

[main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
[main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2381 ms
[main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
[main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.5.Final
[main] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
[ main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
[main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@9ec531
[main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
[main] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
[main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
[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
[main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@54755dd9, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@f1f7db2, org.springframework.security.web.context.SecurityContextHolderFilter@4d3c6593, org.springframework.security.web.header.HeaderWriterFilter@590adb41, org.springframework.security.web.authentication.logout.LogoutFilter@633cc6b5, com.voidhub.api.configuration.jwt.JwtUsernameAndPasswordAuthenticationFilter@4462efe1, com.voidhub.api.configuration.jwt.JwtTokenVerifier@7c3e4b1a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@516462cc, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3456558, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2db4ad1, org.springframework.security.web.session.SessionManagementFilter@43bf5397, org.springframework.security.web.access.ExceptionTranslationFilter@68ed3f30, org.springframework.security.web.access.intercept.AuthorizationFilter@28ee7bee]
[main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
[main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
[main] com.voidhub.api.ApiApplication           : Started ApiApplication in 7.341 seconds (process running for 7.904)

docker ps docker 秒

d1e2d34d8dbd   api   "java -jar /app.jar"     6 seconds ago   Up 5 seconds          0.0.0.0:8080->8080/tcp, :::8080->8080/tcp                                                                 voidhub-backend-1
251065ab7afa   postgres                      "docker-entrypoint.s…"   2 hours ago     Up 5 seconds          0.0.0.0:1999->1999/tcp, :::1999->1999/tcp, 5432/tcp                                                       postgres

running curl localhost:8080 results in运行curl localhost:8080结果

curl: (56) Recv failure: Connection reset by peer

however, running netstat -aon | grep 8080但是,运行netstat -aon | grep 8080 netstat -aon | grep 8080 results in netstat -aon | grep 8080结果

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp6       0      0 :::8080                 :::*                    LISTEN      off (0.00/0/0)

Connection Reset to a Docker container usually indicates that you've defined a port mapping for the container that does not point to an application. Connection Reset to a Docker container 通常表示您为不指向应用程序的容器定义了端口映射。

Did you define server.address=localhost in your application.properties ?您是否在application.properties中定义了server.address=localhost If so, try to remove it or replace it with 0.0.0.0如果是,请尝试将其删除或替换为0.0.0.0

Looks like i did something wrong regarding docker.networking.看起来我在 docker.networking 方面做错了什么。 this is what the new compose file looks like这是新的撰写文件的样子

services:    
  db:
    image: postgres
    container_name: postgres
    network-mode: host
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: example
    expose:
      - "1999"
    ports:
      - "1999:1999"
    command: -p 1999

  api:
    image: api
    network-mode: host
    ports:
      - 8080:8080
    environment:
      - SPRING_PROFILES_ACTIVE=docker

notice how i added a.network mode to both services.请注意我是如何为这两种服务添加 a.network 模式的。

You also need to "expose" the port 8080 from within your docker-compose file.您还需要从 docker-compose 文件中“公开”端口 8080。

api:
  expose:
    - "8080"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将pdf作为在docker容器中运行的spring应用程序的静态资源提供 - Serve pdf as static resource from spring application running in docker container 连接被拒绝:访问在Docker容器中运行的Spring Boot应用程序 - Connection refused: accessing a spring boot application running in docker container Docker-compose - 为运行 spring boot 独立应用程序的 docker 容器提供基于 XML 的配置 - Docker-compose - Providing XML based configuration to docker container running spring boot standalone application 在docker容器中重新部署spring-boot应用程序? - Redeploy spring-boot application in docker container? 无法加载 Spring 在 Docker 中运行的引导应用程序 - Unable to load Spring Boot application running in Docker 从 docker 容器内运行的应用程序连接到本地运行的数据库 - Connect to database running in local from application running inside docker container Java 应用程序如何知道它在 Docker 容器中运行 - How does Java application know it is running within a Docker container 使用Java应用程序和Web服务器运行Docker容器不起作用 - running a Docker container with Java application and a webserver doesnt work 如何同步在docker容器上运行的java应用程序的时间? - How to sync the time of a java application running on docker container? 如何在 docker 容器上的 RestTemplate url 中使用 spring.application.name ? - How to use spring.application.name in RestTemplate url on a docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM