I have a Hello service and World service. The Hello service has a rest point /hello/hw which internally calls rest end point in World service to return string "Hello World". The application works fine as expected if the system is not dockerized.
However the issue is when I want to dockerize the system. I use docker-compose to containerize both services. I have defined a custom network in the docker compose file (named as custom_net).
The rest end point /hello/hw is reachable but Hello service is not able to call the rest end point in the World service.
The application.properties in Hello service has the url of World service as http://localhost:8082/world
I get UnknownHostException when I set the url as http://custom_net:8082/world or to http://custom_net:8092/world
I get Connection refused exception when I don't change the url in application.properties. The log message states 'failed: Connection refused: localhost/127.0.0.1:8082;' I am not sure what configurations are needed to bridge the 2 services
The command docker network list shows the name of custom network as 'tempfolder_custom_net'. The command docker inspect shows that both hello and world service are registered to this network
version: '3'
services:
hello_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: hello_service
volumes:
- ./deploy:/root
networks:
- custom_net
depends_on:
- world_service
command: sh -c "java -jar -Dspring.config.location=file:///root/hello/application.yml /root/hello/hello-0.0.1-SNAPSHOT.jar "
ports:
- 8091:8081
world_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: world_service
volumes:
- ./deploy:/root
command: sh -c "java -jar -Dspring.config.location=file:///root/world/application.yml /root/world/world-0.0.1-SNAPSHOT.jar "
ports:
- 8092:8082
networks:
- custom_net
networks:
custom_net:
driver: bridge
Application.yml of Hello service ...
server:
port: 8081
services:
world:
url: http://localhost:8082/world
CodePudding user response:
You should build new image base on openjdk:8-jdk-alpine
image, and add ENTRYPOINT
You should change this:
server:
port: 8081
services:
world:
url: http://localhost:8082/world
To :
server:
port: 8081
services:
world:
url: http://world_service:8092/world
Make sure, both containers are running.
CodePudding user response:
@Thanh Nuguyen Van thanks for providing useful hints in troubleshooting. The issue was with the host name world_service. Looks like underscore is not a valid URL. Below is my updated docker compose file. I didn't even have to use custom docker network. I updated the url in application.yml to http://worldservice:8082/world. Also note that the port is 8082 not 8092. We have to mention the application's port and not the docker exposed port.
version: '3'
services:
helloservice:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: helloservice
volumes:
- ./deploy:/root
#networks:
#- custom_net
depends_on:
- worldservice
command: sh -c "java -Xdebug -jar -Dspring.config.location=file:///root/hello/application.yml /root/hello/hello-0.0.1-SNAPSHOT.jar "
ports:
- 8091:8081
worldservice:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: worldservice
volumes:
- ./deploy:/root
command: sh -c "java -Xdebug -jar -Dspring.config.location=file:///root/world/application.yml /root/world/world-0.0.1-SNAPSHOT.jar "
ports:
- 8092:8082
#networks:
#- custom_net
#networks:
#custom_net:
#driver: bridge