How do I keep the Eureka server url dynamic in the Eureka Client while using docker? How do I keep the Eureka server url dynamic in the Eureka Client while using docker? docker docker

How do I keep the Eureka server url dynamic in the Eureka Client while using docker?


Spring can pickup Environment Variables. So if you add Environment Variables to the Docker Container that Spring Boot is running in, they will work. This avoids the need to provide a static URL up front.

If you use Docker Compose, it could look like this:

services:  eureka:    image: springcloud/eureka    container_name: eureka    ports:      - "8761:8761"    networks:      - "discovery"    environment:      - EUREKA_INSTANCE_PREFERIPADDRESS=true  spring:    build:      context: .      dockerfile: ./src/main/docker/Dockerfile    depends_on:      - eureka    container_name: spring    ports:     - "8080:8080"    networks:     - "discovery"    environment:      - EUREKA_SERVICE_URL=http://eureka:8761 // This overrides your Spring Property      - EUREKA_INSTANCE_PREFER_IP_ADDRESS=true      - LOGGING_FILE=/tmp/admin.log

Note: Since Environment Variables are not YAML, you need to change the format a bit. https://docs.spring.io/spring-boot/docs/1.5.5.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding