Docker环境下Django集成Celery时task.get()无法正常工作求助
Hey there! I’ve run into this exact same headache when setting up Celery with Django in Docker, so I totally get how frustrating this is. Let’s walk through the most common fixes for this scenario—where messages land in RabbitMQ but task.get() hangs or fails to return a result.
1. Your Celery Worker Isn’t Running (or Connected Properly)
Even if RabbitMQ gets the task message, if your Celery worker isn’t up and listening, the task will never execute. Let’s verify:
- First, check if your Celery worker container is running:
docker ps - If it’s running, pull the logs to confirm it connected to RabbitMQ successfully:
Look for lines likedocker logs <your-celery-worker-container-name>Connected to amqp://user:**@rabbitmq:5672//—if you don’t see this, your worker can’t reach the RabbitMQ service. - Double-check your Celery broker URL matches your Docker setup. In your
celery.pyor worker command, it should reference the RabbitMQ service name from yourdocker-compose.yml:app = Celery('myproject', broker='amqp://user:password@rabbitmq:5672//')
2. Mismatched Task Serialization Settings
Sometimes the task is sent, but the worker can’t deserialize it because Django and Celery use different serialization formats.
- Add these settings to your Django
settings.pyto standardize on JSON:CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' - Make sure your Celery worker is pulling these settings—if you start it with
celery -A myproject worker -l info, it should inherit them automatically if your Celery app is linked to Django correctly.
3. Missing or Unreachable Result Backend
When you call task.get(), Celery needs to fetch the task result from a backend. If you haven’t configured this, or the worker can’t reach it, get() will hang.
- Add a result backend (Redis is a reliable choice for Docker setups) to
settings.py:
Again,CELERY_RESULT_BACKEND = 'redis://redis:6379/0'redisshould match the service name in yourdocker-compose.yml. - Verify the Redis container is running and check worker logs for any connection errors to the backend.
4. Docker Network Isolation
Docker containers run in isolated networks by default. Ensure all your services (Django, Celery, RabbitMQ, Redis) are on the same network.
- In your
docker-compose.yml, group all services under a shared network:version: '3' services: web: build: . networks: - celery-network celery: build: . command: celery -A myproject worker -l info networks: - celery-network rabbitmq: image: rabbitmq:3-management networks: - celery-network redis: image: redis:alpine networks: - celery-network networks: celery-network: driver: bridge
5. Code Sync or Permission Issues (Local Dev)
If you’re using volume mounts for your code, the Celery worker might not pick up the latest task changes.
- Restart the Celery worker container after editing tasks:
docker-compose restart celery - Ensure the user inside the Celery container has permission to read your task files—adjust your Dockerfile’s
USERsetting or volume permissions if needed.
If you’re getting a specific error when calling task.get(), drop that detail and we can narrow it down further!
内容的提问来源于stack exchange,提问作者Ryan Skene

