skip to Main Content

I’ve built a spring-boot application. I create a docker image with the following docker file:

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

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.

docker container logs

[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

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: (56) Recv failure: Connection reset by peer

however, running netstat -aon | grep 8080 results in

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)

3

Answers


  1. Chosen as BEST ANSWER

    Looks like i did something wrong regarding 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.


  2. You also need to "expose" the port 8080 from within your docker-compose file.

    api:
      expose:
        - "8080"
    
    Login or Signup to reply.
  3. 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.

    Did you define server.address=localhost in your application.properties?
    If so, try to remove it or replace it with 0.0.0.0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search