【问题标题】:Docker-compose deploying redis but app.js refuses connectionDocker-compose 部署 redis 但 app.js 拒绝连接
【发布时间】:2019-10-19 13:48:24
【问题描述】:

我正在尝试将 nodejs 应用程序容器化。该应用程序在我手动安装并运行 redis 的节点容器上运行良好,但是当我尝试使用我的 docker-compose 文件在容器中运行该应用程序时,出现错误:

“错误错误:Redis 连接到 localhost:6379 失败 - 连接 ECONNREFUSED 127.0.0.1:6379”。

当我尝试执行 docker-compose up 时,我将在下面发布我的 docker-compose.yml 和 dockerfile 以及控制台日志。

FROM node:8-jessie

WORKDIR /var/api-console

COPY package*.json ./
RUN npm install

COPY . . /var/api-console/

RUN apt-get update
RUN apt-get install python

EXPOSE 3000
version: '3'
services:
  redis:
    image: redis
    ports:
      - "6379:6379"
    command:
      redis-server
    networks:
      - webnet
  app:
    build: ./
    volumes:
      - ./:/var/api-console
    ports:
      - 3000:3000
    command:
      node app.js
    networks:
      - webnet
networks:
  webnet:
AIDEVERSUSCATCH:api-console evan.dhillon$ docker-compose up
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Starting api-console_app_1   ... done
Starting api-console_redis_1 ... done
Attaching to api-console_app_1, api-console_redis_1
redis_1  | 1:C 04 Jun 2019 03:12:27.660 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 04 Jun 2019 03:12:27.660 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 04 Jun 2019 03:12:27.660 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 04 Jun 2019 03:12:27.661 * Running mode=standalone, port=6379.
redis_1  | 1:M 04 Jun 2019 03:12:27.661 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 04 Jun 2019 03:12:27.661 # Server initialized
redis_1  | 1:M 04 Jun 2019 03:12:27.661 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 04 Jun 2019 03:12:27.661 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 04 Jun 2019 03:12:27.661 * Ready to accept connections
app_1    | Express server listening on port 3000
app_1    | Error Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
app_1    | Error AbortError: Redis connection lost and command aborted. It might have been processed.
app_1    | events.js:183
app_1    |       throw er; // Unhandled 'error' event
app_1    |       ^
app_1    | 
app_1    | Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
app_1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
api-console_app_1 exited with code 1

【问题讨论】:

    标签: node.js docker redis docker-compose


    【解决方案1】:

    日志中的这一行解释了连接失败

    Redis 连接到 localhost:6379 失败

    节点应用程序期望 redis 位于本地主机上,但事实并非如此。

    您可以通过环境变量将redis容器的地址提供给您的节点应用程序。

    还在应用服务中添加一个depends_on,使其等待redis服务。修改后的compose文件如下

    version: '3'
    services:
      redis:
        image: redis
        ports:
          - "6379:6379"
        command:
          redis-server
        networks:
          - webnet
      app:
        build: ./
        volumes:
          - ./:/var/api-console
        ports:
          - 3000:3000
        depends_on:
          - redis
        environment:
          redis_server_addr: redis   
          #The dependent service address is set in environment variable which you can use in your app to connect
        command:
          node app.js
        networks:
          - webnet
    networks:
    

    【讨论】:

    • 另外,您需要更改应用程序配置以连接到redis,而不是localhost,而是使用主机作为redis
    • 我尝试使用上面发布的 docker-compose 运行,并在 config.json 中将主机更改为“redis”,但仍然出现相同的错误。应用程序本身内部有什么需要修改的吗?
    • 在你的应用程序代码中有这样的东西 const port = 6379 const host = process.env.redis_server_addr; var client = redis.createClient(port, host);
    • 感谢您的帮助。我添加了这些变量并修复了两个调用以匹配 app.js 中的 redis.createClient(port, host)。仍然得到同样的错误。还有其他解决方案可以尝试吗? @asolanki
    • 我解决了这个问题,在进行更改后,当我运行 docker-compose up 时,它实际上并没有重建 nodejs 项目,而是从缓存中绘制。运行 docker-compose 构建后,config.json 用 redis 更新,问题解决了
    猜你喜欢
    • 2022-01-03
    • 2022-08-05
    • 2017-05-09
    • 2017-07-10
    • 2016-02-14
    • 2021-06-27
    • 2021-08-29
    • 1970-01-01
    • 2017-04-12
    相关资源
    最近更新 更多