Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have live reloading of lua scripts using openresty and docker?

I'm newish to LUA and want to practice some LUA scripting using nginx/openrestry.

Is there a workflow where I can use docker that runs openresty, and link my laptops filesystem to my docker container such that when I make a change to my lua script, I can quickly reload openrestry server so I can quickly see my lua changes take effect?

Any help or guidance would be appreciated.

like image 382
Blankman Avatar asked Jan 24 '26 12:01

Blankman


1 Answers

You can disable Lua code cache — https://github.com/openresty/lua-nginx-module#lua_code_cache — add lua_code_cache off inside the http or server directive block. This is not actually “hot reload”, it's more like php request lifecycle:

every request served by ngx_lua will run in a separate Lua VM instance

You can think of it as if the code is hot–reloaded on each request.

However, pay attention to this:

Please note however, that Lua code written inlined within nginx.conf [...] will not be updated

It means that you should move all your Lua code from the nginx config to Lua modules and only require them:

server {

  lua_code_cache off;

  location /foo {
    content_by_lua_block {
      -- OK, the module will be imported (recompiled) on each request
      require('mymodule').do_foo()
    }
  }

  location /bar {
    content_by_lua_block {
      -- Bad, this inlined code won't be reloaded unless nginx is reloaded.
      -- Move this code to a function inside a Lua module
      -- (e.g., `mymodule.lua`).
      local method = ngx.req.get_method()
      if method == 'GET' then
        -- handle GET
      elseif method == 'POST' then
        -- handle POST
      else
        return ngx.exit(ngx.HTTP_NOT_ALLOWED)
      end
    }
  }

}

Then you can mount your Lua code from the host to the container using --mount or --volume: https://docs.docker.com/storage/bind-mounts/

like image 157
Dmitry Meyer Avatar answered Jan 27 '26 02:01

Dmitry Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!