Skip to content

Commit

Permalink
Improved dockerfile for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
robigan committed Feb 21, 2024
1 parent 527ce43 commit 9f8f26c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
49 changes: 42 additions & 7 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
FROM node:18-alpine
# Install dependencies only when needed
FROM node:18-alpine AS deps

COPY . /app
WORKDIR /app

RUN cd /app \
&& yarn \
&& yarn next telemetry disable \
&& yarn next build
COPY package.json yarn.lock ./

CMD yarn start
RUN yarn

# Rebuild the source code only when needed
FROM node:18-alpine AS builder

WORKDIR /app

COPY . .
COPY --from=deps /app/node_modules ./node_modules

RUN NEXT_PUBLIC_API_BASE_URL="APP_PLACEHOLDER_NEXT_PUBLIC_API_BASE_URL" NEXT_PUBLIC_HCAPTCHA_SITE_KEY="APP_PLACEHOLDER_NEXT_PUBLIC_HCAPTCHA_SITE_KEY" yarn build

# Production image, copy all the files and run next
FROM node:18-alpine AS runner

WORKDIR /app

ENV NODE_ENV production

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/docker-entrypoint.sh ./entrypoint.sh

RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001 -G nodejs && \
chown -R nextjs:nodejs /app/.next && \
chmod +x /app/entrypoint.sh

RUN yarn next telemetry disable

USER nextjs

EXPOSE 3000

ENTRYPOINT ["/app/entrypoint.sh"]

CMD yarn start
12 changes: 12 additions & 0 deletions frontend/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

echo "Check that we have NEXT_PUBLIC_API_BASE_URL, NEXT_PUBLIC_HCAPTCHA_SITE_KEY vars"
test -n "$NEXT_PUBLIC_API_BASE_URL"
test -n "$NEXT_PUBLIC_HCAPTCHA_SITE_KEY"

find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_PLACEHOLDER_NEXT_PUBLIC_API_BASE_URL#$NEXT_PUBLIC_API_BASE_URL#g" # Replace NEXT_PUBLIC_API_BASE_URL

find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#APP_PLACEHOLDER_NEXT_PUBLIC_HCAPTCHA_SITE_KEY#$NEXT_PUBLIC_HCAPTCHA_SITE_KEY#g" # Replace NEXT_PUBLIC_HCAPTCHA_SITE_KEY

echo "Starting Nextjs"
exec "$@"

0 comments on commit 9f8f26c

Please sign in to comment.