How to connect django to docker redis container?

I am trying to connect django to docker redis container

Here is my docker file

  • Nginx + php-fpm: Bad gateway only when xdebug server is running
  • How to enable SCM polling with the Jenkins Pipeline plugin
  • Replacing a particular instance on AWS Elastic BeanStalk?
  • Node error ENOSPC when running pm2 with docker-compose
  • How to install docker-compose on Windows
  • Spring Boot can't read application.properties in Docker
  • FROM        ubuntu:14.04
    RUN         apt-get update && apt-get install -y redis-server
    EXPOSE      6379
    ENTRYPOINT  ["/usr/bin/redis-server"]
    

    Here is the result of docker ps -a

    4f7eaeb2761b        /redis          "/usr/bin/redis-serve"   16 hours ago        Up 16 hours               6379/tcp            redis
    

    Here is a quick sanity check that redis is working inside docker container

     docker exec -ti redis bash
    
     root@4f7eaeb2761b:/# redis-cli ping
     PONG
     root@4f7eaeb2761b:/# redis-cli
     127.0.0.1:6379> exit
    

    Here is my Django settings.py

    CACHES = {
        'default': {
            'BACKEND': 'redis_cache.RedisCache',
            'LOCATION': 'localhost:6379',
        },
    }
    

    Here is my view

    from django.shortcuts import render
    from django.template import loader
    from django.http import HttpResponse
    from django.views.decorators.cache import cache_page
    
    @cache_page(60 * 15)
    def index(request):
        template = loader.get_template('./index.html')
    
        return HttpResponse(template.render())
    

    Here is alternative redis access

    import redis
    
    def index(request):
        r = redis.StrictRedis(host='localhost', port=6379, db=0)
        print r # this line doesn't cause error
        r.set('foo', 'bar') # this line cause error
    
        template = loader.get_template('./index.html')
    
        return HttpResponse(template.render())
    

    I verified that everything work without the @cache_page decorator

    When I used decorator I am getting

    Error 61 connecting to localhost:6379. Connection refused.
    

    I am not sure how do I expose the docker container besides setting Expose port, any help would be appreciated

    Thanks

  • Cannot increase size for docker container - Ubuntu 16.04
  • How to add module to Wildfly using CLI
  • Shell variables in dockerfile missing in docker image labels
  • Using docker-compose to set containers timezones
  • Docker jenkins container, host key verification failed
  • Unable to push my docker to Bluemix - Login error
  • One Solution collect form web for “How to connect django to docker redis container?”

    The thing to understand here is that the container exposed ports != system exposed ports.

    The Docker container for redis is exposing the port 6379 from the container — that is not the same port in the host system.

    Assuming you’re running docker with:

    docker run -ti redis bash
    

    By default, Docker will choose a random port in the host to bind to the port the container is exposing. You can check the host ports with the command (will show nothing if no port is exposed):

    docker port CONTAINER_ID
    

    Instead, you’ll want to run it like this:

    docker run -ti redis bash -p 6379:6379
    

    This tells Docker to link the 6379 host port to the 6379 container port. Then docker port will show you something like this:

    $ docker port CONTAINER_ID
    6379/tcp -> 0.0.0.0:6379
    

    You can also use a docker-compose.yml file to configure this.

    Read more:

    • Brief Primer on Docker networking rules
    • Docker Compose documentation
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.