簡體   English   中英

Next.js。 docker 映像所需的文件

[英]Next.js. Required files for docker image

我正在使用 Docker 創建一個 Next.js 項目,但是每次運行docker build時,Z05B6053C3008B DA.BDA40AF6Z6 圖像都是我的目標是減小 docker 圖像的大小。 為此,我開始使用zeit/pkg ,但它不能正常工作。

問題:

Next.js 中需要哪些文件來docker build (我在每個教程中都看到了COPY..
以及如何減小 docker 圖像的大小?

我遵循了本教程: https://medium.com/@evenchange4/deploy-a-commercial-next-js-application-with-pkg-and-docker-5c73d4af2ee

Dockerfile:

FROM node:lts-alpine as build-env
WORKDIR /app

COPY . .

RUN yarn install --pure-lockfile --ignore-engines
ENV NODE_ENV=production
RUN yarn run build
RUN yarn run pkg

FROM node:alpine

RUN apk update && \
  apk add --no-cache libstdc++ libgcc ca-certificates && \
  rm -rf /var/cache/apk/*

WORKDIR /app

COPY --from=build-env /app/pkg .

ENV NODE_ENV=production

EXPOSE 8080

CMD ./next-app

package.json:

{
  "name": "next-app",
  "bin": "server.js",
  "pkg": {
    "assets": [
      ".next/**/*"
    ],
    "scripts": [
      ".next/**/*.js"
    ]
  },
  "scripts": {
    "test": "jest",
    "dev": "next",
    "build": "next build",
    "start": "NODE_ENV=production node server.js",
    "pkg": "pkg . -t node13-linux-x64 -o pkg"
  },
  "dependencies": {
    "next": "^9.4.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "devDependencies": {
    "pkg": "4.4.8",
    "typescript": "^3.9.2"
  }
}

我知道這個問題被問到已經超過 5 個月了,但我遇到了同樣的問題。

我通過在 docker 中設置多階段構建解決了這個問題,並且只復制了運行生產 nextjs 應用程序所需的文件。

我的 Dockerfile

# Build the app
FROM node:14-alpine as build
WORKDIR /app

COPY . .
RUN npm ci
RUN npm run build
COPY ./.next ./.next


# Run app
FROM node:14-alpine

# Only copy files required to run the app
COPY --from=build /app/.next ./
COPY --from=build /app/package.json ./
COPY --from=build /app/package-lock.json ./

EXPOSE 3000

# Required for healthcheck defined in docker-compose.yml
# If you don't have a healthcheck that uses curl, don't install it
RUN apk --no-cache add curl

# By adding --production npm's devDependencies are not installed
RUN npm ci --production
RUN ./node_modules/.bin/next telemetry disable

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

USER nextjs
CMD ["npm", "start"]

摘自我的 package.json

  "dependencies": {
    "next": "^9.5.5",
    "next-compose-plugins": "^2.2.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM