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

Docker Compose中Spring Boot服务无法连接Redis容器求助

Spring Boot + Docker Compose 连接Redis失败问题排查与解决

我有一个基于Spring Boot开发的电商订单服务,使用Jedis连接Redis。本地运行源码时连接正常,但通过Docker Compose部署后出现错误:redis.clients.jedis.exceptions.JedisConnectionException: Failed to connect to any host resolved for DNS name.。单独运行Redis容器配合本地源码可正常连接,但Docker Compose同时启动服务与Redis时无法建立连接。

现有配置信息

Docker Compose配置

version: '3'

services:
  order-api:
    build:
      context: ./orderapi
      dockerfile: Dockerfile
    ports:
      - "8002:8002"
    depends_on:
      - redis
    environment:
      - MYSQL_HOST=host.docker.internal
      - MYSQL_PORT=3306
      - MYSQL_DATABASE=ecommerce_fyp_order
      - MYSQL_USER=root
      - MYSQL_PASSWORD=12345
      - REDIS_HOST=redis #options: host.docker.internal, redis, localhost
      - REDIS_PORT=6379
    networks:
      - net

  redis:
    image: redis:latest #IPaddress is 172.18.0.2
    ports:
      - "6379:6379"
    command: ["redis-server", "--bind", "redis", "--port", "6379"]
    networks:
      - net

networks:
  net:
    driver: bridge

application.properties配置

server.port=8002
spring.application.name=orderapi

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/ecommerce_fyp_order
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql: true

#Redis
spring.session.redis.namespace=session
spring.data.redis.host={REDIS_HOST:localhost}
spring.data.redis.port=6379

Redis配置类

@Configuration
public class RedisConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

补充问题细节

  • 尝试将REDIS_HOST改为redis、host.docker.internal、localhost均无效
  • 执行docker exec -it e-commercenew-redis-1 redis-cli连接Redis容器时,报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused
  • Docker环境变量显示:REDIS_HOST=redis,但同时存在SPRING_DATA_REDIS_HOST=localhost

问题修复步骤

1. 修正application.properties的变量引用格式

Spring Boot的环境变量引用必须使用${变量名:默认值}格式,而你当前配置用了大括号{},导致REDIS_HOST环境变量未被正确读取,服务一直使用默认的localhost连接,无法访问Docker网络内的Redis容器。

修改Redis配置部分:

#Redis
spring.session.redis.namespace=session
spring.data.redis.host=${REDIS_HOST:localhost}
spring.data.redis.port=${REDIS_PORT:6379}

2. 调整Redis容器的绑定地址

你给Redis容器设置了--bind redis,这会让Redis仅监听容器内redis主机名对应的IP,导致在容器内用默认的127.0.0.1连接时失败。

修改Docker Compose中Redis的command配置,改为绑定所有可用IP:

redis:
  image: redis:latest
  ports:
    - "6379:6379"
  command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"]
  networks:
    - net

绑定0.0.0.0后,Redis会监听容器内所有IP,既支持容器间通过服务名redis访问,也支持在容器内直接用redis-cli连接。

3. 清理镜像中硬编码的Redis配置

从Docker环境变量可以看到SPRING_DATA_REDIS_HOST=localhost,这可能是打包镜像时通过Dockerfile硬编码设置的,会覆盖环境变量REDIS_HOST。检查你的Dockerfile,删除类似ENV SPRING_DATA_REDIS_HOST localhost的命令。

4. 验证容器间网络连通性

重启Docker Compose后,进入order-api容器测试网络:

  • 测试Redis服务可达性:
    docker exec -it <order-api容器ID> ping redis
    
  • 测试Redis连接:
    docker exec -it <order-api容器ID> redis-cli -h redis
    

如果以上命令都能成功执行,说明配置修复完成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:45:55