gpt4 book ai didi

node.js - Node js无法连接到Redis Docker Centos 7

转载 作者:可可西里 更新时间:2023-11-01 11:14:07 29 4
gpt4 key购买 nike

我有 docker compose 使用 Mongo、Redis 和 Node js。Mongo 和 Redis 运行良好,问题是 Node js 无法连接到 Redis。

当我在带有 docker 的 ubuntu 笔记本电脑上进行测试时,它工作正常。但当我在服务器 (Centos 7) 中运行 docker 时不是。

我确定我的 redis 可以正常工作,因为当我进行 SSH 端口转发时它可以正常工作。我可以从我的 ubuntu 访问。

version: '2.1'

services:
aqua-server:
image: aqua-server
build: .
command: pm2-runtime process.yml
container_name: "aqua-server"
working_dir: /app
environment:
NODE_ENV: production
AQUA_MONGO_IP: 172.20.0.1
AQUA_MONGO_PORT: 3002
AQUA_MONGO_DATABASE: aqua-server
AQUA_EXPRESS_PORT: 3000
AQUA_REDIS_PORT: 3003
AQUA_REDIS_HOST: 172.20.0.1
volumes:
- .:/app/
ports:
- 3001:3000
links:
- mongodb
- redis
depends_on:
- mongodb
- redis
networks:
frontend:
ipv4_address: 172.20.0.2
mongodb:
image: mongo:4.2.0-bionic
container_name: "aqua-mongo"
environment:
MONGO_LOG_DIR: /dev/null
volumes:
- /home/sagara/DockerVolume/MongoLocal:/data/db
ports:
- 3002:27017
networks:
frontend:
ipv4_address: 172.20.0.3
redis:
image: redis:5.0.5-alpine
container_name: "aqua-redis"
ports:
- 3003:6379
networks:
frontend:
ipv4_address: 172.20.0.4
networks:
frontend:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/24

这是我在 Node js 上的 DockerFile

FROM node:10.16.0-alpine

WORKDIR /app

COPY . .

RUN npm install
RUN npm install pm2 -g

EXPOSE 3000

CMD pm2-runtime process.yml

这个过程.yml

apps:
- script: index.js
instances: 1
exec_mode: cluster
name: 'aqua-server'

这是我的日志 docker

aqua-server    | Error: Redis connection to 172.20.0.1:3003 failed - connect EHOSTUNREACH 172.20.0.1:3003
aqua-server | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
aqua-server | 2019-09-06T07:48:13: PM2 log: App name:aqua-server id:0 disconnected
aqua-server | 2019-09-06T07:48:13: PM2 log: App [aqua-server:0] exited with code [0] via signal [SIGINT]
aqua-server | 2019-09-06T07:48:13: PM2 log: App [aqua-server:0] starting in -cluster mode-
aqua-server | 2019-09-06T07:48:13: PM2 log: App [aqua-server:0] online

Redis 日志 Docker

aqua-redis     | 1:M 06 Sep 2019 07:47:12.512 * DB loaded from disk: 0.000 seconds
aqua-redis | 1:M 06 Sep 2019 07:47:12.512 * Ready to accept connections

Mongo 日志 Docker

aqua-mongo     | 2019-09-06T07:47:15.895+0000 I  NETWORK  [initandlisten] Listening on /tmp/mongodb-27017.sock
aqua-mongo | 2019-09-06T07:47:15.895+0000 I NETWORK [initandlisten] Listening on 0.0.0.0
aqua-mongo | 2019-09-06T07:47:15.895+0000 I NETWORK [initandlisten] waiting for connections on port 27017

最佳答案

出现该问题是因为您正尝试从 Docker 网络之外的 Ubuntu 进行访问。并且您的 Node 容器位于该网络内,因此它们的通信方式需要稍作修改才能使其正常工作。所以我将引用我的最佳实践方式:

version: '2.1'

services:
aqua-server:
image: aqua-server
build: .
command: pm2-runtime process.yml
container_name: "aqua-server"
working_dir: /app
environment:
NODE_ENV: production
AQUA_MONGO_IP: mongodb
AQUA_MONGO_PORT: 27017
AQUA_MONGO_DATABASE: aqua-server
AQUA_EXPRESS_PORT: 3000
AQUA_REDIS_PORT: 6379
AQUA_REDIS_HOST: redis
volumes:
- .:/app/
ports:
- 3001:3000
links:
- mongodb
- redis
depends_on:
- mongodb
- redis
networks:
frontend:
ipv4_address: 172.20.0.2
mongodb:
image: mongo:4.2.0-bionic
container_name: "aqua-mongo"
environment:
MONGO_LOG_DIR: /dev/null
volumes:
- /home/sagara/DockerVolume/MongoLocal:/data/db
ports:
- 3002:27017
networks:
frontend:
ipv4_address: 172.20.0.3
redis:
image: redis:5.0.5-alpine
container_name: "aqua-redis"
ports:
- 3003:6379
networks:
frontend:
ipv4_address: 172.20.0.4
networks:
frontend:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/24

这种方式可行,因为 Docker 会为您处理 DNS,而域名是服务名称,因此您可以在 docker-compose.yml 中看到上面,我已经改变了AQUA_MONGO_IPmongodbAQUA_REDIS_HOSTredis .我已经更改了 PORT同样,由那个 docker-compose.yml 创建的 Docker 容器将留在网络内,他们通过 PORT 进行通信他们暴露在他们的Dockerfile .所以在这种情况下,其他服务可以与 aqua-server 通信通过PORT=3000 , aqua-server可以与mongodb通信服务通过 PORT=27017 (此端口暴露在 mongodb , EXPOSE 27017 的 Dockerfile 中)

希望对您有所帮助!

关于node.js - Node js无法连接到Redis Docker Centos 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817940/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com