尝试从 Docker 中的芹菜容器连接到 Web 容器



我有一个 django 频道容器和一个芹菜工人,我正在尝试连接到芹菜容器内的 django 频道容器,但是,我发现很难连接。我有一个 django 服务器运行在 http://127.0.0.1:8000/上。这就是我的 docker 撰写文件的样子

web:
restart: always
tty: true
build: ./web/
working_dir: /data/web/fileshiffty
expose:
- "8000"
ports:
- "8000:8000"
links:
- postgres:postgres
- redis:redis
env_file: env
volumes:
- ./web:/data/web
command: bash -c "python3 manage.py runserver 0.0.0.0:8000"
postgres:
restart: always
image: postgres:latest
volumes_from:
- data
volumes:
- ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./backups/postgresql:/backup
env_file:
- env
expose:
- "5432"
redis:
restart: always
image: redis:latest
expose:
- "6379"
worker:
build: ./web/
working_dir: /data/web/fileshiffty
command: bash -c "celery -A fileshiffty worker --loglevel=DEBUG"
volumes:
- ./web:/data/web
links:
- postgres:postgres
- redis:redis
- web:web
data:
restart: always
image: alpine
volumes:
- /var/lib/postgresql
command: "true"

但是,在我的芹菜任务中,它需要它连接到带有 urlws://127.0.0.1:8000/ws/converter/public/的网络套接字。这就是我的任务的样子

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
import img2pdf
import os
import websocket 
import json
import boto
from boto.s3.key import Key
@shared_task
def convert_file_to_pdf(file_name_on_cdn):
ws = websocket.WebSocket()
ws.connect('ws://web/ws/converter/public/')
ws.send(json.dumps({
'message': '{}'.format(file_name_on_cdn), 
'progress': 0}))
# some other stuff 

但是,当任务运行时,我不断收到以下错误

worker_1    | [2018-03-21 16:19:03,392: ERROR/ForkPoolWorker-2] Task api.tasks.convert_file_to_pdf[2fb26db9-7dd5-4b57-9242-fd61e743952d] raised unexpected: ConnectionRefusedError(111, 'Connection refused')
worker_1    | Traceback (most recent call last):
worker_1    |   File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 374, in trace_task
worker_1    |     R = retval = fun(*args, **kwargs)
worker_1    |   File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 629, in __protected_call__
worker_1    |     return self.run(*args, **kwargs)
worker_1    |   File "/data/web/fileshiffty/api/tasks.py", line 30, in convert_file_to_pdf
worker_1    |     ws.connect('ws://web/ws/converter/public/')
worker_1    |   File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 217, in connect
worker_1    |     options.pop('socket', None))
worker_1    |   File "/usr/local/lib/python3.6/site-packages/websocket/_http.py", line 70, in connect
worker_1    |     sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
worker_1    |   File "/usr/local/lib/python3.6/site-packages/websocket/_http.py", line 131, in _open_socket
worker_1    |     raise err
worker_1    |   File "/usr/local/lib/python3.6/site-packages/websocket/_http.py", line 116, in _open_socket
worker_1    |     sock.connect(address)
worker_1    | ConnectionRefusedError: [Errno 111] Connection refused

很明显,它无法连接到 url。我什至尝试设置ws.connect('ws://web/ws/converter/public/')但是,这似乎也不起作用。任何帮助将不胜感激。几个小时以来一直试图找出问题所在

看起来我所要做的就是指定我的 Django 应用程序正在运行的端口,即端口 8000,所以我只需要将ws更改为ws.connect('ws://web:8000/ws/converter/public/')

相关内容

  • 没有找到相关文章

最新更新