I want to deploy my Next.JS app with Docker and GitHub CI. The actions is performing as wanted, but the .env.production
file does not seem to be found when referencing it in the Dockerfile.
I also tried renaming it to .env or any other filename, I tried using another path like /app/.env.production and trying different versions of "SpicyPizza/create-envfile". The .env.production file is not ignored in .dockerignore or .gitignore.
I get the following error:
ERROR: failed to solve: process "/bin/sh -c cat .env.production && yarn global add pnpm && SKIP_ENV_VALIDATION=1 pnpm run build" did not complete successfully: exit code: 1
Error: buildx failed with: ERROR: failed to solve: process "/bin/sh -c cat .env.production && yarn global add pnpm && SKIP_ENV_VALIDATION=1 pnpm run build" did not complete successfully: exit code: 1
This is my current config:
name: Docker Image CI
on:
push:
branches: [ "main" ]
jobs:
build:
environment: production
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create .env file
uses: SpicyPizza/create-envfile@v2
with:
envkey_EMAIL_SERVER: ${{vars.EMAIL_SERVER}}
envkey_EMAIL_FROM: ${{vars.EMAIL_FROM}}
envkey_EMAIL_TO: ${{vars.EMAIL_TO}}
envkey_NEXT_PUBLIC_RECAPTCHA_SITE_KEY: ${{vars.NEXT_PUBLIC_RECAPTCHA_SITE_KEY}}
envkey_RECAPTCHA_SECRET_KEY: ${{secrets.RECAPTCHA_SECRET_KEY}}
file_name: .env.production
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ secrets.DOCKERHUB_REPO }}:latest
My Dockerfile looks like this:
##### DEPENDENCIES
FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat openssl1.1-compat
WORKDIR /app
# Install dependencies
COPY package.json pnpm-lock.yaml* ./
RUN yarn global add pnpm && pnpm i
##### BUILDER
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED 1
RUN cat .env.production && yarn global add pnpm && SKIP_ENV_VALIDATION=1 pnpm run build
##### RUNNER
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
USER root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs .env.production ./
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]
I am slowly loosing my mind as I have been searching the error for hours. What am I doing wrong?
The docker/build-push-action
action requires you to tell it, if you want to use a context.
So your action needs this addition in the build section:
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
You can see the difference.
Build without context
: https://github.com/RiRa12621/so-test-repo/actions/runs/5818894961/job/15776209523#step:5:158
Build with context: https://github.com/RiRa12621/so-test-repo/actions/runs/5818912126/job/15776262227#step:5:162
(My test builds are obviously still failing because I didn't check in any node files but that shouldn't matter to solve your problem.)