简体   繁体   English

使用docker-compose时,节点应用程序无法连接到Redis

[英]While using docker-compose, node application fails to connect to Redis

I am running Ubuntu and trying to setup dev environment for node app, this is my docker-compose.yml : 我正在运行Ubuntu并尝试为节点应用程序设置开发环境,这是我docker-compose.yml

version: "2"
services:
  redis:
    image: redis
    ports:
     - "6381:6379"
    volumes:
      - /var/lib/thinklog-queue:/data
  activity-service:
    build:
      context: ./activity-service
    command: yarn dev
    ports:
      - "3001:3001"
    environment:
      REDIS_QUEUE_URL: "redis://redis:6381"
    depends_on:
      - redis

and Dockerfile for the node app looks like this: 节点应用程序的Dockerfile如下所示:

FROM node:9
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
RUN yarn install
COPY . .
EXPOSE 4000
CMD ["yarn", "start"]

I extend Redis connection string from env variable. 我从环境变量扩展Redis连接字符串。

and when I run docker-compose build && docker-compose up inside logs I get this output from the node app: 当我在日志中运行docker-compose build && docker-compose up ,我从节点应用程序获取以下输出:

activity-service_1 | activity-service_1 | Error: Redis connection to redis:6381 failed - connect ECONNREFUSED 172.20.0.2:6381 错误:Redis与Redis的连接:6381失败-连接ECONNREFUSED 172.20.0.2:6381

It appears that setup is right however there is something blocking intern connections to Redis. 似乎安装正确,但是有一些东西阻止了到Redis的内部连接。 (inside this docker compose network) (在这个Docker撰写网络内部)

Connections to this Redis instance from outside, eg other shell session works just fine: 从外部连接到此Redis实例,例如其他shell会话就可以了:

kuno@dell:~/code/THINKLOG/activity-service$ redis-cli -p 6381
127.0.0.1:6381> ping
PONG

I also tried mounting custom config like so (added new item to volume arrays for Redis inside docker-compose.yml ): 我还尝试像这样挂载自定义配置(在docker-compose.yml内的Redis卷阵列中添加了新项目):

  - ./redis.conf:/usr/local/etc/redis/redis.conf

which has bind line commented out as well as modifying it to be like so (the last address is one that docker-compose uses internally): 其中已注释掉绑定行并对其进行了修改(最后一个地址是docker-compose内部使用的地址):

bind 192.168.1.100 10.0.0.1 172.20.0.2

Should I also mount in custom Redis configuration and configure Redis to allow connections within docker network? 我是否还应该挂载自定义Redis配置并配置Redis以允许docker网络内的连接?

在URL REDIS_QUEUE_URL: "redis://redis:6379"端口更改为6379 REDIS_QUEUE_URL: "redis://redis:6379" 6381是主机IP上的端口,而不是redis容器端口。

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

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