Next.js。 docker 图片所需的文件
Next.js. Required files for docker image
我正在使用 Docker 创建一个 Next.js 项目,但每次我 运行 docker build
,docker 图像都是 300 MB+。我的目标是减小 docker 图像的大小。为此,我开始使用 zeit/pkg,但它无法正常工作。
问题:
docker build
Next.js 中需要哪些文件? (我在每个教程中看到 COPY . .)
我怎样才能减小 docker 图片的大小?
Docker文件:
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"
},
我正在使用 Docker 创建一个 Next.js 项目,但每次我 运行 docker build
,docker 图像都是 300 MB+。我的目标是减小 docker 图像的大小。为此,我开始使用 zeit/pkg,但它无法正常工作。
问题:
docker build
Next.js 中需要哪些文件? (我在每个教程中看到 COPY . .)
我怎样才能减小 docker 图片的大小?
Docker文件:
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"
},