title: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 nodejs
body: <pre><code>LT027296-Mac:~$ docker images
  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  mongo               latest              394204d45d87        3 weeks ago         410MB
  redis               latest              a55fbf438dfd        4 weeks ago         95MB
  nginx               latest              2bcb04bdb83f        4 weeks ago         109MB
  bitnami/mysql       latest              c5c056b8435c        3 months ago        287MB
  LT027296-Mac:~$ docker run --name some-redis -d redis
  15e126e26ea452b2b8c2933c549a15d74bb49aece1fe8b5e4b746e67bced6c20
  LTB0207296-Mac:~ b0207296$ docker ps
  CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
  15e126e26ea4        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp            some-redis
  LT027296-Mac:~$ 
  </code></pre>
  
  <p>Hi 
  I am learning redis  tutorial from below link 
  <a href="https://medium.com/tech-tajawal/introduction-to-caching-redis-node-js-e477eb969eab" rel="nofollow noreferrer">https://medium.com/tech-tajawal/introduction-to-caching-redis-node-js-e477eb969eab</a></p>
  
  <p>I do the following steps</p>
  
  <ol>
  <li>Download redis image from docker</li>
  <li>then run container (as shown above)</li>
  <li>Then I run my code</li>
  </ol>
  
  <pre><code>const express = require('express')
  const fetch = require("node-fetch");
  const redis = require('redis')
  
  // create express application instance
  const app = express()
  
  // create and connect redis client to local instance.
  const client = redis.createClient()
  
  // echo redis errors to the console
  client.on('error', (err) =&gt; {
      console.log("Error " + err)
  });
  
  // get photos list
  app.get('/photos', (req, res) =&gt; {
    // key to store results in Redis store
    const photosRedisKey = 'user:photos';
  
    // Try fetching the result from Redis first in case we have it cached
    return client.get(photosRedisKey, (err, photos) =&gt; {
  
      // If that key exists in Redis store
      if (photos) {
  
        return res.json({ source: 'cache', data: JSON.parse(photos) })
  
      } else { // Key does not exist in Redis store
  
        // Fetch directly from remote api
        fetch('https://jsonplaceholder.typicode.com/photos')
          .then(response =&gt; response.json())
          .then(photos =&gt; {
  
          // Save the  API response in Redis store,  data expire time in 3600 seconds, it means one hour
          client.setex(photosRedisKey, 3600, JSON.stringify(photos))
  
          // Send JSON response to client
          return res.json({ source: 'api', data: photos })
  
        })
          .catch(error =&gt; {
          // log error message
          console.log(error)
          // send error to the client
          return res.json(error.toString())
        })
      }
    });
  });
  
  // start express server at 3000 port
  app.listen(3000, () =&gt; {
    console.log('Server listening on port: ', 3000)
  });
  </code></pre>
  
  <p>I am getting this error</p>
  
  <blockquote>
    <p>Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379</p>
  </blockquote>