Docker 网络不适用于尤里卡

Docker networking not working with eureka

我在 docker 容器中有一个尤里卡服务器,在另一个 docker 容器中有另一个应用程序。我使用 docker network create eureka-s1 创建了一个网络。所以,我 运行 使用命令连接尤里卡服务器:docker run --network eureka-s1 --name eureka-server -p 8761:8761 f595acb214d5 eureka服务器的配置文件如下:

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

所以,运行没问题。现在,当我 运行 我的应用程序使用命令:docker run --network eureka-s1 --name first-service -p 8080:8080 c16b2442b890 时,应用程序没有注册到尤里卡。客户端配置文件如下:

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

有人可以指出这里的问题吗?谢谢。

编辑: 尤里卡服务器 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 客户端 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"]

使用 docker-compose 可以正常工作。

下面是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

并按照以下方式添加更新您的客户端配置文件:

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

并更新 eureka 服务器的 Dockerfile 如下:

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"]

在docker-compose.yml旁边 运行

docker-compose up --build

并且不要忘记 运行 mvn clean install 您的客户端应用程序。