当我尝试从也在端口8000上使用docker-compose部署的Django应用程序调用在端口5000上使用docker-compose部署的服务时,我也遇到了此错误。我也在使用nginx。
ConnectionError at /documents/
HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url:
/api/documents (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7f976607f290>: Failed to establish a new connection: [Errno 111] Connection refused'))
这是发出请求之前的请求对象
kwargs
{'data': None,
'files': {'file': ('XXX',
<InMemoryUploadedFile: XXX.pdf (application/pdf)>,
'application/pdf',
{})},
'headers': {'Authorization': 'Token token=XXX',
'Content-Type': 'application/pdf',
'PSPDFKit-API-Version': '2020.1.3'},
'json': None}
method
'post'
session
<requests.sessions.Session object at 0x7f954da972d0>
url
'http://127.0.0.1:5000/api/documents'
以下是相关文件
docker-compose
version: '3.7'
services:
web:
build:
context: ./www
dockerfile: Dockerfile.prod
command: gunicorn app.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./www:/usr/src/app
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
expose:
- 8000
env_file: env.prod
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: env.prod
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
ports:
- 1337:80
depends_on:
- web
pspdfkit:
image: "pspdfkit/pspdfkit:2020.1"
environment:
PGUSER: XXX
PGPASSWORD: XXX
PGDATABASE: XXX
PGHOST: db
PGPORT: 5432
# Activation key for your PSPDFKit Server installation.
ACTIVATION_KEY: XXXX
# Secret token used for authenticating API requests.
API_AUTH_TOKEN: XXXX
# Base key used for deriving secret keys for the purposes of authentication.
SECRET_KEY_BASE: XXXX
# Public key used for verification of JWTs from web clients. It has to be in the PEM format.
JWT_PUBLIC_KEY: |
-----BEGIN PUBLIC KEY-----
XXXXTYYYYY
-----END PUBLIC KEY-----
JWT_ALGORITHM: RS256
# Credentials to access the admin dashboard.
DASHBOARD_USERNAME: XXX
DASHBOARD_PASSWORD: YYY
depends_on:
- db
restart: always
ports:
- "5000:5000"
volumes:
- asset_storage:/srv/asset_storage
volumes:
postgres_data:
static_volume:
media_volume:
asset_storage:
nginx.conf
upstream app {
server web:8000;
}
server {
client_max_body_size 20M;
listen 80;
location / {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /home/app/web/staticfiles/;
}
location /mediafiles/ {
alias /home/app/web/mediafiles/;
}
}
Settings.py
PSPDFKIT_SERVER_HOST = "http://127.0.0.1:5000"
PSPDFKIT_EXTERNAL_SERVER = "http://127.0.0.1:5000"
我尝试将url更改为localhost
,0.0.0.0
,pspdfkit
(来自docker-compose文件)以及服务器的公共IP,但最终在所有情况下都会出现上述错误,但当我使用公共IP时(无法建立连接)
我不太熟悉nginx,因此我可能缺少一些导致此问题的重要配置步骤。帮助将不胜感激!
答案 0 :(得分:0)
错误来自于使用 0.0.0.0
Docker 引入了一个虚拟网络,所以每个 docker-isntance 都可以看到 本身为 127 而不是主机
(感谢@Jmons)
要在本地启动您的代码,使用您的固定 IPv4 地址。
对于 Windows 用户:打开命令提示符 (cmd) 并运行 ipconfig
命令,检索位于 IPv4 address. . . . . . . . . . . . . .:
的 @IP,它通常是类型 192.168.x.x
IPv4 address in command prompt