0

I have two services, spring boot docker and when I try communication with rest template I got java.net.ConnectException: Connection refused (Connection refused)

url is http://localhost:8081/api/v1/package/250Mbps

Service 1 docker-compose.yml:

product_catalogue_service:
image: openjdk:8-jdk-alpine
ports:
  - "8081:8081"
volumes:
  - .:/app
working_dir: /app
command: ./gradlew bootRun
restart: on-failure

Service 2 docker-compose.yml:

order_service:
image: openjdk:8-jdk-alpine
ports:
  - "8083:8083"
volumes:
  - .:/app
working_dir: /app
command: ./gradlew bootRun
restart: on-failure

Rest template URL, and it is working when I run project 2 from the IntelliJ: http://localhost:8081/api/v1/package/250Mbps

When I run docker ps, name of first service is: productcatalogueservice_product_catalogue_service_1

I tried to use that instead of localhost - unknown host exception.

I tried "product_catalogue_service_1" instead, also unknown host exception, and finally I tried "product_catalogue_service" also unknown host exception.

Any idea?

J.H.13
  • 3
  • 4

1 Answers1

0

By default, docker-compose creates a network between your containters and assign the serivce name as de host name.

So, you can reach the product_catalog_service from order_service like so: http://product_catalog_service:8081/api/v1/package/250Mbps

Now, from your post it seems that you have two different docker-compose.yml files, one for each service. In this case, each container is living in its own network (created by docker-compose) and they can't see each other, at least by default.

Honestly, I've never try to connect two containers created from two different docker-compose files. I always create one docker-compose.yml file, put there all my services and let docker-compose manage the network between them.

Using one docker-compose.yml

version: ...
services:
  product_catalogue_service:
    image: openjdk:8-jdk-alpine
    ports:
      - "8081:8081"
    volumes:
      - .:/app
    working_dir: /app
    command: ./gradlew bootRun
    restart: on-failure
  order_service:
    image: openjdk:8-jdk-alpine
    ports:
      - "8083:8083"
    volumes:
      - .:/app
    working_dir: /app
    command: ./gradlew bootRun
    restart: on-failure

One last thing, this answer explains in great detail why localhost:8081 is not working from your order_service container.

theBittor
  • 786
  • 1
  • 11
  • 20
  • Thank you, now I understand and I will set up things different! – J.H.13 Mar 30 '20 at 06:45
  • I solved this by setting up one docker-compose file, also underscore in service name is not good practice because underscore is not allowed in domain names. So after all those changes, I have `http://product-catalogue-service:8081/api/v1/package/250Mbp` working in order service. – J.H.13 Mar 30 '20 at 12:48