Search code examples
dockerjax-rsliferay

Connecting to a dockerized REST JaxRS end point from within another container locally


I am attempting to connect to a rest end point of a JaxRS liferay portlet. If I try and connect through postman using http://localhost:8078/engine-rest/process-definition

It works 200 okay.

I am attempting to connect to the same end point from within another docker container part of the same docker network, I have tried with localhost and I receive the error:

java.net.ConnectException: Connection refused (Connection refused)

I have also tried http://wasp-engine:8078, wasp-engine is the docker name of the container. Still receiving the same error.

Here are the two containers in my compose file:

wasp-engine:
  image: in/digicor-engine:test
  container_name: wasp-engine
  ports:
    - "8078:8080"
  depends_on:
    mysql:
      condition: service_healthy

wasp:
  image: in/wasp:local2
  container_name: Wasp
  volumes:
    - liferay-document-library:/opt/liferay/data
  environment:
    - camundaEndPoint=http://wasp-engine:8078
  ports:
    - "8079:8080"
  depends_on:
    mysql:
      condition: service_healthy

They are both connecting to the mysql fine which is part of the same docker network and referenced via:

jdbc.default.url=jdbc:mysql://mysql/liferay_test

Solution

  • tl;dr

    Use http://wasp-engine:8080

    The why

    In your docker-compose the ports: - "8078:8080" field on wasp-engine will expose port 8080 of the docker container to your host computer on port 8078. This is what allows your postman to succeed in connecting to the container over localhost. However, once inside the docker container localhost refers to the docker container itself. This port forwarding no longer applies.

    Using docker-compose you can use the name of the container to target the specific docker container. You mentioned you tried this with the URI http://wasp-engine:8078. When you access the container this way the original port is used not the forwarded port for the host machine. This means that the docker container should be targeting port 8080.

    Putting it all together, the final URI should be http://wasp-engine:8080.