WittCode💻

How to Add Modules to Nginx Docker Image

By

Learn how to add modules like RTMP, Lua, etc. to Nginx using the official Nginx Docker image. We will also go over Docker build arguments and how to use them.

Table of Contents 📖

Nginx Default Modules

If we spin up the official Nginx image, we can see the modules that are already installed inside /etc/nginx/modules.

ngx_http_geoip_module-debug.so         ngx_http_js_module.so                 ngx_stream_geoip_module-debug.so
ngx_http_geoip_module.so               ngx_http_xslt_filter_module-debug.so  ngx_stream_geoip_module.so
ngx_http_image_filter_module-debug.so  ngx_http_xslt_filter_module.so        ngx_stream_js_module-debug.so
ngx_http_image_filter_module.so        ngx_stream_js_module.so               ngx_http_js_module-debug.so

Here we can see the http modules, stream modules, etc. In this article we will be adding the RTMP module.

INFO: RTMP stands for real time messaging protocol, a protocol that allows for streaming video and audio.

Dockerfile for Installing Nginx Modules

The developers of the official Nginx image made it very easy to install third party modules. We can install modules through pkg-oss, a tool that allows us to create, install, and upgrade third party modules for Nginx. This is all handled for us inside a Dockerfile at the following URL.

https://github.com/nginxinc/docker-nginx/blob/master/modules/Dockerfile

INFO: Note that Docker BuildKit is required to use the following Dockerfile. Docker BuildKit is enabled by default in Docker version 23 but needs to be enabled in previous versions by setting the environment variable DOCKER_BUILDKIT to 1.

We can download the contents of this Dockerfile using cURL.

curl -o Dockerfile https://raw.githubusercontent.com/nginxinc/docker-nginx/master/modules/Dockerfile

The -o option tells cURL to write to a file instead of stdout.

Installing Nginx RTMP Module

Now we can build an image from this Dockerfile. We can get it to install additional modules by specifying ENABLED_MODULES as a build argument.

docker build --build-arg ENABLED_MODULES="rtmp" -t nginx-rtmp-i .

Build arguments are passed to Docker at build time, allowing us to customize the build. The ENABLED_MODULES argument is used to specify which modules to enable in the Nginx image. Therefore, the above command will create a Nginx image with the RTMP module. Now we just need to create a container from this image.

docker run --name nginx-rtmp-c nginx-rtmp-i

Now if we inspect the /etc/nginx/modules directory we can see the RTMP module.

ngx_http_geoip_module-debug.so         ngx_http_js_module.so                 ngx_stream_geoip_module-debug.so
ngx_http_geoip_module.so               ngx_http_xslt_filter_module-debug.so  ngx_stream_geoip_module.so
ngx_http_image_filter_module-debug.so  ngx_http_xslt_filter_module.so        ngx_stream_js_module-debug.so
ngx_http_image_filter_module.so        ngx_rtmp_module-debug.so              ngx_stream_js_module.so
ngx_http_js_module-debug.so            ngx_rtmp_module.so

Note the presence of the RTMP modules. We can install more than one module by passing multiple modules to ENABLED_MODULES.

docker build --build-arg ENABLED_MODULES="rtmp ndk lua" -t nginx-rtmp-c .

This will create an Nginx image with the RTMP, Lua, and NDK modules.