Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to run container as non-root user and other security updates #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
FROM nginx:1.15-alpine
FROM nginx:1.17-alpine

COPY start.sh /usr/local/bin/

RUN apk add --update bash \
&& rm -rf /var/cache/apk/* \
&& chmod -R g+w /var/cache/nginx /var/log/nginx /etc/nginx \
&& chown -R nginx:root /var/cache/nginx /var/log/nginx /etc/nginx \
&& chmod g+w /run \
&& sed -i 's/user nginx;//g' /etc/nginx/nginx.conf \
&& chmod +x /usr/local/bin/start.sh

EXPOSE 80
EXPOSE 8080

USER 1000
Copy link

@wirespecter wirespecter Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question. Even if you switched to a non-root user here, wouldn't nginx still be running as root in the background?

My point is: if an attacker finds a way to exploit nginx he will be root.
To avoid this: a new non-root user must be created and add user nonroot_username; in nginx conf so that nginx is not run as root too :)


CMD ["start.sh"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Merten Peetz
Copyright (c) 2020 Marc Schmid

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# Docker-Web-Redirect #

![Docker Build Status](https://img.shields.io/docker/build/morbz/docker-web-redirect.svg) ![Docker Pulls](https://img.shields.io/docker/pulls/morbz/docker-web-redirect.svg) ![Docker Stars](https://img.shields.io/docker/stars/morbz/docker-web-redirect.svg)
![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/m4rc77/docker-web-redirect)
![Docker Cloud Automated build](https://img.shields.io/docker/cloud/automated/m4rc77/docker-web-redirect)
![Docker Pulls](https://img.shields.io/docker/pulls/m4rc77/docker-web-redirect)
![Docker Stars](https://img.shields.io/docker/stars/m4rc77/docker-web-redirect)

This Docker container listens on port 80 and redirects all web traffic permanently to the given target domain/URL.
This Docker container listens (by default) on port 8080 and redirects all web traffic permanently to the given target domain/URL.

_Hint: This repo was forked from https://github.com/MorbZ/docker-web-redirect._

## Features ##
- Lightweight: Uses only ~2 MB RAM on Linux
- Keeps the URL path and GET parameters
- Permanent redirect (HTTP 301)
- Image Size only ~25MB
- Image runs for security reasons with non-root user

## Usage ##
### Docker run ###
The target domain/URL is set by the `REDIRECT_TARGET` environment variable.
The target domain/URL is set by the `REDIRECT_TARGET` environment variable.
The port may be changed to another port than 8080 by the `PORT` environment variable.
Possible redirect targets include domains (`mydomain.net`), paths (`mydomain.net/my_page`) or specific protocols (`https://mydomain.net/my_page`).

**Example:** `$ docker run --rm -d -e REDIRECT_TARGET=mydomain.net -p 80:80 morbz/docker-web-redirect`
**Example (Listen on Port 8080):** `$ docker run --rm -d -e REDIRECT_TARGET=mydomain.net -p 8080:8080 m4rc77/docker-web-redirect`

**Example (Listen on Port 80):** `$ docker run --rm -d -u0:0 -e REDIRECT_TARGET=mydomain.net -e PORT=80 -p 80:80 m4rc77/docker-web-redirect `

### Paths are retained ###
The URL path and GET parameters are retained. That means that a request to `http://myolddomain.net/index.php?page=2` will be redirected to `http://mydomain.net/index.php?page=2` when `REDIRECT_TARGET=mydomain.net` is set.
Expand All @@ -29,9 +39,13 @@ This image can be combined with the [jwilder nginx-proxy](https://hub.docker.com
version: '3'
services:
redirect:
image: morbz/docker-web-redirect
image: m4rc77/docker-web-redirect
restart: always
environment:
- VIRTUAL_HOST=myolddomain.net
- REDIRECT_TARGET=mydomain.net
- VIRTUAL_PORT=8080
```

### Build the image yourself ###
`$ docker build -t m4rc77/docker-web-redirect:latest .`
10 changes: 5 additions & 5 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ if [ -z "$REDIRECT_TARGET" ]; then
echo "Redirect target variable not set (REDIRECT_TARGET)"
exit 1
else
# Add http if not set
if ! [[ $REDIRECT_TARGET =~ ^https?:// ]]; then
REDIRECT_TARGET="http://$REDIRECT_TARGET"
# Add https if not set
if ! [[ $REDIRECT_TARGET =~ ^http?:// ]]; then
REDIRECT_TARGET="https://$REDIRECT_TARGET"
fi

# Add trailing slash
Expand All @@ -14,8 +14,8 @@ else
fi
fi

# Default to 80
LISTEN="80"
# Default to 8080
LISTEN="8080"
# Listen to PORT variable given on Cloud Run Context
if [ ! -z "$PORT" ]; then
LISTEN="$PORT"
Expand Down