-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Next JS with Sharp in docker container #3877
Comments
It looks like you're using yarn v1:
Please see #3871 |
@lovell Thank you for a quick response. However I'm not sure how to solve my problem. Would you mind explaining how I can solve this issue in my project? In my mind I use NPM and not yarn except in my Dockerfile on line 30: "RUN yarn build". I've tried replacing this with "RUN npm run build" but I still have the error. |
Make sure you're not copying |
I replaced:
with this:
"Make sure you're not copying node_modules into the container from outside." |
The These questions relate to Docker and Node.js rather than being specific to sharp, so might be more suitable for a more general site such as StackOverflow. |
@lovell It's from Next JS example Dockerfile. |
However... locking sharp to version 0.32.6 seems to have solved the problem. |
I tried running https://github.com/vercel/next.js/tree/canary/examples/with-docker locally and can reproduce - the problem is due to vercel/nft#371 |
I see... so it wasn't just me then. I'm somewhat pleased to hear that 😅 Would you say sticking to version 0.32.6 for now is a stable choice? |
The docs you need are here - https://nextjs.org/docs/pages/api-reference/next-config-js/output Either switch off tracing or specify I'll close this as there's already one sharp issue tracking this and there's nothing else we can do. |
Where did you install sharp? I tried locking it on dependencies and i'm still getting the same issues on my container. I'm also using the nextjs official dockerfile example. FROM node:20.10-alpine AS base
# Install dependencies only when needed
FROM base 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
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /root/.npm /root/.npm
COPY . .
# Generate Prisma Files
RUN npx prisma generate
ENV NEXT_SHARP_PATH=/tmp/node_modules/sharp
RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_SHARP_PATH=/tmp/node_modules/sharp
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
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 /app/prisma ./prisma
USER nextjs
EXPOSE 3000
ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["npm", "run", "start:migrate:prod"] Package.json
|
This comment was marked as off-topic.
This comment was marked as off-topic.
@99lalo did you solve this problem? i'm kinda doing the same thing & have already installed sharp in package.json but docker container can't find it. it is probably bcz my |
So basically you just need to manually copy |
This comment was marked as off-topic.
This comment was marked as off-topic.
I found a root cause in my case: It was a discrepancy between dependencies of I'm kinda surprised that no one mentioned the binary compatibility of dependencies. By looking at Sharp's The fix is super easy: do the proper dependencies installation inside the Docker with the target platform. Today's package managers are clever enough to install the correct binary variant, even if the lock file is followed. |
If you're using npm this is probably due to npm/cli#4828 Please see https://sharp.pixelplumbing.com/install#cross-platform for more information if you are attempting to share lockfiles and For anyone else reading this and still having problems, please note there were bugs in previous versions of Next.js relating to its "standalone" build feature that prevented some dependencies from being included. Please ensure you are using the latest version of all your dependencies, including both Next.js and sharp. Finally please avoid yarn v1 and its lockfiles. |
Possible bug
Is this a possible bug in a feature of sharp, unrelated to installation?
npm install sharp
completes without error.node -e "require('sharp')"
completes without error.If you cannot confirm both of these, please open an installation issue instead.
Are you using the latest version of sharp?
sharp
as reported bynpm view sharp dist-tags.latest
.If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.
If you are using another package which depends on a version of
sharp
that is not the latest, please open an issue against that package instead.What is the output of running
npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp
?System:
OS: Windows 11 10.0.22631
CPU: (16) x64 AMD Ryzen 9 4900HS with Radeon Graphics
Memory: 10.61 GB / 31.42 GB
Binaries:
Node: 18.18.2 - C:\Program Files\nodejs\node.EXE
npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
npmPackages:
sharp: ^0.33.0 => 0.33.0
The issue
I'm trying to use sharp in my conatainer with Next JS:
My Dockerfile:
My Package.json
After running my container on this image I get the following error:
Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production
The text was updated successfully, but these errors were encountered: