# OpenResty Docker image

This is a fork of https://github.com/ficusio/openresty's alpine flavour.

### Paths & config

NginX is configured with `/opt/secondbit/nginx` [prefix path](http://nginx.org/en/docs/configure.html), which means that, by default, it loads configuration from `/opt/secondbit/nginx/conf/nginx.conf` file. The default HTML root path is `/opt/secondbit/nginx/html/`.

OpenResty bundle includes several useful Lua modules located in `/opt/secondbit/lualib/` directory. This directory is already present in Lua package path, so you don't need to specify it in NginX `lua_package_path` directive.

The Lua NginX module is built with LuaJIT 2.1, which is also available as stand-alone `lua` binary.

### `ONBUILD` hook

This image uses [`ONBUILD` hook](https://docs.docker.com/reference/builder/#onbuild) that automatically copies all files and subdirectories from the `nginx/` directory located at the root of Docker build context (i.e. next to your `Dockerfile`) into `/opt/secondbit/nginx/`. The minimal configuration needed to get NginX running is the following:

```
project_root/
 ├ nginx/ # all subdirs/files will be copied to /opt/secondbit/nginx/
 |  └ conf/
 |     └ nginx.conf # your NginX configuration file
 └ Dockerfile
```

Dockerfile:

```
FROM secondbit/nginx:latest
EXPOSE 8080
```

### Command-line parameters

NginX is launched with the `nginx -g 'daemon off; error\_log /dev/stderr info; access\_log /dev/stdout;'` command. This means that you should not specify the `daemon` directive in your `nginx.conf` file, because it will lead to NginX config check error (duplicate directive).

No-daemon mode is needed to allow host OS' service manager, like `systemd`, or [Docker itself](https://docs.docker.com/reference/commandline/cli/#restart-policies) to detect that NginX has exited and restart the container. Otherwise in-container service manager would be required.

Error log and access log are redirected to `stderr` and `stdout` respectively to simplify debugging and log collection with tools like [progrium/logspout](https://github.com/progrium/logspout).

If you wish to run it with different command-line options, you can add `CMD` directive to your Dockerfile. It will override the command provided in this image. Another option is to pass a command to `docker run` directly:

```text
$ docker run --rm -it --name test secondbit/nginx bash
root@06823698db68:/opt/secondbit/nginx $ ls -l
total 12
drwxr-xr-x    2 root     root          4096 Feb  1 14:48 conf
drwxr-xr-x    2 root     root          4096 Feb  1 14:48 html
drwxr-xr-x    2 root     root          4096 Feb  1 14:48 sbin
```

### Usage during development

To avoid rebuilding your Docker image after each modification of Lua code or NginX config, you can add a simple script that mounts config/content directories to appropriate locations and starts NginX:

```bash
#!/usr/bin/env bash

exec docker run --rm -it \
  --name my-app-dev \
  -v "$(pwd)/nginx/conf":/opt/secondbit/nginx/conf \
  -v "$(pwd)/nginx/lualib":/opt/secondbit/nginx/lualib \
  -p 8080:8080 \
  secondbit/nginx:latest "$@"

# you may add more -v options to mount another directories, e.g. nginx/html/

# do not do -v "$(pwd)/nginx":/opt/secondbit/nginx because it will hide
# the NginX binary located at /opt/secondbit/nginx/sbin/nginx
```

Place it next to your `Dockerfile`, make executable and use during development. You may also want to temporarily disable [Lua code cache](https://github.com/openresty/lua-nginx-module#lua_code_cache) to allow testing code modifications without re-starting NginX.
