Cannot send HTTP request to another docker container

When I try to send HTTP request from one container to another I get

(Connection refused); nested exception is java.net.ConnectException

I am sure the cause of the error is an invalid address. Though I have checked similar issues I wasn’t able to fix it myself.

Dockerfile of mainApp (the one which sends requests)

FROM openjdk:8
ADD target/mainApp.war mainApp.war
EXPOSE 8078
ENTRYPOINT ["java", "-jar", "mainApp.war"]

Here I make the request

String pepAddress =  "http://microservice3:8080";

public ResponseEntity<String> getUser() {

       RestTemplate restTemplate = new RestTemplate();
       restTemplate.setErrorHandler(new ErrorHandler());

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
        requestFactory.setProxy(proxy);
        restTemplate.setRequestFactory(requestFactory);

        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(pepAddress + "/getUser")
                .queryParam("userId","1");

        HttpEntity<?> httpEntity = new HttpEntity<>(httpHeaders);

        ResponseEntity<String> result = restTemplate
                .exchange(builder.toUriString(),
                            HttpMethod.GET,
                            httpEntity,
                            String.class);
        return result;
    }

SecondApp Dockerfile

FROM openjdk:8
ADD target/PEP.war PEP.war
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "PEP.war"]

And the docker-compose.yml

version: '2'
services:
  microservice1:
    image: mainapp
    expose:
      - 8078
    networks:
      - test

  microservice3:
    image: pep
    expose:
      - 8080
    networks:
      - test

networks:
  test: 
    external: true

Source: StackOverflow