You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose启动微服务时服务连接被拒,但本地可访问的问题

Docker Compose启动微服务时Eureka无法连接Config Server(Connection refused)

本地通过浏览器或命令行启动Eureka服务时,可正常访问http://localhost:8888/eureka-server/docker,但执行docker-compose up启动时,Eureka服务抛出连接拒绝错误:

2024-04-13T15:45:36.659Z  INFO 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Exception on Url - http://localhost:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/eureka-server/docker": Connection refused.

相关配置文件如下:

Config Server的application.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xyz/config-repo.git
          username: xyz
          password: xyz

# WARNING: Exposing all management endpoints over http should only be used during development, must be locked down in production!
# WARNING: Exposing all management endpoints over http should only be used during development, must be locked down in production!
management.endpoint.health.show-details: "ALWAYS"
management.endpoints.web.exposure.include: "*"

logging:
  level:
    root: info

---
spring:
  config:
    activate:
      on-profile: docker
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xyz/config-repo.git
          username: xyz
          password: xyz

Eureka Server的application.yml

spring.config.import: "configserver:http://localhost:8888"

spring:
  application.name: eureka-server
  cloud.config:
    failFast: true
    retry:
      initialInterval: 3000
      multiplier: 1.3
      maxInterval: 10000
      maxAttempts: 20
    uri: http://localhost:8888
    username: root
    password: root
management:
  security:
    enabled: false

---
spring.config.activate.on-profile: docker

spring.cloud.config.uri: http://config-server:8888

docker-compose.yml

version: '2.1'

services:

  config-server:
    build: spring-cloud/config-server
    mem_limit: 512m
    networks:
      - my-network
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - ENCRYPT_KEY=my-very-secure-encrypt-key
      - SPRING_SECURITY_USER_NAME=root
      - SPRING_SECURITY_USER_PASSWORD=root
    volumes:
      - $PWD/config-repo:/config-repo
    ports:
      - "8888:8888"


  eureka:
    build: spring-cloud/eureka-server
    mem_limit: 512m
    networks:
      - my-network
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - ENCRYPT_KEY=my-very-secure-encrypt-key
      - CONFIG_SERVER_USR=root
      - CONFIG_SERVER_PWD=root
    ports:
      - "8761:8761"

networks:
  my-network:
    name: my-network

问题根源

Docker容器内的localhost指向容器自身而非宿主机。虽然你在Docker Profile中配置了spring.cloud.config.uri: http://config-server:8888,但spring.config.import的配置未被覆盖,导致Eureka仍尝试访问容器内的localhost:8888,而容器内未运行Config Server,因此出现连接拒绝。

修复步骤

  1. 更新Eureka的application.yml,在Docker Profile中同时覆盖spring.config.import配置:
---
spring.config.activate.on-profile: docker

spring.config.import: "configserver:http://config-server:8888"
spring.cloud.config.uri: http://config-server:8888
  1. 配置服务启动依赖与健康检查,确保Config Server就绪后再启动Eureka:

    • 修改docker-compose.yml中config-server服务,添加健康检查:
    config-server:
      build: spring-cloud/config-server
      mem_limit: 512m
      networks:
        - my-network
      environment:
        - SPRING_PROFILES_ACTIVE=docker
        - ENCRYPT_KEY=my-very-secure-encrypt-key
        - SPRING_SECURITY_USER_NAME=root
        - SPRING_SECURITY_USER_PASSWORD=root
      volumes:
        - $PWD/config-repo:/config-repo
      ports:
        - "8888:8888"
      healthcheck:
        test: ["CMD", "curl", "-f", "http://root:root@localhost:8888/actuator/health"]
        interval: 5s
        timeout: 5s
        retries: 5
    
    • 修改eureka服务,添加依赖条件:
    eureka:
      build: spring-cloud/eureka-server
      mem_limit: 512m
      networks:
        - my-network
      environment:
        - SPRING_PROFILES_ACTIVE=docker
        - ENCRYPT_KEY=my-very-secure-encrypt-key
        - CONFIG_SERVER_USR=root
        - CONFIG_SERVER_PWD=root
      ports:
        - "8761:8761"
      depends_on:
        config-server:
          condition: service_healthy
    
  2. 验证修复:执行docker-compose down && docker-compose up,此时Eureka会通过Docker内部DNS解析config-server服务,正常连接到Config Server。

内容的提问来源于stack exchange,提问作者MAYANK DEVRANI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:45:05