Skip to content

Commit

Permalink
Fix docker entrypoint (#878)
Browse files Browse the repository at this point in the history
With this change, we search for relevant environment variables
and replace the placeholder value with the actual runtime
environment variable's value.

We don't have to specify the keys of the environment variables
anymore and the variables can be unset.

Based on https://www.tomoliver.net/posts/nextjs-docker-public-env-vars
  • Loading branch information
evroon committed Aug 30, 2024
1 parent 55d1337 commit 54b70a7
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions frontend/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#!/bin/bash
set -evo pipefail
set -eo pipefail

# Script to replace `NEXT_PUBLIC_*` environment variables because they're set at build-time but we want to set them at runtime in `docker-compose.yml`
# Define an array of environment variable names
env_vars=("NEXT_PUBLIC_API_BASE_URL" "NEXT_PUBLIC_HCAPTCHA_SITE_KEY")
# Script to replace `NEXT_PUBLIC_*` environment variables because they're set
# at build-time but we want to set them at runtime in `docker-compose.yml`

echo "Checking that we have variables..."
# Iterate over the array and perform checks
for var in "${env_vars[@]}"; do
test -n "${!var}"
done
# The first part wrapped in a function
makeSedCommands() {
printenv | \
grep '^NEXT_PUBLIC' | \
sed -r "s/=/ /g" | \
xargs -n 2 bash -c 'echo "sed -i \"s#APP_PLACEHOLDER_$0#$1#g\""'
}

# Iterate over the array and perform substitutions
for var in "${env_vars[@]}"; do
find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_PLACEHOLDER_$var#${!var}#g"
# Set the delimiter to newlines (needed for looping over the function output)
IFS=$'\n'
# For each sed command
for c in $(makeSedCommands); do
# For each file in the .next directory
for f in $(find .next -type f); do
# Execute the command against the file
COMMAND="$c $f"
eval $COMMAND
done
done

echo "Starting Nextjs"
Expand Down

0 comments on commit 54b70a7

Please sign in to comment.