Search code examples
redislua

redis container how to load lua script from host


I am trying to execute a lua script against a redis instance running in a container. The code is very simple 2 liner example.

local foo = redis.call("ping")
return foo

As this file is on the host machine, how do i get it to execute inside docker exec ? Or should I install redis-cli locally to test it?


Solution

  • I think I understood what you want. You have a Lua script on your docker host that you want to load into Redis running inside a docker container without needing redis-cli on the host.

    So, start the official Redis in a container as a daemon:

    docker run --name some-redis -d redis
    

    Now, assuming your script is on the host in a file called script.lua, you want to load that into dockerised Redis:

    docker exec -i some-redis redis-cli -x script load < script.lua
    

    That will return a SHA sum - I got 93126620988a563ac9dd347d02a9cdbbbeaee3c1 - and you can then run the script with:

    docker exec -i some-redis redis-cli EVALSHA 93126620988a563ac9dd347d02a9cdbbbeaee3c1 0
    PONG
    

    If you want to automate the capture of the SHA sum and its subsequent use:

    sha=$(docker exec -i some-redis redis-cli -x script load < script.lua)
    

    Then:

    docker exec -i some-redis redis-cli EVALSHA $sha 0
    PONG