title: nodejs container won't connect to redis container docker-compose
body: <p>I have an express API running in a node container and a redis container.
  When attempting to connect the node container to redis I am greeted with the following error.</p>
  <pre><code>api_1    | events.js:291
  api_1    |       throw er; // Unhandled 'error' event
  api_1    |       ^
  api_1    |
  api_1    | Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
  api_1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
  api_1    | Emitted 'error' event on RedisAdapter instance at:
  api_1    |     at RedisClient.onError (/usr/src/api/node_modules/socket.io-redis/dist/index.js:65:22)
  api_1    |     at RedisClient.emit (events.js:314:20)
  api_1    |     at RedisClient.on_error (/usr/src/api/node_modules/redis/index.js:342:14)
  api_1    |     at Socket.&lt;anonymous&gt; (/usr/src/api/node_modules/redis/index.js:223:14)
  api_1    |     at Socket.emit (events.js:314:20)
  api_1    |     at emitErrorNT (internal/streams/destroy.js:92:8)
  api_1    |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
  api_1    |     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  api_1    |   errno: 'ECONNREFUSED',
  api_1    |   code: 'ECONNREFUSED',
  api_1    |   syscall: 'connect',
  api_1    |   address: '127.0.0.1',
  api_1    |   port: 6379
  api_1    | }
  </code></pre>
  <p>Here the code that is establishing the connection:</p>
  <pre><code>const redisClient = require(&quot;redis&quot;).createClient({host: process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost'});
  </code></pre>
  <p>I have tried changing 'localhost' to &quot;redis&quot; to &quot;redis://redis:6379&quot;. Nothing works.</p>
  <p>As I understand it &quot;localhost&quot; in the context of a container refers to its own internal network, however both nodejs and redis are running on the same network which I've specified in my docker compose file.</p>
  <p>Docker compose snippet</p>
  <pre><code>    api: 
          build: ./api
          ports: 
              - &quot;3004:3004&quot;
          volumes:
              - type: bind
                source: ./api
                target: /usr/src/api
          working_dir: /usr/src/api
          depends_on: 
              - redis
          networks:
              - backend
  
      redis: 
          image: &quot;redis:latest&quot;
          ports: 
              - &quot;6379:6379&quot;
          hostname: redis
          networks:
              - backend
      ```
  
  What could this be. All the &quot;solutions&quot; that I've managed to find through research don't work for me.
  </code></pre>