I am trying to host my website through a Docker container but i am facing an issue during the build process.
Following is my Dockerfile:
FROM node:18-alpine AS base
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN sed -E -n 's/[^#]+/export &/ p' .env.production.local >> .envvars
RUN source .envvars && npm run build
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
RUN mkdir .next
RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3001
ENV PORT 3001
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]
When the build process reaches a line in my code that parses the Firebase Service Key, I get the following error: SyntaxError: Unexpected token t in JSON at position 1. This error does not however occur if I build the website outside the docker env (my local machine).
Following is the parsing code:
const serviceAccountKey = process.env.FIREBASE_SERVICE_ACCOUNT_KEY;
if (!serviceAccountKey) {
throw new Error('FIREBASE_SERVICE_ACCOUNT_KEY is missing');
}
let saK;
if (typeof serviceAccountKey === 'string') {
try {
saK = JSON.parse(serviceAccountKey);
} catch (error) {
console.error('FIREBASE_SERVICE_ACCOUNT_KEY is not a valid JSON', error.stack);
}
} else if (typeof serviceAccountKey === 'object') {
saK = serviceAccountKey;
}
Why does it fail when my Docker image is building?
