0

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply, tried your solution but still same error Error: Redis connection to "redis://redis_db:6379" failed - connect ENOENT "redis://redis_db:6379", redis log * Ready to accept connections, operating system is macos and using docker-desktop.
Is your code trying to use REDIS_URL as a host name? (Does setting REDIS_URL=redis_db help, even though that's not a URL?)
No its url passed to redis library, redis.createClient({ url: process.env.REDIS_URL })

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.