5

I have a Django project that I am using with memcached and Docker. When I use sudo docker-compose up in development I'd like the entire cache to be cleared. Rather than disabling caching wholesale while in development mode, is there a way to run cache.clear() as noted in this question on each re-run of sudo docker-compose up?

I am not sure whether this should go in:

  1. docker-entrypoint.sh
  2. Dockerfile
  3. docker-compose.yml
  4. Somewhere else?

docker-compose.yml:

version: "3"
services:
  redis:
    image: "redis:alpine"
    command: "redis-server --requirepass ${REDISPASS} --bind 0.0.0.0"
    ports:
      - '6379:6379'
  memcached:
    image: "memcached:latest"
    ports:
      - '11211:11211'
  nginx:
      image: nginx:latest
      volumes:
        - ./configuration/nginx/conf.d/:/etc/nginx/conf.d
        - ./configuration/nginx/realtime:/etc/nginx/realtime
        - ./static_cdn/static_root/static:/static
      ports:
        - 80:80
        - 443:443      
      depends_on:
        - app_main
        - app_async_app1
        - app_async_app2
  app_main:
      command: "djangoAppName.settings.prod ${SECRET_KEY} 1 ${DB_PASS}     ${REDISPASS}"      
      image: "django_image_name"
      ports:
        - 0003:0003
      volumes:
        - ./static_cdn/static_root/:/static_cdn/
      depends_on:
        - redis
        - memcached
 app_async_app2:
      command: "djangoAppName.settings.prod ${SECRET_KEY} 2 ${DB_PASS} ${REDISPASS}"      
      image: "django_image_name"
      ports:
        - 0002:0002
      depends_on:
        - redis
        - memcached
        - app_main
  app_async_app1:
      command: "djangoAppName.settings.prod ${SECRET_KEY} 3 ${DB_PASS} ${REDISPASS}"    
      image: "django_image_name"
      depends_on:
        - redis
        - memcached
        - app_main
      ports:
        - 0001:0001
  react:
      command: "npm run"
      image: "django_image_name"
      volumes:
        - ./static_cdn/static_root/:/static_cdn/
      depends_on:
        - memcached
        - app_main
Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • It should be part of Dockerfile's CMD but it depends on how you've organised your docker config. I believe alternatively it can also go in `docker-compose.yml`'s `command` key. If you post your setup it'd be easier to recommend where to put it based on it. – Nobilis Mar 07 '19 at 14:42
  • Thanks @Nobilis. Added .yml. Let me know if you think that is all that is needed to clear the cache! Thanks for the help. – Scott Skiles Mar 07 '19 at 14:54

1 Answers1

4

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 BaseCommand
from django.core.cache import cache

class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        cache.clear()
        self.stdout.write('Cleared cache\n')
Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
Nobilis
  • 7,310
  • 1
  • 33
  • 67