Skip to content

Commit

Permalink
feat: dynamic ports (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quadrubo authored Jul 17, 2023
1 parent 257ef91 commit a543080
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ RUN apt-get update \
htop \
iputils-ping \
openssh-client \
# Needed for envsubst
gettext-base \
# Install cron file
&& echo "MAILTO=\"\"\n* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
# Install node & npm
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ services:
- PGID=1000
- APP_URL=http://localhost
- TZ=Europe/Berlin
# Uncomment if you want to use ports other than 80 & 443
# - HTTP_PORT=8080
# - HTTPS_PORT=8443
volumes:
- '/etc/localtime:/etc/localtime:ro'
- './data/config:/config' # Directory for sqlite database & .env
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ services:
- PGID=1000
- APP_URL=http://localhost
- TZ=Europe/Berlin
- HTTP_PORT=8080
- HTTPS_PORT=8443
volumes:
- '/etc/localtime:/etc/localtime:ro'
- './data/config:/config' # Directory for sqlite database & .env
Expand Down
16 changes: 16 additions & 0 deletions docker/deploy/etc/s6-overlay/s6-rc.d/nginx/data/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/command/with-contenv bash
if [[ -z "${HTTP_PORT}" ]]; then
url="http://localhost/ping"
else
url="http://localhost:${HTTP_PORT}/ping"
fi

response=$(curl --location --insecure --silent "$url")

if [[ $response == "pong" ]]; then
exit 0
else
echo "❌ There seems to be a failure in checking the web server + PHP-FPM. Here's the response:"
echo $response
exit 1
fi
50 changes: 50 additions & 0 deletions docker/deploy/etc/s6-overlay/scripts/laravel-automations
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,56 @@ s6-setuidgid webuser php $WEBUSER_HOME/artisan db:seed --class FirstAdminUserSee
echo "✅ Admin user created."
echo ""

# Replace ports. Needed as the project is running in network_mode: host which doesn't support mapping ports.
# The nginx files have to be modified manually.
#
# Creates:
# - http.conf.bak https.conf.bak (Original files)
# - http.conf.template https.conf.template (Files with port variables to replace)
# - http.conf https.conf (Files used by the webserver, recreated each start)
echo "Replacing ports..."

if [ ! -f "/etc/nginx/site-opts.d/http.conf.bak" ] || [ ! -f "/etc/nginx/site-opts.d/http.conf.template" ]; then
cp /etc/nginx/site-opts.d/http.conf /etc/nginx/site-opts.d/http.conf.bak
cp /etc/nginx/site-opts.d/http.conf /etc/nginx/site-opts.d/http.conf.template

# Replace the port "80" with "${HTTP_PORT}" in the file
sed -i 's/listen 80 default_server;/listen ${HTTP_PORT} default_server;/' /etc/nginx/site-opts.d/http.conf.template
sed -i 's/listen \[::\]:80 default_server;/listen [::]:${HTTP_PORT} default_server;/' /etc/nginx/site-opts.d/http.conf.template
fi

if [ ! -f "/etc/nginx/site-opts.d/https.conf.bak" ] || [ ! -f "/etc/nginx/site-opts.d/https.conf.template" ]; then
cp /etc/nginx/site-opts.d/https.conf /etc/nginx/site-opts.d/https.conf.bak
cp /etc/nginx/site-opts.d/https.conf /etc/nginx/site-opts.d/https.conf.template

# Replace the port "443" with "${HTTPS_PORT}" in the file
sed -i 's/listen 443 http2 ssl default_server;/listen ${HTTPS_PORT} http2 ssl default_server;/' /etc/nginx/site-opts.d/https.conf.template
sed -i 's/listen \[::\]:443 http2 ssl default_server;/listen [::]:${HTTPS_PORT} http2 ssl default_server;/' /etc/nginx/site-opts.d/https.conf.template
fi

if [[ -z "$HTTP_PORT" ]]; then
echo "No overridden HTTP port found. Using default config."

cp /etc/nginx/site-opts.d/http.conf.bak /etc/nginx/site-opts.d/http.conf
else
echo "Replacing HTTP Port with $HTTP_PORT"

envsubst '$$HTTP_PORT' < /etc/nginx/site-opts.d/http.conf.template > /etc/nginx/site-opts.d/http.conf
fi

if [[ -z "$HTTPS_PORT" ]]; then
echo "No overridden HTTPS port found. Using default config."

cp /etc/nginx/site-opts.d/https.conf.bak /etc/nginx/site-opts.d/https.conf
else
echo "Replacing HTTPS Port with $HTTPS_PORT"

envsubst '$$HTTPS_PORT' < /etc/nginx/site-opts.d/https.conf.template > /etc/nginx/site-opts.d/https.conf
fi

echo "✅ Ports replaced."
echo ""

# App install done, show a message
echo "✅ All set, Template started."
echo ""

0 comments on commit a543080

Please sign in to comment.