当前位置: 首页 > 知识库问答 >
问题:

Docker-compose部署redis但app.js拒绝连接

秦锐
2023-03-14

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

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

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

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

共有1个答案

须衡虑
2023-03-14

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

Redis连接到localhost:6379失败

节点应用程序希望redis在本地主机上,但它不在本地主机上。

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服务器,但未连接。但是,如果我猛击redis容器,我可以访问redis-cli。 docker-compose.yml看起来像这样 Dockerfile_redis 当我尝试使用此代码连接到Redis服务器时 它发出此警告 有谁知道如何将Redis容器连接到PHP容器? 问题答案: 你的问题 Docker Compose 为不同的服务创建了单独的dock

  • 问题内容: 我正在尝试设置一个docker- compose文件,该文件旨在用Supervisor替换运行多个进程(RQ worker,RQ仪表板和Flask应用程序)的单个Docker容器解决方案。 主机系统是Debian 8 Linux,我的样子如下(我删除了所有其他条目以减少错误源): “ rq-worker1”是Python RQ工作者,尝试通过本地主机和端口6379连接到Redis,但无

  • 问题内容: 我创建了一个小工具,过去它像一个魅力一样可以部署小型WordPress实例。看起来像这样: 但是当我现在启动它时(也许是因为docker更新到Docker 1.9.1版本,build ),它失败了 当我的猫的有项针对MySQL: 而且我能够ping通MariaDB服务器。 当我安装WordPress并多次重启后,MariaDB容器打印出来: 是哪个指示它正在运行,不是吗? 我如何获得W

  • 我是码头工人的新宝宝。我正在Spring Boot应用程序中使用docker。下面是我的档案。 application.properties文件: 问题的第一部分解决了:当我运行应用程序时,我得到以下错误: 1)我想要达到的- 希望为我的应用程序创建一个jar。 创建jar后,我要从jar创建映像 创建应用程序的映像后,我想推入docker集线器。 从其他服务器运行docker-compose.y

  • 我试图在docker compose中建立一个Spring Boot应用程序,该应用程序依赖于名为teste的MySQL数据库。发布docker compose up后,我得到: 我在Linux Mint上运行,我的docker compose版本是1.23.2,我的docker版本是18.09.0。 应用程序.属性 docker-compose.yml 和Dockerfile

  • 我正在使用另一个容器中的DockerizeSpring启动应用程序和redis。 我使用docker comush在同一个网络中运行两个容器,这是我的docker-compose.yml: 这是我的application.yml文件: 我也尝试了redis hots=redis,但结果是一样的。 我的问题是,我得到这个java.net.连接异常:拒绝连接(拒绝连接)”,即使容器在同一个网络中。 我