Warm tip: This article is reproduced from serverfault.com, please click

Error when connecting to redis docker container

发布于 2020-11-28 23:20:09

I have an error where I cannot connect from my Go application to my redis container. It was working for at least 2 weeks, until I restarted my pc, I don't see what could have changed, and I ensured that no other containers or processes are using the same 6379 port.

My error:

panic: failed to load incr lua script: EOF

goroutine 1 [running]:
code/website.connectToRedisLimiterDatabase(0x0, 0x0)

I can connect into the redis container via my cli:

//exec inside
docker exec -it container-name redis-cli

// set value
set name "test"

// get value
get name
// shows test

Here is where I get the error in my go code:

redisLimiter "github.com/ulule/limiter/v3/drivers/store/redis"
redisSessions "github.com/rbcervilla/redisstore/v8"

// RedisLimiterInstance contains the Redis limiter client and store objects
type RedisLimiterInstance struct {
    Client redisLimiter.Client
    Store  limiter.Store
}

// RedisSessionInstance contains the Redis session client and store objects
type RedisSessionInstance struct {
    Client *redis.Client
    Store  *redisSessions.RedisStore
}

var redisLimiterInstance RedisLimiterInstance
var redisSessionInstance RedisSessionInstance


func connectToRedisLimiterDatabase() error {
    redisLimiterClient := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })

    store, err := redisLimiter.NewStoreWithOptions(redisLimiterClient, limiter.StoreOptions{
        Prefix:   "rate_limiter_",
        MaxRetry: 3,
    })
    if err != nil {
        panic(err)
    }
    // panic: failed to load incr lua script: EOF
    redisLimiterInstance = RedisLimiterInstance{
        Client: redisLimiterClient,
        Store:  store,
    }
    return nil
}

func connectToRedisSessionDatabase() error {
    redisSessionClient := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
    // New default RedisStore
    store, err := redisSessions.NewRedisStore(context.Background(), redisSessionClient)
    if err != nil {
        log.Fatal("failed to create redis store: ", err)
    }

    store.KeyPrefix("session_")
    store.Options(sessions.Options{
        Path: "/",
        MaxAge:   86400 * 7,
        HttpOnly: false,

    })

    redisSessionInstance = RedisSessionInstance{
        Client: redisSessionClient,
        Store:  store,
    }
    return nil
}

Via the docker desktop the container shows:

Configuration loaded

Running mode=standalone, port=6379.

Server initialized

Ready to accept connections

My conf file is just:

bind 127.0.0.1
port 6379

and the docker file itself is:

FROM redis:6.0.9

COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

Any thoughts?

Questioner
Steven
Viewed
0
Burak Serdar 2020-11-29 11:41:21

Binding to the address 127.0.0.1 in the redis container would only allow connections from within the container. Either remove the line or bind to 0.0.0.0 so redis can bind to all interfaces instead of just the loopback.