title: Docker can't connect to redis from another service
body: <p>I can't figure out how to connect to my <code>redis</code> service from my <code>app</code> service. Using DDocker version 18.03.1-ce, build 9ee9f40ocker for Mac.</p>
  <p>I've tried connecting the various ways I've found on similar questions:</p>
  <pre><code>const client = redis.createClient({ host: 'localhost', port: 6379});
  
  const client = redis.createClient({ host: 'redis', port: 6379});
  
  const client = redis.createClient('redis://redis:6379');
  
  const client = redis.createClient('redis', 6379); // and reversed args
  </code></pre>
  <p>I always get some form of:</p>
  <blockquote>
  <p>Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379</p>
  <p>Error: Redis connection to redis:6379 failed - connect ECONNREFUSED 172.20.0.2:6379</p>
  </blockquote>
  <p><strong>Docker containers</strong></p>
  <pre><code>$ docker ps
  CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS                    NAMES
  0fd798d58561        app_app         &quot;pm2-runtime start e…&quot;   2 seconds ago        Up 7 seconds                                 app
  65d148e498f7        app_redis       &quot;docker-entrypoint.s…&quot;   About a minute ago   Up 8 seconds        0.0.0.0:6379-&gt;6379/tcp   redis
  </code></pre>
  <p><strong>Redis works:</strong></p>
  <pre><code>$ docker exec -it redis /bin/bash
  root@65d148e498f7:/data# redis-cli ping
  PONG
  </code></pre>
  <p><strong>Redis Dockerfile</strong> (pretty simple)</p>
  <pre><code>FROM redis:4.0.9
  COPY redis.conf /usr/local/etc/redis/redis.conf
  CMD [&quot;redis-server&quot;, &quot;/usr/local/etc/redis/redis.conf&quot;]
  </code></pre>
  <p><strong>app Dockerfile</strong></p>
  <pre><code>FROM node:10.3.0-slim
  RUN mkdir -p /app
  COPY src/* /app/
  CMD [&quot;pm2-runtime&quot;, &quot;start&quot;, &quot;/app/ecosystem.config.js&quot;]
  </code></pre>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: &quot;3&quot;
  services:
    redis:
      build: ./redis/
      container_name: redis
      restart: unless-stopped
      ports:
        - &quot;6379:6379&quot;
      expose:
        - &quot;6379&quot;
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
      environment:
        - 'API_PORT=6379'
        - 'NODE_ENV=production'
    app:
      depends_on:
        - redis
      build: ./app/
      container_name: app
      restart: unless-stopped
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - /app/node_modules
      environment:
        - 'NODE_ENV=production'
  </code></pre>