-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace alpine with node-slim and fix Docker deployment issues
- Loading branch information
Showing
7 changed files
with
45 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters