'OkHttpClient is not working inside Docker

I'm building server inside docker and trying to use OkHttpClient to get data but it seems not working.

When I test with postman, there is nothing logged in server log, but it shows 502 Bad gateway error on postman

Nginx 502 Bad gateway error.

My code is as below.

public String checkConnection(){
        OkHttpClient client = new OkHttpClient.Builder().build();
        log.debug("Build OkHttpClient");
        Request request = new Request.Builder()
                .url("https://example.com")
                .build();
        log.debug("Build Request");
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

Update: Below code didn't work too.

public String checkRetro(){
        String url = "https://example.com";

        Request.Builder builder = new Request.Builder();
        builder = builder.url(url);
        log.debug("add url = " + builder);

        Request request = builder.build();
        log.debug("Build Request");

        OkHttpClient client = new OkHttpClient.Builder().proxy(Proxy.NO_PROXY).build();
        log.debug("Build OkHttpClient");

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

My environment:

Docker

Nginx

Java 1.8

OkHttp-3.9.0

retrofit-2.1.0



Solution 1:[1]

Is the code you posted running inside docker? Did I understand correctly that you also have an api running in your docker container, that you then call with postman to verify, that your OkHttpClient succeeded?

Just a guess, if your setup is how I described it, there might be an issue with your docker container being reachable via your local network or the docker container is not configured correctly?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Patrick Wilmes