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

使用Docker Compose部署双容器至同一网络时遇沙箱接口添加失败错误

问题解决方法

核心冲突原因

你在duty-students-notifier服务中同时配置了network_mode: host和networks: dsn-net,这两个配置互斥:

  • network_mode: host会让容器直接复用宿主机网络栈,不会生成独立网络接口,也无法加入自定义桥接网络
  • 同时指定networks会触发Docker尝试给容器分配桥接网络IP,这和host模式的网络配置冲突,最终导致IP路由冲突错误

修复步骤

1. 移除冲突的网络配置

删除duty-students-notifier服务中的network_mode: host配置,让容器正常加入自定义dsn-net网络:

services:
  duty-students-notifier:
    image: duty-students-notifier:latest
    # 移除该行:network_mode: host
    container_name: duty-students-notifier
    build:
      context: ../
      dockerfile: ./docker/Dockerfile
    env_file: ../.env
    volumes:
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    networks:
      - dsn-net
    restart: always

2. 修正Redis服务的端口配置

Redis默认使用6379端口,你当前配置的expose: - 5432是PostgreSQL的端口,属于错误配置,需修正:

dsn-redis:
  image: redis:latest
  expose:
    - 6379  # 修正为Redis默认端口
  volumes:
    - dsn-redis-data:/var/lib/redis
    - dsn-redis-conf:/usr/local/etc/redis/redis.conf
  networks:
    - dsn-net
  restart: always

3. 重新部署容器

执行以下命令清理旧容器并重新启动:

docker-compose down
docker-compose up -d

额外说明

如果业务确实需要使用host网络模式(比如要直接访问宿主机所有端口),则:

  • 无需配置networks字段
  • 容器内访问Redis时直接用宿主机IP(如127.0.0.1),而非服务名dsn-redis
  • 需确保Redis服务绑定到宿主机网络,或通过端口映射暴露到宿主机

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 00:10:35