0

I have the springdoc in my application that generates swagger using the default url http://localhost:8090/swagger-ui/index.html If I run the application without docker it works fine, but when using docker it doesn't

Here my dockerfile

FROM openjdk:17

COPY target/subscription-manager-0.0.1-SNAPSHOT.jar subscription-manager.jar



ENTRYPOINT ["java", "-jar", "subscription-manager.jar"]

The docker-compose.yml

services:
  subscription-manager_app:
    container_name: subscription-manager_app
    image: hizam/subscription-manager_app:1.0.0
    build: .
    ports:
      - '8090:8080'
    environment:
      DATABASE_URL: jdbc:postgresql://db:5432/subscription-manager
      DATABASE_USERNAME: postgres
      DATABASE_PASSWORD: 1
    depends_on:
      - db
  db:
    container_name: db
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 1
      POSTGRES_DB: subscription-manager
    ports:
      - '5333:5432'
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata: { }

Here Iam using securityfilterchain which gives authorization to access swagger as shown in this code

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {

    private final UserService userService;
    private final JwtAuthenticationFilter jwtAuthenticationFilter;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception
    {
        http.csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(authorize->
                authorize.requestMatchers("/swagger-ui/**","/swagger-resources/*","/v3/api-docs/**")
                        .permitAll()
                        .requestMatchers("/api/v1/auth/register","/api/v1/auth/login")
                        .permitAll()
                        .anyRequest().authenticated())
                .httpBasic(Customizer.withDefaults())
                .sessionManagement(session->session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authenticationProvider(authenticationProvider())
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userService.userDetailsService());
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;

    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception
    {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
9
  • Since the Spring Boot app runs on port 8080, make sure to add EXPOSE 8080 in the Dockerfile for correct port mapping in docker-compose (e.g., 8090:8080) Commented Jun 11 at 17:15
  • I tried and it didn't work @sriram_aleti Commented Jun 11 at 18:02
  • When you say "it doesn't work", what specifically doesn't work? If the problem is fetching the Swagger/OpenAPI specification, do other endpoints work? Can you edit the question to include details on what you're trying to call and what exact error you're getting? Commented Jun 11 at 18:21
  • Yes when entering the url localhost:8090/swagger-ui/index.html This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE I checked the logs of the container subscription-manager_app shows no error. All other endpoints that are created in the restcontroller are working ex:localhost:8090/api/v1/auth/register. @DavidMaze This happens only when the docker is running. Commented Jun 11 at 20:28
  • If you can reach other endpoints, this suggests that the container is created and the Docker-related wiring is correct. What code or configuration sets up the Swagger-UI endpoint? Commented Jun 11 at 22:12

1 Answer 1

0

I have the springdoc in my application that generates swagger using the default url http://localhost:8090/swagger-ui/index.html

If your app is listening on port 8090 inside the container then you need to map the host port to 8090 in your compose file e.g.:

ports:
  - '8090:8090'
Sign up to request clarification or add additional context in comments.

4 Comments

It wont work because tomcat is running on server 8080. Note : All the endpoints that are created in the restcontroller are working ex:localhost:8090/api/v1/auth/register. The only problem is localhost:8090/swagger-ui/index.html is not working.
so in the container tomcat is running on 8080 and swagger UI is running on 8090? So map both - 8080:8080 - 8090:8090 Both services should then be available on the respective ports on the host.
same issue @andycaine
so if you run outside of docker (java -jar subscription-manager.jar) which ports does the app expose? Then if you run the container directly (docker run -p <host-port>:<container-port> <image>) you should be able to expose both those ports (with multiple -p flags).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.