Search code examples
springdockerspring-cloudnetflix-eureka

Docker networking not working with eureka


I have one eureka server inside a docker container and another application inside another docker container. I have made a network using docker network create eureka-s1. So, I'm running the eureka server using the command: docker run --network eureka-s1 --name eureka-server -p 8761:8761 f595acb214d5 The config file of the eureka server is as follows:

spring:
  application:
    name: discovery-service

eureka:
  instance:
    prefer-ip-address:true
    hostname:localhost

  client:
    eureka-server-connect-timeout-seconds: 5
    enabled: true
    fetch-registry: false
    register-with-eureka: false

server:
  port: 8761

So, its running fine. Now, when I run my application using the command: docker run --network eureka-s1 --name first-service -p 8080:8080 c16b2442b890, the application is not registering to eureka. The config file of the client is as follows:

spring:
  application:
    name: first-service
server:
  port: 8080
eureka:
  client:
    fetch-registry: true
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka  

Can someone point out the problem here? Thanks.

Edit: Eureka Server Dockerfile:

FROM openjdk:8
ADD target/cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar
EXPOSE 8761
ENTRYPOINT ["java", "-jar", "cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar"]

Eureka Client Dockerfile:

FROM openjdk:8
ADD target/first-service-0.0.1-SNAPSHOT.jar first-service-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "first-service-0.0.1-SNAPSHOT.jar"]

Solution

  • It's work fine with docker-compose.

    Below is the docker-compose.yml

    version: '2.1'
    
    services:
      eureka:
        build: ${PATH_TO_EUREKA_FOLDER}
        mem_limit: 350m
        networks:
            - my-network
        healthcheck:
          test: ["CMD", "curl", "-f", "http://eureka:8761"]
          interval: 30s
          timeout: 10s
          retries: 5
    
      client:
        build: ${PATH_TO_YOUR_CLIENT_FOLDER}
        mem_limit: 350m
        networks:
          - my-network
        links:
          - eureka
        depends_on:
          eureka:
            condition: service_healthy
    
    
    networks:
      my-network:
        name: my-network
    

    And add update your client config file as bellow :

    spring:
        application:
            name: first-service
    server:
        port: 8080
    eureka:
        client:
            fetch-registry: true
            serviceUrl:
                defaultZone: http://eureka:8761/eureka
    

    And update the Dockerfile of the eureka server as bellow :

    FROM openjdk:8
    
    RUN apt-get update
    RUN apt-get install -y curl
    
    EXPOSE 8761
    
    ADD ./target/*.jar app.jar
    
    ENTRYPOINT ["java","-jar","/app.jar"]
    

    And next to docker-compose.yml run

    docker-compose up --build
    

    And don't forget to run mvn clean install your client application.