Compare commits

...

3 Commits

Author SHA1 Message Date
b489675003 feature: added default port forwarding logic and readme
Some checks failed
GIT.TSWF.IO/docker-base-images/nginx-routes/pipeline/head There was a failure building this commit
Gitea/docker-base-images/nginx-routes/pipeline/head This commit looks good
2023-09-08 11:31:39 +03:00
21fe5003b8 feature: added port forwarding by default
Some checks failed
Gitea/docker-base-images/nginx-routes/pipeline/head There was a failure building this commit
2023-09-08 00:59:00 +03:00
8d20fb128c chore: fixed image name
All checks were successful
Gitea/docker-base-images/nginx-routes/pipeline/head This commit looks good
2023-09-02 00:19:39 +03:00
4 changed files with 77 additions and 4 deletions

View File

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

2
Jenkinsfile vendored
View File

@ -18,7 +18,7 @@ pipeline {
stage('prepare') {
steps {
script {
env.IMAGE_BASE_NAME = "jdk14-alpine"
env.IMAGE_BASE_NAME = "nginx-routes"
env.RELEASE_TAG_PREFIX = "release-"
env.REGISTRY_HOST = "git.tswf.io"
if (env.TAG_NAME == null) {

46
README.MD Normal file
View File

@ -0,0 +1,46 @@
# 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"
#...
```

View File

@ -6,6 +6,24 @@ if [ -f "/invoke_initial_script.sh" ]; then
source /invoke_initial_script.sh
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
exec nginx -g 'daemon off;'
else