Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis+Docker+Django - Error 111 Connection Refused

I'm trying to use Redis as a broker for Celery for my Django project that uses Docker Compose. I can't figure out what exactly I've done wrong, but despite the fact that the console log messages are telling me that Redis is running and accepting connections (and indeed, when I do docker ps, I can see the container running), I still get an error about the connection being refused. I even did

docker exec -it <redis_container_name> redis-cli
ping

and saw that the response was PONG.

Here are the Celery settings in my settings.py:

BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True
CELERY_TIMEZONE = "UTC"

Here are the Redis container settings in my docker-compose.yml:

redis:
    image: redis
    ports:
        - "6379:6379"

I remembered to link the redis container with my web container as well. I can start up the server just fine, but I get the connection refused error when I try to upload anything to the site. What exactly is going wrong?

EDIT: I remembered to use VBoxManage to port forward such that I can go to my browser and access my site at localhost:8000, so it doesn't seem like I need to use the VM's IP instead of localhost for my settings.py.

EDIT 2: If I replace localhost in the settings with either the IP address of the docker-machine VM or the IP address of the Redis container, then what happens is that I really quickly get a false success message on my website when I upload a file, but then nothing actually gets uploaded. The underlying upload function, insertIntoDatabase(), uses delay.

like image 589
Dan K Avatar asked Feb 09 '23 00:02

Dan K


1 Answers

I just had similar problem due to updating Celery from v3.1 to v4 and according to this tutorial it was needed to change BROKER_URL to CELERY_BROKER_URL in the settings.py

settings.py part

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

docker-compose.yml part

version: '2'
services:
  web:
    container_name: django-container
    *******
    other options
    *******

    depends_on:
      - cache
      - db

  cache:
    container_name: redis-container
    restart: always
    image: redis:latest
like image 76
TitanFighter Avatar answered Feb 13 '23 02:02

TitanFighter