Running a springboot application in local works fine and I am able to access all the apis, but the docker image running is giving 404 error

Running a springboot application in local works fine and I am able to access all the apis, but the docker image of same codebase running is giving 404 error for every api, not even /actuator/mappings api

Dockerfile:

# Use a base image with Java 21 pre-installed
FROM openjdk:21

# Copy the application's jar file into the container
COPY target/PeerMessenger-1.0-SNAPSHOT.jar /app/app.jar

# Expose port 8080
EXPOSE 8080

# Run the application
CMD ["java", "-jar", "/app/app.jar"]

docker-compose.yml:

version: '3'
services:
  peer-to-peer-messenger:
    image: peer-to-peer-messenger-image
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/db_messenger
      - SPRING_DATASOURCE_USERNAME=user
      - SPRING_DATASOURCE_PASSWORD=your_password

  db:
    image: postgres
    environment:
      - POSTGRES_DB=db_messenger
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=your_password
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/db_messenger
spring.datasource.username=user
spring.datasource.password=your_password
server.port=8080

logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.boot=DEBUG

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

management.endpoints.web.exposure.include=mappings

driver class:

package com.peermessenger;

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

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

On hitting the following API:

curl --location 'http://localhost:8080/users/all' \
--header 'Content-Type: application/json' \
--header 'Cookie: JSESSIONID=799333C6129227D82E0E287A831ADFFD' \
--data ''

It shows logs:

 c.p.PeerMessengerApplication             : Started PeerMessengerApplication in 1.83 seconds (JVM running for 2.036)
peer-to-peer-messenger-1  | 2023-12-26 20:15:48.500 DEBUG 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
peer-to-peer-messenger-1  | 2023-12-26 20:15:48.500 DEBUG 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.029  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.029  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.029 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.029 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected AcceptHeaderLocaleResolver
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.029 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected FixedThemeResolver
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.030 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@4f63e3c7
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.030 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.support.SessionFlashMapManager@6892cc6f
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.031 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails="false": request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.031  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.038 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/users/all", parameters={}
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.044 DEBUG 1 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.045 DEBUG 1 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.047 DEBUG 1 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.047 DEBUG 1 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.048 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.050 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.051 DEBUG 1 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.051 DEBUG 1 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.068 DEBUG 1 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.068 DEBUG 1 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [{timestamp=Tue Dec 26 20:15:53 UTC 2023, status=404, error=Not Found, path=/users/all}]
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.100 DEBUG 1 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
peer-to-peer-messenger-1  | 2023-12-26 20:15:53.100 DEBUG 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Described about the issue above that the code is running fine and all the endpoints are working fine in local setup but in docker run it isn’t able to find any endpoints and show 404 Not Found error for every apis.
Even for actuator/mappings api too where we see all the endpoints.

  • 1

    Did you tried running the jar locally or through the IDE? the jar might not be correctly built.

    – 

  • 1

    True it worked, thanks @LMC Actually the jar file wasn’t updated so rebuilt the application by running mvn clean package and it updated the jar file.

    – 




Leave a Comment