docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.24k stars 541 forks source link

Docker Multi-Stage Build Failing on COPY --from #909

Closed OnerInce closed 1 year ago

OnerInce commented 1 year ago

I am trying to build following multi-stage Dockerfile:

FROM node:16.13-alpine AS base

FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]

However, it fails on the last stage. Error message is: buildx failed with: ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref ixtqd2us9kaftl9v6qm0kc0rt::vteockhvetegcevutywlk57tn: "/app/public": not found.

Dockerfile building fine on local, that means something wrong with caching. I have tried different caching parameters but no luck. My Github Workflow definition as follows:

name: Deploy to Amazon ECS

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    environment: production

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@master
        with:
          version: latest
          buildkitd-flags: --debug

      - name: Build initial image
        uses: docker/build-push-action@v4
        with:
          context: ./frontend-next
          builder: ${{ steps.buildx.outputs.name }}
          file: frontend-next/Dockerfile.prod
          target: runner
          push: false
          tags: latest
          cache-from: type=gha,scope=my_cache
          cache-to: type=gha,mode=max,scope=my_cache
crazy-max commented 1 year ago

mode=max is not a valid attribute for cache-from: https://docs.docker.com/build/cache/backends/gha/#synopsis

Do you have a link to your repo? Hard to tell without logs and full workflow. Also if it works locally what local command did you invoked?

OnerInce commented 1 year ago

It is a private repo unfortunately and yes, image builds all fine locally. using docker build. Added full workflow definition to original message, there is nothing else on logs other than the error message.

crazy-max commented 1 year ago

Also if it works locally what local command did you invoked?

^

OnerInce commented 1 year ago

Also if it works locally what local command did you invoked?

^

docker build -t my_image -f frontend-next/Dockerfile.prod ./frontend-next

crazy-max commented 1 year ago

Well "/app/public": not found means this folder does not exist in your ./frontend-next context. You can find out by adding this step right before - name: Build initial image:

      - run: tree -nh ./frontend-next

Either you have local changes not pushed or they are gitignored.

OnerInce commented 1 year ago

Well, it turns out public folder was empty. So it doesn't exist on Github. There was nothing wrong with caching. Wasted several hours with it :)