Django无法连接到docker中的redis



对不起我的英语。我在django中有一个项目,在我的项目中,我想使用芹菜作为后台任务,现在我需要在docker中为这个库设置设置。这是我的docker文件:

FROM python:3
MAINTAINER Alex2
RUN apt-get update
# Install wkhtmltopdf
RUN curl -L#o wk.tar.xz https://downloads.wkhtmltopdf.org/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz 
&& tar xf wk.tar.xz 
&& cp wkhtmltox/bin/wkhtmltopdf /usr/bin 
&& cp wkhtmltox/bin/wkhtmltoimage /usr/bin 
&& rm wk.tar.xz 
&& rm -r wkhtmltox
RUN apt-get install -y cron
# for celery
ENV APP_USER user
ENV APP_ROOT /src
RUN groupadd -r ${APP_USER} 
&& useradd -r -m 
--home-dir ${APP_ROOT} 
-s /usr/sbin/nologin 
-g ${APP_USER} ${APP_USER}

# create directory for application source code
RUN mkdir -p /usr/django/app
COPY requirements.txt /usr/django/app/
WORKDIR /usr/django/app
RUN pip install -r requirements.txt

这是我的docker-compose.dev

version: '2.0'
services:
web:
build: .
container_name: api_dev
image: img/api_dev
volumes:
- .:/usr/django/app/
- ./static:/static
expose:
- "8001"
env_file: env/dev.env
command: bash django_run.sh
nginx:
build: nginx
container_name: ng_dev
image: img/ng_dev
ports:
- "8001:8001"
volumes:
- ./nginx/dev_api.conf:/etc/nginx/conf.d/api.conf
- .:/usr/django/app/
- ./static:/static
depends_on:
- web
links:
- web:web
db:
image: postgres:latest
container_name: pq01
ports:
- "5432:5432"
redis:
image: redis:latest
container_name: rd01
command: redis-server
ports:
- "8004:8004"
celery:
build: .
container_name: cl01
command: celery worker --app=myapp.celery
volumes:
- .:/usr/django/app/
links:
- db
- redis

我有一个错误:

cl01      | User information: uid=0 euid=0 gid=0 egid=0
cl01      | 
cl01      |   uid=uid, euid=euid, gid=gid, egid=egid,
cl01      | [2018-07-31 16:40:00,207: ERROR/MainProcess] consumer: Cannot connect to redis://redis:8004/0: Error 111 connecting to redis:8004. Connection refused..
cl01      | Trying again in 2.00 seconds...
cl01      | 
cl01      | [2018-07-31 16:40:02,211: ERROR/MainProcess] consumer: Cannot connect to redis://redis:8004/0: Error 111 connecting to redis:8004. Connection refused..
cl01      | Trying again in 4.00 seconds...
cl01      | 
cl01      | [2018-07-31 16:40:06,217: ERROR/MainProcess] consumer: Cannot connect to redis://redis:8004/0: Error 111 connecting to redis:8004. Connection refused..
cl01      | Trying again in 6.00 seconds...

我不明白为什么它没有连接。我的设置文件项目

CELERY_BROKER_URL = 'redis://redis:8004/0'
CELERY_RESULT_BACKEND = 'redis://redis:8004/0'

一切看起来都很好,但可能在某些文件中我没有添加一些设置。请帮我解决这个问题

我认为端口映射导致了问题,因此,将docker-compose.dev文件中的redis设置更改为(删除了ports选项(

redis:
image: redis:latest
container_name: rd01
command: redis-server

以及在您的settings.py

CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

除非您在本地环境中使用端口,否则您不必映射端口

相关内容

  • 没有找到相关文章

最新更新