简体   繁体   English

Redis docker-compose 与 Nodejs - 连接 ENOENT

[英]Redis docker-compose with Nodejs - connect ENOENT

I am trying to containerize my whole developer workflow using docker-compose .我正在尝试使用docker-compose将我的整个开发人员工作流程容器化。 Problem I am facing is with redis contianer.我面临的问题是redis My worker container is unable to connect to redis.我的工作容器无法连接到 redis。 I trie different solution from stackoverflow like:我尝试了与 stackoverflow 不同的解决方案,例如:

Setting up node with redis using docker-compose 使用 docker-compose 使用 redis 设置节点

Access redis locally on docker - docker compose 在 docker - docker 上本地访问 redis

nodejs, redis, npm and docker-compose setup nodejs,redis,npm 和 docker-compose 设置

Docker-compose, anyway to specify a redis.conf file? Docker-compose,无论如何要指定 redis.conf 文件?

How to connect to a Redis container using Docker Compose? 如何使用 Docker Compose 连接到 Redis 容器?

And many others also from github, tried different examples but no luck.还有许多其他人也来自 github,尝试了不同的示例但没有运气。

Here is my docker-compose file:这是我的 docker-compose 文件:

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操作系统:macOS

Docker: Docker Desktop Docker:Docker桌面

Edit: Found a solution using host and port in js code编辑:在 js 代码中找到使用主机和端口的解决方案

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.如果您找到使用 redis 连接字符串的解决方案,请分享。

Thanks for your help谢谢你的帮助

Setting network_mode: host disables Docker networking for a specific container.设置network_mode: host为特定容器禁用 Docker 网络。 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.在您的情况下,由于 Redis 数据库正在禁用 Docker 网络,因此应用程序容器无法通过名称访问它。 Removing all of the network_mode: host options should address this.删除所有network_mode: host选项应该可以解决这个问题。

Networking in Compose describes the overall network setup. Compose中的网络描述了整个网络设置。 Of note, Compose will create a default network for you, and containers will be reachable using their Compose service names.值得注意的是,Compose 将为您创建一个default网络,并且可以使用它们的 Compose 服务名称访问容器。 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: .您通常不需要指定自定义networks:选项、显式提供container_name: 、设置hostname: 、提供过时的links:或设置network_mode:

You should be able to successfully trim the docker-compose.yml file you show down to:您应该能够成功地将您显示的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_URL="redis://redis_db:6379"
    # `docker-compose up worker` also starts Redis
    # Not strictly needed for networking
    depends_on:
      - redis_db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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