dockernext.jsdockerfile

How to create a Dockerfile with a combined dev and prod environment?


I try to dockerize a Next.js app with a combined image for dev and prod.

So I have the Dockerfile as follows:

FROM node:17.1.0-alpine3.12 AS development
WORKDIR /app
ENV HOST=0.0.0.0
ENV PORT=3000
ENV NODE_ENV=development
EXPOSE 3000
CMD [ "yarn", "dev" ]

FROM node:17.1.0-alpine3.12 AS dependencies
ENV NODE_ENV=production
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile


FROM node:17.1.0-alpine3.12 AS builder
ENV NODE_ENV=development
WORKDIR /app
COPY . .
RUN yarn install --frozen-lockfile && NODE_ENV=production yarn build

FROM node:17.1.0-alpine3.12 AS production
WORKDIR /app
ENV HOST=0.0.0.0
ENV PORT=3000
ENV NODE_ENV=production
COPY --chown=node --from=builder /app/next.config.js ./
COPY --chown=node --from=builder /app/public ./public
COPY --chown=node --from=builder /app/.next ./.next
COPY --chown=node --from=builder /app/yarn.lock /app/package.json ./
COPY --chown=node --from=dependencies /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD [ "yarn", "start" ]

But if I try to build the image with the command:

docker build -t  frontend-prod --target production .

I get this error:

Dockerfile:20
--------------------
  18 |     WORKDIR /app
  19 |     COPY . .
  20 | >>> RUN yarn install --frozen-lockfile && NODE_ENV=production yarn build
  21 |
  22 |     FROM node:17.1.0-alpine3.12 AS production
--------------------
ERROR: failed to solve: process "/bin/sh -c yarn install --frozen-lockfile && NODE_ENV=production yarn build" did not complete successfully: exit code: 1

This is the whole stack trace:

 > [builder 4/4] RUN yarn install --frozen-lockfile && NODE_ENV=production yarn build:
0.614 yarn install v1.22.15
0.718 [1/4] Resolving packages...
0.947 [2/4] Fetching packages...
50.16 info There appears to be trouble with your network connection. Retrying...
83.90 info There appears to be trouble with your network connection. Retrying...
117.2 info There appears to be trouble with your network connection. Retrying...
121.9 info There appears to be trouble with your network connection. Retrying...
181.2 info @next/swc-darwin-arm64@13.5.6: The platform "linux" is incompatible with this module.
181.2 info "@next/swc-darwin-arm64@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-darwin-arm64@13.5.6: The CPU architecture "x64" is incompatible with this module.
181.2 info @next/swc-darwin-x64@13.5.6: The platform "linux" is incompatible with this module.
181.2 info "@next/swc-darwin-x64@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-linux-arm64-gnu@13.5.6: The CPU architecture "x64" is incompatible with this module.
181.2 info "@next/swc-linux-arm64-gnu@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-linux-arm64-musl@13.5.6: The CPU architecture "x64" is incompatible with this module.
181.2 info "@next/swc-linux-arm64-musl@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-win32-arm64-msvc@13.5.6: The platform "linux" is incompatible with this module.
181.2 info "@next/swc-win32-arm64-msvc@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-win32-arm64-msvc@13.5.6: The CPU architecture "x64" is incompatible with this module.
181.2 info @next/swc-win32-ia32-msvc@13.5.6: The platform "linux" is incompatible with this module.
181.2 info "@next/swc-win32-ia32-msvc@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 info @next/swc-win32-ia32-msvc@13.5.6: The CPU architecture "x64" is incompatible with this module.
181.2 info @next/swc-win32-x64-msvc@13.5.6: The platform "linux" is incompatible with this module.
181.2 info "@next/swc-win32-x64-msvc@13.5.6" is an optional dependency and failed compatibility check. Excluding it from installation.
181.2 error @typescript-eslint/parser@6.21.0: The engine "node" is incompatible with this module. Expected version "^16.0.0 || >=18.0.0". Got "17.1.0"
181.2 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
181.2 error Found incompatible module.
------

How to resolve the error?


Solution

  • just look at the backtrace and you will see the real error reason:

    error @typescript-eslint/parser@6.21.0: The engine "node" is incompatible with this module. Expected version "^16.0.0 || >=18.0.0". Got "17.1.0"

    To fix it you can just increase base docker image version

    FROM node:18.0.0-alpine3.12