简体   繁体   中英

Redis docker-compose with Nodejs - connect ENOENT

I am trying to containerize my whole developer workflow using docker-compose . Problem I am facing is with redis contianer. My worker container is unable to connect to redis. I trie different solution from stackoverflow like:

Setting up node with redis using docker-compose

Access redis locally on docker - docker compose

nodejs, redis, npm and docker-compose setup

Docker-compose, anyway to specify a redis.conf file?

How to connect to a Redis container using Docker Compose?

And many others also from github, tried different examples but no luck.

Here is my docker-compose file:

version: "3.6"

services:
  redis_db:
    # image: bitnami/redis:latest # tried different image also
    image: redis
    container_name: rme_redis
    network_mode: host # tried networks also
    # command: ['redis-server', '/redis.conf']
    # volumes:
    #   - ./docker/redis.conf:/redis.conf
    # hostname: redis_db
    # networks: 
    #   - rme
    # environment:
    #   - ALLOW_EMPTY_PASSWORD=yes
      # - REDIS_PASSWORD="redis"
    ports:
      - 6379:6379
    # networks:
    #   node_net:
    #     ipv4_address: 172.28.1.4

  worker:
    build: worker
    image: worker
    container_name: rme_worker
    command: node worker.js
    # networks:
    #   - rme
    # network_mode: host
    environment: 
      - REDIS_URL="redis://redis_db:6379" # localhost when netowk_mode is host
    volumes:
      - ./worker:/app
    # links:
    #   - redis_db
    depends_on:
      - redis_db

Error I am getting is:

Error: Redis connection to "redis://redis_db:6379" failed - connect ENOENT "redis://redis_db:6379"
     at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
   errno: -2,
   code: 'ENOENT',
   syscall: 'connect',
   address: '"redis://redis_db:6379"'
 }

Operating system: macOS

Docker: Docker Desktop

Edit: Found a solution using host and port in js code

const redisClient = redis.createClient({
  host: process.env.REDIS_HOST,
  port: parseInt(process.env.REDIS_PORT)
})
# docker-compose.yml
version: "3.6"
services:
  redis_db:
    image: redis
    # only needed to directly access Redis from host
    ports:
      - 6379:6379
  worker:
    build: worker
    environment: 
      - REDIS_HOST=redis_db
      - REDIS_PORT=6379
    depends_on:
      - redis_db

If you find solution using redis connection string do share.

Thanks for your help

Setting network_mode: host disables Docker networking for a specific container. It's only necessary in some unusual situations where a container doesn't listen on a fixed port number or where you're trying to use a container to manage the host's network setup.

In your case, since the Redis database is disabling Docker networking, the application container can't reach it by name. Removing all of the network_mode: host options should address this.

Networking in Compose describes the overall network setup. Of note, Compose will create a default network for you, and containers will be reachable using their Compose service names. You shouldn't normally need to specify custom networks: options, explicitly provide a container_name: , set a hostname: , provide archaic links: , or set network_mode: .

You should be able to successfully trim the docker-compose.yml file you show down to:

version: "3.6"
services:
  redis_db:
    image: redis
    # only needed to directly access Redis from host
    ports:
      - 6379:6379
  worker:
    build: worker
    environment: 
      - REDIS_URL="redis://redis_db:6379"
    # `docker-compose up worker` also starts Redis
    # Not strictly needed for networking
    depends_on:
      - redis_db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM