Search code examples
javadockernetflix-eurekaspring-cloud-netflixservice-discovery

How Do I Containerize Eureka Server In Docker


i have been struggling to use my created eureka-server container in docker... I have gone through previous solutions and am still not getting why i cant access the url: http://localhost:8761/ I have changed my properties file severally but no one seems to be working... Firstly my application.properties file goes like this

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
spring.application.name=discovery-service
eureka.instance.prefer-ip-address=true
logging.level.org.springframework.cloud.commons.util.InetUtils=trace
spring.cloud.inetutils.timeout-seconds=10

And my dependecies tag of my pom.xml goes like this

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

And i have also added the @EnableEurekaServer to my application class... So when i created an image for it using the docker command docker build -t davidtega/eureka-layered -f Dockerfile.layered . It worked perfectly, and i started a container using the docker command docker run -p 8761:8761 -t davidtega/eureka-layered And this the log enter image description here

But when i try to access http://localhost:8761/, this site cannot be reached is the response i get everytime... So i noticed my app was running on 0.0.0.0:8761 not 127.0.0.1:8761 enter image description here

I was wondering how do i change it ??? I have two docker files, the first one is the DockerFile and the second one is the Dockerfile.layered For my DockerFile, this is what is in it...

FROM openjdk:17

COPY target/*.jar app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

And my DockerFile.layered file contains

FROM eclipse-temurin:17.0.4.1_1-jre as builder
WORKDIR extracted
ADD target/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract

FROM eclipse-temurin:17.0.4.1_1-jre
WORKDIR application
COPY --from=builder extracted/dependencies/ ./
COPY --from=builder extracted/spring-boot-loader/ ./
COPY --from=builder extracted/snapshot-dependencies/ ./
COPY --from=builder extracted/application/ ./
EXPOSE 8761
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

Please and please help i request assistance, i am using spring cloud version 2.7 and java 17... Thanks


Solution

  • Add to the config eureka.hostname=localhost and eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

    Make sure the port is mapped doing a docker run -p 8761 and then check that the port is correctly listening with lsof -i -P -n | grep LISTEN