python - 如何连接到http ://localhost:9000/api/v1/data from jupyter in docker

标签 python api docker jupyter-notebook

我正在 docker 中运行 jupyter。我想从我的 jupyter 笔记本连接到可通过 url http://localhost:9000/api/v1/data 访问的 API .

如果我在本地 jupyter 笔记本(即不在 docker 中)执行以下行,我将成功连接。

import requests
r =requests.get('http://localhost:9000/api/v1/data')
r.status_code

但是,如果在 docker 中的 jupyter Notebook 中执行,相同的行将不会返回错误。 产生的错误为...

ConnectionError: HTTPConnectionPool(host='localhost', port=9000): Max retries exceeded with url: /api/v1/data (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fca3b20bfd0>: Failed to establish a new connection: [Errno 111] Connection refused',))

我尝试将端口 9000 映射到用于运行容器的 YML 文件中的 9000。

# Copyright 2019 QuantRocket LLC - All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Features
# - for local deployment
# - includes all services
# - pinned to the current production versions
# - Sends anonymous crash reports. To disable, edit flightlog
#   env to: SEND_CRASH_REPORTS: 'false'

x-quantrocket-version: '1.9.0'
x-quantrocket-deploy-target: 'local'
version: '2.4' # Docker Compose file version
volumes:
  codeload:
  db:
  flightlog:
  settings:
  zipline:
services:
  account:
    image: 'quantrocket/account:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    depends_on:
      - db
    restart: always
  blotter:
    image: 'quantrocket/blotter:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    depends_on:
      - db
    restart: always
  codeload:
    image: 'quantrocket/codeload:1.9.0'
    environment:
      GIT_URL: 'https://github.com/quantrocket-codeload/quickstart.git'
      GIT_BRANCH: 1.9
    volumes:
      - 'codeload:/codeload'
    restart: always
  countdown:
    image: 'quantrocket/countdown:1.9.0'
    volumes:
      - 'settings:/etc/opt/quantrocket'
      - 'codeload:/codeload'
    restart: always
  db:
    image: 'quantrocket/db:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
      - 'settings:/etc/opt/quantrocket'
    depends_on:
      - postgres
    restart: always
  flightlog:
    image: 'quantrocket/flightlog:1.9.0'
    volumes:
      - 'flightlog:/var/log/flightlog'
      - 'settings:/etc/opt/quantrocket'
    restart: always
    environment:
      SEND_CRASH_REPORTS: 'true'
  fundamental:
    image: 'quantrocket/fundamental:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    depends_on:
      - db
    restart: always
  history:
    image: 'quantrocket/history:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    depends_on:
      - db
    restart: always
  houston:
    image: 'quantrocket/houston:1.9.0'
    ports:
      - '1969:80'
    restart: always
  ibg1:
    image: 'quantrocket/ibg:1.9.972.0'
    environment:
      INI_SETTINGS: '--ExistingSessionDetectedAction=primary'
      API_SETTINGS: '--readOnlyApi=false --masterClientID=6000 --exposeEntireTradingSchedule=true'
    volumes:
      - 'settings:/etc/opt/quantrocket'
    restart: always
  jupyter:
    image: 'quantrocket/jupyter:1.9.0'
    ports:
      - '9000:9000'
    volumes:
      - 'codeload:/codeload'
      - 'db:/var/lib/quantrocket'
    restart: always
  launchpad:
    image: 'quantrocket/launchpad:1.9.0'
    volumes:
      - 'codeload:/codeload'
    restart: always
  license-service:
    image: 'quantrocket/license-service:1.9.0'
    volumes:
      - 'settings:/etc/opt/quantrocket'
    restart: always
  logspout:
    image: 'gliderlabs/logspout:latest'
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
    depends_on:
      - houston
      - flightlog
    command: 'syslog+udp://flightlog:9021,syslog://logs5.papertrailapp.com:47405?filter.name=*houston*'
    restart: always
  master:
    image: 'quantrocket/master:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
      - 'codeload:/codeload'
    depends_on:
      - db
    restart: always
  moonshot:
    image: 'quantrocket/moonshot:1.9.0'
    volumes:
      - 'codeload:/codeload'
    restart: always
  postgres:
    image: 'quantrocket/postgres:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    environment:
      PGDATA: '/var/lib/quantrocket/postgresql/data/pg_data'
    restart: always
  realtime:
    image: 'quantrocket/realtime:1.9.0'
    volumes:
      - 'db:/var/lib/quantrocket'
    depends_on:
      - db
    restart: always
  satellite:
    image: 'quantrocket/satellite:1.9.0'
    volumes:
      - 'codeload:/codeload'
    depends_on:
      - codeload
    restart: always
  theia:
    image: 'quantrocket/theia:1.9.0'
    volumes:
      - 'codeload:/codeload'
    depends_on:
      - codeload
    restart: always
  zipline:
    image: 'quantrocket/zipline:1.9.0'
    volumes:
      - 'codeload:/codeload'
      - 'zipline:/root/.zipline'
    restart: always

但错误仍然存​​在。

最佳答案

当同一组合中的 docker 容器之间进行通信时,您可以使用另一个容器的名称来引用另一个容器作为域名。

所以在你的情况

import requests
r =requests.get('http://jupyter:9000/api/v1/data')
r.status_code

关于python - 如何连接到http ://localhost:9000/api/v1/data from jupyter in docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327047/

相关文章:

docker - 通过 telnet 连接到 docker 容器

docker - 引导后Docker服务无法启动

python - 在 Pyramid 的非请求上下文中检索应用程序配置设置

twitter-bootstrap - 点击按钮即可自动播放youtube视频

java - 为什么 Java Collections API 不包含图形实现?

javascript - 自动从 Dynamics 365 和 Azure 更新外部数据库

python - 我如何计算滚动 idxmax

python - 单元格编辑期间 QTableWidgetItem 中的居中文本

python - 如何将元组内的字典列表转换为 pandas DataFrame?

docker - 在 Docker 内安装 npm 之后访问 node_modules