Compare commits

..

3 Commits

Author SHA1 Message Date
9e8b6e44bb feature: try set_by_lua func
All checks were successful
Gitea/docker-base-images/nginx-routes/pipeline/head This commit looks good
2023-09-08 01:19:19 +03:00
8da9c6e598 wip
All checks were successful
Gitea/docker-base-images/nginx-routes/pipeline/head This commit looks good
2023-09-08 01:12:46 +03:00
a82e359da2 fix: fixed Dockerfile syntax
All checks were successful
Gitea/docker-base-images/nginx-routes/pipeline/head This commit looks good
2023-09-08 01:00:35 +03:00
4 changed files with 16 additions and 70 deletions

View File

@ -1,19 +1,17 @@
ARG BASE_IMAGE="fabiocicerchia/nginx-lua:1.25.2-alpine3.18.3" ARG BASE_IMAGE="nginx"
FROM ${BASE_IMAGE} FROM ${BASE_IMAGE}
# Install ping and ip # Install ping and ip
RUN apk add --no-cache iproute2 iputils bash RUN apt update && \
apt install -y iproute2 iputils-ping
# Default nginx proxy envs # Default nginx proxy envs
ENV NGINX_PROXY_SOURCE_PORT=${NGINX_PROXY_SOURCE_PORT:-"Please configure NGINX_PROXY_SOURCE_PORT env!"} ENV NGINX_PROXY_SOURCE_PORT=${NGINX_PROXY_SOURCE_PORT:-"Please configure NGINX_PROXY_SOURCE_PORT env!"}
ENV NGINX_PROXY_SOURCE_ADDRESS=${NGINX_PROXY_SOURCE_ADDRESS:-"Please configure NGINX_PROXY_SOURCE_ADDRESS env!"} ENV NGINX_PROXY_SOURCE_ADDRESS=${NGINX_PROXY_SOURCE_ADDRESS:-"Please configure NGINX_PROXY_SOURCE_ADDRESS env!"}
# Copy default nginx configuration # Copy default nginx configuration
#COPY default.nginx.conf /etc/nginx/nginx.conf COPY default.nginx.conf /etc/nginx/nginx.conf
# Remove default nginx config
RUN rm -r /etc/nginx/nginx.conf
# Copy entrypoint # Copy entrypoint
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

View File

@ -1,46 +0,0 @@
# Nginx Routes Helper
Специальный образ NGINX, который упрощает работу с проксированием портов между разными сетками у докер контейнеров.
По-умолчанию установлены утилиты для работы с роутами.
# Пре-стартовый скрипт
При старте контейнера происходит проверка на наличие скрипта, который должен быть выполнен в самом начале.
Скрипт располагается в `/invoke_initial_script.sh`, собственно сюда его и надо монтировать.
Например, требуется добавить роуты в контейнер при его старте.
`/invoke_initial_script.sh`:
```bash
#!/usr/bin/env bash
echo "Adding routes..."
ip route add 192.168.255.0/24 via 172.20.20.2
echo "Routes added!"
```
Тут может быть совершенно что угодно, этот скрипт выполнится до старта NGINX.
# Проброс портов по-умолчанию
Если пользователь не примонтирует свой `/etc/nginx/nginx.conf`, то по-умолчанию будет использоваться
обычный `stream` прокси с порта на адрес. Слушаемый порт и целевой адрес указываются через переменные окружения:
- `NGINX_PROXY_SOURCE_PORT` - Порт (например `80`), который слушает NGINX
- `NGINX_PROXY_TARGET_ADDRESS` - Адрес и порт (например `localhost:25565`), куда будет отправляться трафик
Пример использования в `docker-compose`:
```yml
version: '3.7'
services:
test-nginx:
image: "git.tswf.io/docker-base-images/nginx-routes:latest"
ports:
- "25565:25565"
environment:
NGINX_PROXY_SOURCE_PORT: 25565
NGINX_PROXY_TARGET_ADDRESS: "another_container:9000"
another_container:
image: "blablabla"
#...
```

12
default.nginx.conf Normal file
View File

@ -0,0 +1,12 @@
events {}
set_by_lua $nginx_proxy_source_port 'return os.getenv("$NGINX_PROXY_SOURCE_PORT")';
set_by_lua $nginx_proxy_source_address 'return os.getenv("$NGINX_PROXY_SOURCE_ADDRESS")';
stream {
server {
listen $nginx_proxy_source_port;
proxy_pass $nginx_proxy_source_address;
}
}

View File

@ -6,24 +6,6 @@ if [ -f "/invoke_initial_script.sh" ]; then
source /invoke_initial_script.sh source /invoke_initial_script.sh
fi fi
# Проверяем наличие файла /etc/nginx/nginx.conf
if [ ! -f "/etc/nginx/nginx.conf" ]; then
# Заполняем файл /etc/nginx/nginx.conf с использованием переменных окружения
cat > /etc/nginx/nginx.conf <<EOF
events {}
stream {
server {
listen $NGINX_PROXY_SOURCE_PORT;
proxy_pass $NGINX_PROXY_TARGET_ADDRESS;
}
}
EOF
echo "Created default nginx conf forwarding from port '$NGINX_PROXY_SOURCE_PORT' to address '$NGINX_PROXY_TARGET_ADDRESS'"
else
echo "Skip default "
fi
if [[ "$1" = 'app' ]]; then if [[ "$1" = 'app' ]]; then
exec nginx -g 'daemon off;' exec nginx -g 'daemon off;'
else else