-
I have a problem implementing sharp for Dockerfile. Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly Next.js with sharp works fine for local developing:
next.config.js
} Dockerfile:
.env file:
Sharp is installed in package.json I checked both Next/Vercel tuts:
RUN Docker:
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 25 replies
-
If you have installed sharp in package.json, you do not need to specify sharp location in the |
Beta Was this translation helpful? Give feedback.
-
I figured it out. I removed But I deployed it in my K8s Cluster with Docker and it works as is expected. |
Beta Was this translation helpful? Give feedback.
-
Anyone find a solution yet? Same issue here. Using:
All I find is a lot of people struggling with this, but never a solution. |
Beta Was this translation helpful? Give feedback.
-
I figured it out! The reason is that I used different architecture and platform of Node.js at build and runtime. My dockerfile:
Ref: lovell/sharp#2312
The sharp bin version built in first build stage doesn't match
So I just use the
Hope it helps 😄 |
Beta Was this translation helpful? Give feedback.
-
I had the same issue. I was doing my dev on macOS, dev and build was OK on Mac. I made a tar of the docker image on my Mac, and load it on a prod server. Impossible to have image optimization. |
Beta Was this translation helpful? Give feedback.
-
The problem is that the example in with-docker repo is incorrect. COPY --from=deps /app/node_modules ./node_modules
COPY . . First it copies node_modules from deps and then it overrides them by copying from local directory There is my version of slightly modified Dockerfile which works correctly: # Install dependencies only when needed
FROM node:16-alpine 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 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
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY pages/ ./pages/
COPY public/ ./public/
COPY server/ ./server/
COPY src/ ./src/
COPY .eslintignore \
.eslintrc.json \
.gitignore \
next-env.d.ts \
next.config.js \
tsconfig.json \
./
COPY --from=deps /app/package.json ./package.json
COPY --from=deps /app/package-lock.json ./package-lock.json
COPY --from=deps /app/node_modules ./node_modules
# Add env for production
# COPY .docker/production/.env.local .env.local
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/.env.local ./.env.local
COPY --from=builder /app/public ./public
# 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
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"] P.s. also note that it copies only the required dirs and files from local directory to speed up the build |
Beta Was this translation helpful? Give feedback.
-
I had the same issue and couldn't figure out how to solve it with Turns out that the equivalent to the command RUN npm install -g --arch=x64 --platform=linux --libc=glibc sharp with RUN pnpm i -g --config.arch=x64 --config.platform=linux --config.libc=glibc sharp After more tinkering, pinning the /app $ node
Welcome to Node.js v18.19.1.
Type ".help" for more information.
> sharp = require("/app/node_modules/sharp")
Uncaught Error:
Something went wrong installing the "sharp" module
Cannot find module '../build/Release/sharp-linuxmusl-x64.node'
Require stack:
- /app/node_modules/.pnpm/[email protected]/node_modules/sharp/lib/sharp.js
- /app/node_modules/.pnpm/[email protected]/node_modules/sharp/lib/constructor.js
- /app/node_modules/.pnpm/[email protected]/node_modules/sharp/lib/index.js
- <repl>
Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current linuxmusl-x64 runtime: "npm install --platform=linuxmusl --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install Turns out that I'm using alpine linux, so apparently forcing the usage of RUN pnpm i --config.arch=x64 --config.platform=linux --config.libc=musl [email protected] Notice the removal of the global flag, the specific After confirming the fix, I tried to go back to the latest version (0.33.2) by removing the version pinning, but it broke again with that single change, confirming what was suggested by @vvbbnn00, who links back to #59516. For reference, my final Dockerfile looks like the following: Expand Dockerfile# https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
FROM node:18-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 yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# ENV PNPM_HOME=/usr/local/bin
RUN corepack enable pnpm && pnpm i --frozen-lockfile
RUN pnpm i --config.arch=x64 --config.platform=linux --config.libc=musl [email protected]
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
# https://github.com/vercel/next.js/discussions/36935
# Uncomment the following line if there are permission issues with .next/cache directory
# RUN mkdir -p /app/.next/cache && chown nextjs:nodejs /app/.next/cache
RUN mkdir -p /app/.next/cache
# Persist the next cache in a volume
VOLUME ["/app/.next/cache"]
COPY . .
COPY --from=deps /app/node_modules ./node_modules
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
ENV NEXT_SHARP_PATH=/app/node_modules/sharp
RUN corepack enable pnpm && pnpm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# COPY --from=deps --chown=nextjs:nodejs /usr/local/lib/node_modules/sharp /usr/local/lib/node_modules/sharp
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
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 ["node", "server.js"] |
Beta Was this translation helpful? Give feedback.
-
if you are using
"pnpm": {
"supportedArchitectures": {
"os": [
"current"
],
"cpu": [
"x64",
"arm64"
]
}
}
RUN pnpm install --frozen-lockfile --prefer-offline
RUN pnpm install [email protected] don't forget to remove if this is not working, then do |
Beta Was this translation helpful? Give feedback.
-
I think easiest solution is installing sharp in docker before running the server. I completing every step to build my app, after that I am installing sharp before running my server. Full Docker# Install dependencies only when needed
FROM node:20.7.0-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN npm i
# Rebuild the source code only when needed
FROM node:20.7.0-alpine AS builder
ENV NEXT_TELEMETRY_DISABLED 1
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Production image, copy all the files and run next
FROM node:20.7.0-alpine AS runner
ENV NEXT_TELEMETRY_DISABLED 1
ENV PORT 3000
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
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
# Install sharp
RUN npm i sharp
USER nextjs
EXPOSE 3000
ENTRYPOINT ["node", "server.js"] |
Beta Was this translation helpful? Give feedback.
If you have installed sharp in package.json, you do not need to specify sharp location in the
.env
file.