简体   繁体   中英

Unknown Host Exception

Unknown HostException Exception on Docker Container.

I have multiples services, my docker-compose has the following code:

version: '3.5'

    services:


      reservation:
        container_name: sp-app-reservation
        image: swimming-pool/reservation-service
        environment:
         - SPRING_PROFILES_ACTIVE=dev
        ports:
         - 8181:8182
        depends_on:
         - notification
        network_mode: host
        restart: always


      web:
        container_name: sp-app-web-ui
        image: swimming-pool/web-ui-service
        environment:
         - SPRING_PROFILES_ACTIVE=dev
        ports:
         - 8080:8081
        depends_on:
         - notification
         - reservation
         - fileupload
        network_mode: host
        restart: always  

      eureka:
        container_name: sp-app-eureka
        image: swimming-pool/discovery-service
        environment:
         - SPRING_PROFILES_ACTIVE=dev
         - SERVICE_PORT_RESERVATION=8181
         - SERVICE_PORT_FILEUPLOAD=8686
         - SERVICE_PORT_WEB=8080
        ports:
         - 8761:8762
        network_mode: host
        restart: always

I'm using Rest Template to invoke from Web service to Reservation service using the " http://sp-app-reservation:8182/rest/reservation ".

I'm facing the below exception, and i can see my container running with the same name.

org.springframework.web.client.ResourceAccessException: I/O error on GET 
request for "http://sp-app-reservation:8182/rest/reservation/":
sp-app-reservation;  nested exception is java.net.UnknownHostException: 
sp-app-reservation

You are using network_mode: host , so you need to configure host record "sp-app-reservation" on the host OS level (for example "hardcode" it in /etc/hosts ).

Better option will be to create docker network, where you can use docker container names and you will publish only selected ports to host OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM