skip to Main Content

I just got a new PC last week. So I setup my working environment as usual in Windows 10 with the latest Windows Docker Desktop. Then created a very simple spring boot REST service just to say hello, created the image with Spring boot Buildpacks 3 days ago, it worked fine with port mapping “docker run -p 8090:8080 davy/myapp”. This image is working well even today: I can access my application by “http://localhost:8090/sayHello” even today.

the working image

So, I started to build my real application and completed some functionalities. I wanted to test my app and created a new image by using spring boot Buildpacks.

Now I got a big problem: I cannot not access the application running in the container by port mapping with port mapping “docker run -p 8090:8080 davy/myapp” any more by “http://localhost:8090/sayHello”. It got an error page said "localhost did not send any data"

cannot send data image

Then I got my container IP by “docker inspect -f ‘{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ 548e29f46ca7”, which displayed as “172.17.0.2/”. So I tried http://172.17.0.2:8090/sayHello. Now I got a timeout after waiting for some time I got "172.17.0.2 took too long to respond":

timeout image

I did not see any difference in the ports binding: both are 0.0.0.0:8090->8080/tcp

port binding for 2 images

I re-built the image several times by using Spring boot Buildpacks, 1 time Dockerfile and docker-compose.yml, and I cannot make the container like the old container any more.

I also tried “docker run -p 8088:8080 davyhu/myapp -m http.server –bind 0.0.0.0”, but got the same result: cannot access app by localhost, and IP timeout.

Thanks in advance for the helps!


Here are some more information:

config in pom.xml for buildpacks (no change for both versions in the pom.xml):

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <image>
                    <name>davyhu/${project.artifactId}</name>
                </image>
            </configuration>
        </plugin>
    </plugins>
</build>

PDFController.java

public class PdfGenerationController {

private static final Logger logger = LoggerFactory.getLogger(PdfGenerationController.class);

private static final SimpleDateFormat DateFormatterIn1 = new SimpleDateFormat("yyyy-MM-dd");
//private static final SimpleDateFormat DateFormatterIn2 = new SimpleDateFormat("dd/MM/yyyy");
private static final SimpleDateFormat DateFormatterOut = new SimpleDateFormat("dd/MM/yyyy");
private static final SimpleDateFormat DateFormatterIn2 = new SimpleDateFormat("yyyy-MM-dd");
//private static final SimpleDateFormat DateFormatterOut = new SimpleDateFormat("yyyy-MM-dd");

@Autowired
private ResourceBundleMessageSource source;
@Value("${pdf.title}")
private static String pdfTitle;
@Value("${pdf.footerText1}")
private static String pdfFooterText1;

@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/getPdf")
public ResponseEntity<byte[]> getPDF(
        @RequestHeader(name = "Accept-Language", required = true) final Locale locale, 
        @RequestBody String jsonInput) {
    logger.info("myCustomerDetails() method started");
    logger.info(jsonInput);
    logger.info("locale = {}", locale);
    
    JSONObject data = new JSONObject(jsonInput);

    byte[] pdfFile = null;
    ResponseEntity<byte[]> response = null;
    HttpHeaders headers = new HttpHeaders();
    try {
        pdfFile = new PdfGenertor().generatePDF(data,locale);
    }catch (Exception e) {
        e.printStackTrace();
        response = new ResponseEntity<>(null, headers, HttpStatus.METHOD_FAILURE);
    }

    headers.setContentType(MediaType.APPLICATION_PDF);
    String filename = "fro_soa_form.pdf";
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    response = new ResponseEntity<>(pdfFile, headers, HttpStatus.OK);
    return response;
}

private String formatDate(SimpleDateFormat format, String str) {
    try {
        Date date = format.parse(str);
        return DateFormatterOut.format(date);
    } catch (Exception e) {
        return "";
    }
}

@GetMapping("/sayHello")
public String sayHello() {
    return "Hello";
}

}

It the code worked fine in eclipse with postman (PDF displayed with Jason input and header accepted language).

first and second image are all build with "mvn spring-boot:build-image".

If anything I need to post, please let mw know.

Thanks!

Dockerfile: 
FROM openjdk:11-slim as build
MAINTAINER xxxx.ca
COPY target/fro_soa_backend-0.0.1-SNAPSHOT.jar fro_soa_backend-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java","-jar","/fro_soa_backend-0.0.1-SNAPSHOT.jar"]

2

Answers


  1. Chosen as BEST ANSWER

    I cannot figure it out what's wrong. So I just created a new workspace from scratch, import the code, rebuilt the application into Docker with Dockerfile, then the Port mapping get working again.

    Now my front Angular and backend Spring boot Apps can communicate on localhost.


  2. Well just to tell you that 172.17..0.2 is the ip container so it can’t be reached out of the container, it’s created from the container to get access from other services or micro-services.

    It seems that your app config isn’t proprely display to this port, means that your application isn’t on 8080 port that’s why it gets you empty response, when you added your fonctionalities, you need to use your Dockerfile and expose your application in 8080.

    FROM  <image_name>
    EXPOSE   8080
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search