Skip to content

Commit

Permalink
Initial commit 🐳
Browse files Browse the repository at this point in the history
  • Loading branch information
tofran committed Sep 13, 2024
0 parents commit 31c06d7
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci-cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI/CD

on:
push:
branches:
- main
pull_request:
release:
types:
- published
workflow_dispatch:

jobs:
build_and_push:
name: Build and push image
permissions:
contents: read
id-token: write
packages: write
uses: significa/actions/.github/workflows/docker-image.yaml@main
with:
image_name: nginx-with-basic-auth
5 changes: 5 additions & 0 deletions 40-basic-auth-htpasswd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -eu

/usr/bin/htpasswd -bc /etc/nginx/htpasswd "$BASIC_AUTH_USERNAME" "$BASIC_AUTH_PASSWORD"
20 changes: 20 additions & 0 deletions 50-setup-health-and-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

set -eu

mkdir -p /usr/share/nginx/app-version
echo -n "$APP_VERSION" > /usr/share/nginx/app-version/app-version.txt

NGINX_CONF_PATH="/etc/nginx/conf.d/default.conf"

if [ "$ENABLE_HEALTH_ROUTE" = "true" ]; then
echo "Enabling /healthz route"
sed -i 's/# include \/etc\/nginx\/healthz.conf;/include \/etc\/nginx\/healthz.conf;/' $NGINX_CONF_PATH
fi

if [ "$ENABLE_VERSION_ROUTE" = "true" ]; then
echo "Enabling /healthz/version route"
sed -i 's/# include \/etc\/nginx\/app_version.conf;/include \/etc\/nginx\/app_version.conf;/' $NGINX_CONF_PATH
fi

cat $NGINX_CONF_PATH
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM nginx:1-alpine

RUN apk update && \
apk add --no-cache apache2-utils && \
rm /var/cache/apk/*

COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY ./healthz.conf ./app_version.conf /etc/nginx/
COPY ./40-basic-auth-htpasswd.sh ./50-setup-health-and-version.sh /docker-entrypoint.d/

ARG APP_VERSION=unknown
ENV APP_VERSION=$APP_VERSION
ENV ENABLE_HEALTH_ROUTE=true
ENV ENABLE_VERSION_ROUTE=true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Significa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Nginx with Basic Auth Docker Image

This is a simple, small, and multi-arch Docker image of Nginx with basic authentication enabled.

**Example**

```sh
docker run \
-e BASIC_AUTH_USERNAME=your_username \
-e BASIC_AUTH_PASSWORD=your_password \
-v ./your/www/dir:/usr/share/nginx/html \
ghcr.io/significa/nginx-with-basic-auth
```

Image name/url: `ghcr.io/significa/nginx-with-basic-auth`

## Usage

This image can be configured using environment variables during runtime. You do not need to prepare htpasswd encoded strings beforehand. Simply pass the environment variables in plaintext.


**Required Environment Variables**

* `BASIC_AUTH_USERNAME`: The username for basic authentication.
* `BASIC_AUTH_PASSWORD`: The password for basic authentication.

**Optional Environment Variables**

These environment variables are optional and are used to enable `/healthz` and `/healthz/version`. They default to `true` but can be set to `false` to disable them. The version rute consumes the `APP_VERSION` env var (that can be set via a build argument).

* `ENABLE_HEALTH_ROUTE`: Enables the `/healthz` route.
* `ENABLE_VERSION_ROUTE`: Enables the `/version` route.
5 changes: 5 additions & 0 deletions app_version.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
location /healthz/version {
charset utf-8;
alias /usr/share/nginx/app-version/;
try_files /app-version.txt =404;
}
5 changes: 5 additions & 0 deletions healthz.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
location /healthz {
charset utf-8;
default_type text/plain;
return 200 "OK";
}
21 changes: 21 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
listen 80;
listen [::]:80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html;
charset utf-8;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
}

# include /etc/nginx/healthz.conf;
# include /etc/nginx/app_version.conf;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

0 comments on commit 31c06d7

Please sign in to comment.