user9669681
user9669681

Reputation:

Docker + OpenResty - flexible configuration. How to?

I have the openresty application that deploys with docker.

My docker file:

FROM openresty/openresty:alpine-fat

ARG APPLICATION_PATH="/srv/www/my-app"

COPY nginx.conf /usr/local/openresty/nginx/conf

RUN mkdir ${APPLICATION_PATH}

I'm running docker with this command:

docker run -v $(CURRENT_DIRECTORY):/srv/www/my-app -v $(CURRENT_DIRECTORY)/conf:/etc/nginx/conf.d --name="$(APP_NAME)" -p $(PORT):80 -d $(CONTAINER_NAME)

This command stored in the Makefile and variables values like that:

CONTAINER_NAME = my-app
APP_NAME = my-app

override PORT = 8080

ifeq ($(OS),Windows_NT)
    CURRENT_DIRECTORY=%cd%
else
    CURRENT_DIRECTORY=${PWD}
endif

So also I have my-app.conf stored in the conf directory. This is nginx-configuration file, where I have this line:

content_by_lua_file '/srv/www/my-app/main.lua';

And further I have nginx.conf, where I have this line:

lua_package_path ";;/srv/www/my-app/?.lua;/srv/www/my-app/application/?.lua";

I don't want duplicate /srv/www/my-app in the 3 files. How I can avoid this?

Upvotes: 2

Views: 4452

Answers (2)

reza imany
reza imany

Reputation: 49

you can use this repo https://github.com/RezaImany/openresty-full-solution.git it has docker compose file and you can easily modify the nginx.conf and other .conf files

Upvotes: 0

Alexander Altshuler
Alexander Altshuler

Reputation: 3064

IMO, your approach is not consistent.

You copy nginx.conf file, but mount a volume for my-app.conf (is it included into nginx.conf?)

Curiously that $(CURRENT_DIRECTORY)/conf is mounted twice - as /srv/www/my-app/conf and as /etc/nginx/conf.d.

Below is my approach for OpenResty containers:

  1. Write simple nginx.conf without includes. Copy it into container as you do. The only reason to mount a folder with nginx.conf is ability to reload nginx configuration after changes. Keep in mind - if you would mount a single file reload may not work. https://github.com/docker/for-win/issues/328

  2. Copy all Lua files mentioned in *_by_lua_file directives into /usr/local/openresty/nginx

  3. Copy all Lua files required from files above (if any) into /usr/local/openresty/lualib

  4. Don't use absolute file paths in *_by_lua_file directives, you may specify relative path to /usr/local/openresty/nginx

  5. Don't use lua_package_path directive, defaults should works.

Here is the simple working example https://gist.github.com/altexy/8f8e08fd13cda25ca47418ab4061ce1b

Upvotes: 2

Related Questions