【问题标题】:requests.exceptions.ConnectionError: HTTPConnectionPool(host='docker.for.linux.localhost', port=8000): Max retries exceeded with url: /api/userrequests.exceptions.ConnectionError: HTTPConnectionPool(host='docker.for.linux.localhost', port=8000): url: /api/user 超出最大重试次数
【发布时间】:2021-06-14 13:46:09
【问题描述】:

在 ubuntu 20.04 LTS 中,我收到此错误

requests.exceptions.ConnectionError: HTTPConnectionPool(host='docker.for.linux.localhost', port=8000): url: /api/user 超出最大重试次数

其实我已经为flask和django制作了两个docker镜像。 在烧瓶应用中,

我的 main.py:

from dataclasses import dataclass
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from sqlalchemy import UniqueConstraint
import requests


app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql://root:root@db/main'
CORS(app)

db = SQLAlchemy(app)



@dataclass
class Product(db.Model):
    id: int
    title: str
    image: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=False)
    title = db.Column(db.String(200))
    image = db.Column(db.String(200))

@dataclass
class ProductUser(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    product_id = db.Column(db.Integer)

    UniqueConstraint('user_id', 'product_id', name='user_product_unique')

@app.route('/api/products')
def index():
    return jsonify(Product.query.all())

@app.route('/api/products/<int:id>/like', methods=['POST'])
def like(id):
    req = requests.get('http://docker.for.linux.localhost:8000/api/user')
    return jsonify(req.json())


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

我的 Dockerfile:

FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip3 install -r requirements.txt
COPY . /app

我的 docker-compose.yaml 文件:

version: '3.8'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    command: 'python main.py'
    ports:
      - 8001:5000
    volumes:
      - .:/app
    depends_on:
      - db

  queue:
    build:
      context: .
      dockerfile: Dockerfile
    command: 'python3 -u consumer.py'
    depends_on:
      - db

  db:
    image: mysql:5.7.22
    restart: always
    environment:
      MYSQL_DATABASE: main
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - .dbdata:/var/lib/mysql
    ports:
      - 33067:3306

当我在 postman 中输入网址时 http://localhost:8001/api/products/1/like 邮寄方式 我收到问题中提到的上述错误

谁能告诉我如何解决这个问题?

【问题讨论】:

    标签: python-3.x docker flask postman


    【解决方案1】:

    我正在为我使用 Windows 10 http://docker.for.win.localhost:8000/api/user 工作。您可以尝试使用 lin 或检查并确认 Linux 缩写。

    req = requests.get('http://docker.for.win.localhost:8000/api/user')
    json = req.json()
    

    【讨论】:

    猜你喜欢
    • 2019-09-24
    • 2020-07-19
    • 2020-11-18
    • 1970-01-01
    • 2014-01-27
    • 2020-10-31
    • 2019-10-30
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多