title: Can't connect Redis server to nodejs, Docker compose
body: <p>I'm struggling to connect a redis deployment to my nodejs app. Of course locally without the use of docker, it works well, so I'm at odds as to whether this is an issue to do with my code, or the way I've set up my docker compose file</p>
  
  <p>Dockerfile: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>FROM node:8
  WORKDIR /app
  COPY package.json /app
  COPY . /app
  RUN npm install
  CMD ["npm", "start"]
  EXPOSE 3000</code></pre>
  </div>
  </div>
  </p>
  
  <p>docker-compose.yml</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>version: "3"
  services:
    web:
      container_name: web-container
      restart: always
      depends_on:
        - redis
      build: . 
      ports: 
      - "3000:3000"
      links: 
        - redis
    redis:
      container_name: redis-container
      image: "redis:latest"
      ports:
        - "6379:6379"
      volumes:
        - ./data:/data</code></pre>
  </div>
  </div>
  </p>
  
  <p>Redis Connection File (RedisService.js)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const redis         = require("redis");
  const client        = redis.createClient();
  const DbUtils       = require("../../db_utils");
  const {promisify}   = require("util");
  const getAsync      = promisify(client.get).bind(client);
  const existsAsync   = promisify(client.exists).bind(client);
  
  class RedisCache {
      constructor () {
          var connected;
          // * Initiliase the connection to redis server
          client.on("connect", () =&gt; {console.log(" Redis cache is ready"); connected = true;})
          client.on("error", (e) =&gt; {console.log("Redis cache error:\n" + e); connected = false;});
      }
  
      async setData (id, data) {
          // * Stringify data if it's an object
          data = data instanceof Object ? JSON.stringify(data) : data;
          client.set(id, data);
          return true;
      }
  
      async getData (key) {
          return getAsync(key).then(data =&gt; {
              data = JSON.parse(data) instanceof Object ? JSON.parse(data) : data;
              return data; 
          })
      }
  
      async exists (key) {
          return existsAsync(key).then(bool =&gt; {
              return bool;
          })
      }
  
      // Returns status of redis cache
      async getStatus () {
          return this.connected;
      }
  }
  
  module.exports = new RedisCache();</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>ERROR</strong>
  Error: Redis connection to 127.0.0.11:6379 failed - connect ECONNREFUSED 127.0.0.11:6379</p>