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

Docker Compose中Jupyter容器访问Spring Boot遇ConnectionRefusedError

容器内Jupyter无法访问本地Spring Boot接口的解决方案

我搭建了包含MongoDB、Mongo-Express、Jupyter Notebook的Spring Boot本地应用,本地Python脚本访问http://localhost:8080/api/v1/metadata/sites能正常返回<Response [200]>,但在Jupyter容器内运行相同代码时出现ConnectionRefusedError。以下是相关配置:

本地运行正常的Python代码

import requests
import json

url = "http://localhost:8080/api/v1/metadata/sites"

response = requests.get(url)
print(response)

# print json content
print(response.json())

>>> <Response [200]>

Jupyter的Dockerfile

FROM python:3.9
LABEL authors=""

# Install any necessary packages
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    build-essential \
    git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /app

# Copy the requirements file and install the dependencies
RUN pip install --upgrade pip
RUN pip install --no-cache-dir numpy==1.26.4  \
    jupyter \
    pandas==2.2.2 \
    requests==2.31.0

# Copy the notebooks to the container
COPY . /app

EXPOSE 8888

# Set the default command to run Jupyter Notebook
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "--NotebookApp.token=''"]

docker-compose.yml

version: '3.9'

services:
  # Database - Mongo DB
  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: always
    ports:
      - "27017:27017"
    env_file: .env
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
      - MONGO_INITDB_DATABASE=${MONGO_INITDB_DATABASE}
    volumes:
      - mongo-volume:/data/db2
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

  # Database Manager
  mongo-express:
    image: mongo-express:latest
    container_name: mongo-express
    restart: always
    ports:
      - "8081:8081"
    depends_on:
      - mongodb
    env_file: .env
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=${ME_CONFIG_MONGODB_ADMINUSERNAME}
      - ME_CONFIG_MONGODB_ADMINPASSWORD=${ME_CONFIG_MONGODB_ADMINPASSWORD}
      - ME_CONFIG_MONGODB_SERVER=mongodb # same name of "mongodb" in services


  jupyter:
    build:
      context: .
      dockerfile: ./jupyter/Dockerfile
    container_name: jupyter-metadata-itineris
    ports:
      - '8888:8888'
    volumes:
      - ./notebooks:/app

# Define named volumes
volumes:
  mongo-volume: {}

networks:
  default:
    name: mongodb_network

问题原因

容器内的localhost指向容器自身的网络栈,而你的Spring Boot应用运行在宿主机上,所以Jupyter容器无法通过localhost:8080访问到宿主机的服务。

解决方法

方法1:使用宿主机实际IP地址

找到宿主机在本地网络中的IP(Windows用ipconfig,Linux/macOS用ip addr),替换Python代码中的URL:

url = "http://你的宿主机IP:8080/api/v1/metadata/sites"

方法2:Docker内置宿主机别名(Windows/macOS)

Windows和macOS环境下,Docker提供host.docker.internal作为宿主机的默认别名,直接替换URL即可:

url = "http://host.docker.internal:8080/api/v1/metadata/sites"

方法3:Linux环境下使用Host网络模式

修改docker-compose.yml中Jupyter服务的网络配置,使用宿主机网络:

jupyter:
  build:
    context: .
    dockerfile: ./jupyter/Dockerfile
  container_name: jupyter-metadata-itineris
  network_mode: host # 添加该行
  volumes:
    - ./notebooks:/app

注意:使用Host网络后,端口映射ports: - '8888:8888'可移除,容器将直接使用宿主机的网络端口。

方法4:将Spring Boot应用加入同一Docker网络

把Spring Boot应用容器化,并加入到mongodb_network网络中,即可通过服务名访问。示例docker-compose.yml新增配置:

spring-boot-app:
  image: 你的Spring Boot镜像名
  container_name: spring-boot-app
  ports:
    - "8080:8080"
  networks:
    - mongodb_network

Jupyter中的URL改为:

url = "http://spring-boot-app:8080/api/v1/metadata/sites"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:06:33