0

No matter what I try I can't seem to get my node app to connect to redis between containers within the same docker-compose yml config. I've seen a lot of similar questions but none of the answers seem to work.

I'm using official images in both cases, not building my own

I am putting "redis" as my host and setting it as hostname in my docker compose YML config

const client = redis.createClient({ host: "redis" });

in my redis.conf I am using bind 0.0.0.0

This what the console is printing out:

 Redis connection to redis:6379 failed - getaddrinfo ENOTFOUND redis redis:6379

 Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
port: 6379 
}

This is my docker-compose.yml

version: '3'
services:
  server:
    image: "node:10-alpine"
    working_dir: /usr/src/app
    user: "node"
    command: "npm start"
    volumes:
      - .:/usr/src/app
    ports:
      - '3000:3000'
      - '3001:3001'
      - '9229:9229' # Node debugging port
    environment:
      - IS_DOCKER=1
      - NODE_ENV=DEVELOPMENT
    depends_on:
      - db

  db:
    image: "redis:5.0-alpine"
    expose:
      - '6379'
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data:/data
    command:
      - redis-server
      - /usr/local/etc/redis/redis.conf
    hostname: redis

volumes:
  redis-data:

UPDATE

Here's my redis.conf, it's not much.

bind 0.0.0.0
appendonly yes
appendfilename "my_app.aof"
appendfsync always

UPDATE 2

Things I've noticed and tried

  • in my original setup, when I run docker inspect I can see they are both joined to the same network. when I exec.../bin/bash into the redis container I can successfully ping the server container but when I'm in the server container it can not ping the redis one.

  • network_mode: bridge -adding that to both containers does not work

I did get one baby step closer by trying out this:

server:
   network_mode: host
redis:
   network_mode: service:host

I'm on a Mac and in order to get host mode to work you need to do that. It does work in the sense that my server successfully connects to the redis container. However, hitting localhost:3000 does not work even though I'm forwarding the ports

1
  • I thought I figured it out for a moment using network_mode: host but a new problem happend where I could not reach localhost:3000 in my browser. I did see the app run and connect to redis successfully though Commented Feb 14, 2020 at 0:24

2 Answers 2

3
version: '3'
services:
  server:
    image: "node:10-alpine"
    #network_mode: bridge
    #links is necessary if you use network_mode: bridge
    #links: [redis]
    networks:
      - default
    working_dir: /usr/src/app
    user: "node"
    command: "npm start"
    volumes:
      - .:/usr/src/app
    ports:
      - '3000:3000'
      - '3001:3001'
      - '9229:9229' # Node debugging port
    environment:
      - IS_DOCKER=1
      - NODE_ENV=DEVELOPMENT
    depends_on:
      - redis

  redis:
    image: "redis:5.0-alpine"
    #container_name: redis
    #network_mode: bridge
    networks:
      - default
    expose:
      - '6379'
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data:/data
    command:
      - redis-server
      - /usr/local/etc/redis/redis.conf

volumes:
  redis-data:

networks:
  default:

Rename the container to the hostname you want to use: redis in your case instead of db.
To make it accessible over the docker network you will have to put them on the same network like above or use network_mode: bridge and links: [redis] instead.

Try this to test your network:
docker ps to get the current container id or running name from the server container
docker exec -it id/name /bin/sh
Now you have a shell inside server and should be able to resolve redis via:
ping redis or nc -zv redis 6379

Sign up to request clarification or add additional context in comments.

7 Comments

no luck. I've tried with and without the container_name as redis, also removing the hostname redis as well. I've tried not using my custom conf as well. nothing seems to work
You do not need container_name: or explicit links: for this to work. (The hostname: declaration is also ineffective.) The important change from the original docker-compose.yml file is renaming db to redis, to match the expected host name.
What @DavidMaze says is correct, but sometimes docker-compose can be tricky. Please check my updated answer to debug your issue
bridge mode doesn't work however (in default network mode) when I shell into the server container and ping redis I get this PING redis (172.18.0.2): 56 data bytes; ping: permission denied (are you root?) which is promising! redis is being mapped to the right IP! but it's just being denied by the redis container for some reason (or maybe the server container is blocking it?)
I believe this might have to do with using alpine. When I switched to the node:10 image I can successfully ping redis from within the node server container. But for some reason I still can't connect to it in the node app
|
0

For those who still getting this error i found that in new versions of redis - 4 and up you need to configure the client like this:

const client = createClient({
  socket: {
    host: host,
     port: process.env.REDIS_PORT,
   },
});

it solved my problem

then in docker compose file you don't need to specify ports

version: "3.4"
services:
  redis-server:
    image: "redis:latest"
    restart: always
  api:
    depends_on:
      - redis-server
    restart: always
    build: .
    ports:
      - "5000:5000"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.