Skip to content

Commit

Permalink
Replace alpine with node-slim and fix Docker deployment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
claitz committed Sep 5, 2023
1 parent 5a1da08 commit d6c0543
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 52 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ DATABASE_HOST=your_database_host
DATABASE_USER=your_database_user
DATABASE_PASSWORD=your_database_password
DATABASE_NAME=your_database_name
PROHIBITED_CHARACTERS= ",#,&,%,.,!,^,_,-"
MIN_PASSWORD_LENGTH=6
MAX_PASSWORD_LENGTH=12
DISCORD_CLIENT_ID=your_discord_client_id
Expand Down
1 change: 0 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ DATABASE_HOST=APP_DATABASE_HOST
DATABASE_USER=APP_DATABASE_USER
DATABASE_PASSWORD=APP_DATABASE_PASSWORD
DATABASE_NAME=APP_DATABASE_NAME
PROHIBITED_CHARACTERS=APP_PROHIBITED_CHARACTERS
MIN_PASSWORD_LENGTH=APP_MIN_PASSWORD_LENGTH
MAX_PASSWORD_LENGTH=APP_MAX_PASSWORD_LENGTH
DISCORD_CLIENT_ID=APP_DISCORD_CLIENT_ID
Expand Down
30 changes: 8 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
COPY package-lock.json ./
RUN npm ci

# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
#COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY entrypoint.sh .
COPY .env.production .

# Copy necessary files and directories for building the app at runtime
COPY . .
COPY --from=deps /app/node_modules ./node_modules

# Execute script
RUN apk add --no-cache --upgrade bash
RUN ["chmod", "+x", "./entrypoint.sh"]
ENTRYPOINT ["./entrypoint.sh"]
USER nextjs
EXPOSE 3000
ENV PORT 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["node_modules/.bin/next", "start"]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ To set up the OpenDAoC Account Manager, copy `.env.example` to `.env` and adjust
- `DATABASE_NAME`: Database name.

- **Password Settings**:
- `PROHIBITED_CHARACTERS`: Prohibited characters for passwords.
- `MIN_PASSWORD_LENGTH`: Minimum password length.
- `MAX_PASSWORD_LENGTH`: Maximum password length.

Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ services:
- DATABASE_USER: your_database_user
- DATABASE_PASSWORD: your_database_password
- DATABASE_NAME: your_database_name
- PROHIBITED_CHARACTERS: " ,#,&,%,.,!,^,_,-"
- MIN_PASSWORD_LENGTH: 6
- MAX_PASSWORD_LENGTH: 12
- DISCORD_CLIENT_ID: your_discord_client_id
Expand Down
61 changes: 36 additions & 25 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
#!/bin/bash
# no verbose
set +x
# config

envFilename='.env.production'
nextFolder='./.next/'
function apply_path {
# read all config file
while IFS= read -r line || [ -n "$line" ]; do
# no comment or not empty
if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then
continue

# Set permissions
chown -R nextjs:nodejs /app
chown nextjs:nodejs $envFilename

# Replace values in .env.production with environment variables
tempFile=$(mktemp)
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments or empty lines
if [[ "${line:0:1}" == "#" ]] || [[ -z "$line" ]]; then
continue
fi
# split

# Split the line into name and default value
configName="$(cut -d'=' -f1 <<<"$line")"
configValue="$(cut -d'=' -f2 <<<"$line")"
# get system env
envValue=$(env | grep "^$configName=" | grep -oe '[^=]*$');

# if config found
if [ -n "$configValue" ] && [ -n "$envValue" ]; then
# replace all
echo "Replace: ${configValue} with: ${envValue}"
find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g"
defaultValue="$(cut -d'=' -f2 <<<"$line")"

# Get the environment variable value
envValue=$(printenv "$configName")

# If the environment variable is set, replace the default value in .env.production
if [[ -n "$envValue" ]]; then
echo "Replace: $defaultValue with: $envValue in $envFilename"
printf "%s\n" "$line" | sed "s#^$configName=$defaultValue#$configName=$(printf '%s' "$envValue" | sed 's/[\/&]/\\&/g')#" >> "$tempFile"
else
printf "%s\n" "$line" >> "$tempFile"
fi
done < $envFilename
}
apply_path
echo "Starting Nextjs"
exec "$@"
done < $envFilename

# Replace the contents of .env.production with the modified temp file
cat "$tempFile" > "$envFilename"
rm "$tempFile"

# Build the site as the nextjs user
su -s /bin/sh -c "npm run build" nextjs

# Start the Next.js app as nextjs user
su -s /bin/sh -c "exec node_modules/.bin/next start" nextjs
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
DATABASE_NAME: process.env.DATABASE_NAME,

// Password settings
PROHIBITED_CHARACTERS: process.env.PROHIBITED_CHARACTERS?.split(",") || [" ","#","&","%",".","!","^","_","-"],
PROHIBITED_CHARACTERS: [" ","#","&","%",".","!","^","_","-"],
MIN_PASSWORD_LENGTH: parseInt(process.env.MIN_PASSWORD_LENGTH),
MAX_PASSWORD_LENGTH: parseInt(process.env.MAX_PASSWORD_LENGTH),

Expand Down

0 comments on commit d6c0543

Please sign in to comment.