How to clear cache for Django with Docker? How to clear cache for Django with Docker? docker docker

How to clear cache for Django with Docker?


As per this answer you can add a service that's executed before the memcached service that clears out the cache. As it looks like you're using Linux Alpine, you can add this service to docker-compose.yml:

clearcache:    command: [sh, -c, "python manage.py clear_cache"] 

and then add to memcached:

memcached:    ...    depends_on:     - clearcache

There's also an example in there that does it in the same command and not relying on a separate service (though personally I don't like that).

For the cache clearing command, this answer has some useful discussion and posts.

clear_cache.py:

from django.core.management.base import BaseCommandfrom django.core.cache import cacheclass Command(BaseCommand):    def handle(self, *args, **kwargs):        cache.clear()        self.stdout.write('Cleared cache\n')