Connecting to existing external Redis from Docker Container

Viewed 22

I have a small fastify app that connects to an external Redis server. I am using the fastify-redis npm package (which uses ioredis under the hood). fastify-redis is connecting using rediss:// format

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,
        }) 

This all works fine run locally using npm start. When I dockerise it, though, I get an error, which looks like it is caused by not being able to connect to the Redis instance

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

What have I missed?

2 Answers

you mostly will need to run your container with --network host as your container runnning inside private network and can't reach your network to communicate with any external services.

I discovered the issue.

2 things.

  1. (The dumb one) make sure any environment variable settings (like the rediss URL) are actually being set!
  2. The Redis server was refusing the connection due to certificate issues. I was using bullseye-slim and had to change to alpine and add a step to fix that
    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"]

Namely

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