从Docker Container连接到现有的外部Redis

suzh9iv8  于 2022-10-08  发布在  Docker
关注(0)|答案(2)|浏览(178)

我有一个连接到外部Redis服务器的小Fastify应用程序。我使用的是Fastify-redis NPM包(它在引擎盖下使用ioredis)。Fastify-redis正在使用rediss://格式进行连接

REDIS_URL='rediss://:xxxxyyyyxxxyxxxyxyxyxyxyxxy@blahblah-dev.redis.cache.windows.net:6380'
const Fastify = require('fastify')
const fastifyRedis = require('@fastify/redis')

     fastify = Fastify({ logger: true, pluginTimeout: 50000 })
        fastify.register(fastifyRedis, {
          url: process.env.REDIS_URL,
          enableAutoPipelining: true,
        })

使用NPM Start在本地运行时,这一切都运行得很好。但是,当我停靠它时,我收到一个错误,看起来像是由于无法连接到Redis示例而导致的

redisutils_1  | > node index.js
redisutils_1  | 
redisutils_1  | /usr/src/node_modules/ioredis/built/redis/event_handler.js:175
redisutils_1  |                     self.flushQueue(new errors_1.MaxRetriesPerRequestError(maxRetriesPerRequest));
redisutils_1  |                                     ^
redisutils_1  | 
redisutils_1  | MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
redisutils_1  |     at Socket.<anonymous> (/usr/src/node_modules/ioredis/built/redis/event_handler.js:175:37)
redisutils_1  |     at Object.onceWrapper (node:events:628:26)
redisutils_1  |     at Socket.emit (node:events:513:28)
redisutils_1  |     at TCP.<anonymous> (node:net:313:12)
redisutils_1  | 
redisutils_1  | Node.js v18.9.0

我错过了什么?

hyrbngr7

hyrbngr71#

您主要需要使用--network host运行您的容器,因为您的容器运行在私有网络内,并且无法到达您的网络与任何外部服务进行通信。

jvidinwx

jvidinwx2#

我发现了这个问题。

两件事。

1.(愚蠢的那个)确保所有环境变量设置(如rediss URL)都已实际设置!
1.Redis服务器因证书问题拒绝连接。我用的是牛眼瘦身,不得不换成阿尔卑斯山,再加一个台阶来解决这个问题。

FROM node:alpine
    RUN apk update && apk add ca-certificates && update-ca-certificates
    WORKDIR /usr/src/app
    COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
    RUN npm install --production --silent && mv node_modules ../
    COPY . .
    EXPOSE 3000
    RUN chown -R node /usr/src/app
    USER node
    CMD ["npm", "start"]

RUN apk update && apk add ca-certificates && update-ca-certificates

相关问题