I'd like to have a lean Docker image for nginx with the Lua module enabled. How can I create this based on Alpine linux?
6 Answers
Here is a Dockerfile
:
FROM alpine:3.6
RUN apk add --no-cache nginx-mod-http-lua
# Delete default config
RUN rm -r /etc/nginx/conf.d && rm /etc/nginx/nginx.conf
# Create folder for PID file
RUN mkdir -p /run/nginx
# Add our nginx conf
COPY ./nginx.conf /etc/nginx/nginx.conf
CMD ["nginx"]
Installing the nginx-mod-http-lua
package will also install nginx
and luajit
, among others.
The nginx.conf
should contain at least this:
load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;
events {
worker_connections 1024;
}
daemon off;

- 14,759
- 6
- 32
- 44
-
6These `load_module` are not needed if you don’t remove `include /etc/nginx/modules/*.conf;` from the default [nginx.conf](https://github.com/alpinelinux/aports/blob/954391ce7493439d12180d5c5118dee26f8ac587/main/nginx/nginx.conf#L15). – Jakub Jirutka Nov 20 '17 at 17:11
-
@Marian: How do we install luarocks in this case? I did find include path for luajit. – Raeesaa Jan 03 '18 at 12:41
-
load_module" directive is not allowed here in /etc/nginx/conf.d – famas23 May 27 '19 at 18:42
-
Why the --no-cache flag? – jurl Jan 26 '20 at 06:57
-
2@jurl That's explained well in https://stackoverflow.com/a/49119046/1228491 – Marian Jan 27 '20 at 09:21
-
Lots of failures! Building from source code is Absolutely necessary! – Time Killer Apr 10 '22 at 15:14
Dockerfile:
FROM nginx:1.15-alpine
RUN mkdir -p /run/nginx
RUN apk add --no-cache nginx-mod-http-lua
COPY nginx_conf/ /etc/nginx/ # Your nginx conf
COPY lua/ /etc/lua/ # Your lua files
First line of nginx conf:
load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;

- 1,583
- 10
- 12
-
5nginx: [emerg] module "/usr/lib/nginx/modules/ndk_http_module.so" version 1016001 instead of 1018000 in /etc/nginx/nginx.conf:1 – Time Killer Jul 14 '20 at 01:25
-
weird... that was not happening last year. Try with FROM nginx:1.15-alpine I checked and is working. If you do the research to make it works with latest version please let me know the results. Anyways I edited the original answer. Thank you for the feedback. – Piero Jul 15 '20 at 04:44
-
The Nginx version does not match the module version, It seems that `apk add` cannot specify a version number. – Time Killer Jul 15 '20 at 05:08
-
Current alpine version is 1.21 - is there another solution apart from going back several nginx versions? – GrahamB Oct 25 '21 at 16:53
-
2You can use the tag `stable-alpine` (nginx version 1.20.2). But I have another error `module "/usr/lib/nginx/modules/ndk_http_module.so" is not binary compatible `. Do you have any clue to resolve it? – wolfgunner Dec 23 '21 at 12:04
-
We use Openresty, a platform that integrates nginx and Lua.
In the default nginx file, you could call Lua like so:
server {
listen 80;
listen 443 ssl; # 'ssl' parameter tells NGINX to decrypt the traffic
# 1
location ~ /api/(.*) {
resolver xxx.x.x.xx;
rewrite_by_lua_block {
ngx.req.set_header("x-header", "12345678901234567")
}
}
The alpine image here: https://github.com/openresty/docker-openresty/tree/master/
There is also an alpine-fat that had make
, git
and other libraries that can help you build within your Docker image.

- 88,126
- 95
- 281
- 483

- 185
- 2
- 16
See: "Adding third-party modules to nginx official image" At: https://github.com/nginxinc/docker-nginx/tree/master/modules
"It's possible to extend a mainline image with third-party modules either from your own instuctions following a simple filesystem layout/syntax using build_module.sh helper script, or failing back to package sources from pkg-oss."
$ docker build --build-arg ENABLED_MODULES="ndk lua" -t my-nginx-with-lua .

- 31
- 2
Building on Luc Chauvin's answer, here's a full walkthrough using the information from the guide on GitHub as well as the changes necessary to get it working with docker compose. This guide assumes that you previously used the provided alpine nginx image with the service set up like:
services:
nginx:
image: nginx:alpine
Download the base builder dockerfile: https://github.com/nginxinc/docker-nginx/blob/master/modules/Dockerfile.alpine (there is also a debian based version available).
Update the service in your
docker-compose.yaml
file to build the modules. Use the folder containing the dockerfile for the context, for this example I've placed it alongside the compose file:services: nginx: build: context: ./ args: ENABLED_MODULES: ndk lua
Add the following to the top of your
nginx.conf
to load the modules:load_module modules/ndk_http_module.so; load_module modules/ngx_http_lua_module.so;
Build the container with
docker compose build
. This will take a while, but at the end you should be able todocker compose up
without issue.

- 1,617
- 1
- 17
- 32
You look on the Docker Hub
and you find an Nginx image, based on Alpine Linux, with Lua support
Some examples
https://hub.docker.com/r/ilagnev/alpine-nginx-lua/
or
https://hub.docker.com/r/firesh/nginx-lua/
Have a look at the Dockerfile for more details

- 1,780
- 1
- 11
- 18

- 30,758
- 6
- 57
- 59
-
1I found both of these, but found that none of the two was up to date (last pushed a year ago). In addition, they are lacking the information on how to enable the module. – Marian Nov 18 '17 at 13:05