Docker Compose - Redis container refusing connection to Node container

雨燕双飞 提交于 2021-01-29 09:02:24

问题


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:


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



来源:https://stackoverflow.com/questions/60216161/docker-compose-redis-container-refusing-connection-to-node-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!