我有我的 dokku 集群,并且希望使用 docker 而不是 buildpack 来部署它。
[vercel/next.js]你们有用于部署 next.js 的 dockerfile 吗?
回答
总的来说,我想说你们已经远远击败了 create-react-app!
FROM node:6-onbuild
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
RUN npm run build
EXPOSE 80
正如我所说,我正在尝试在我的 dokku 上运行这个东西,并且暴露 80 有点成功,但现在我无法让 letencrypt 与这个新应用程序一起使用。会继续调查...
@ojosdegris 感谢您的研究。能够在任何地方部署它确实很重要。同时,您可以使用now
.
$ cd your-project
$ now
?
我在解决这个问题方面取得了一些进展,但遇到了障碍。
.dockerignore:
node_modules
npm-debug.log
.next
Dockerfile:
FROM node:argon
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
RUN npm run build
# Bundle app source
COPY . /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]
重击:
$ docker build -t my-nodejs-app .
$ docker run -it -P --rm --name my-running-app my-nodejs-app
$ open http://localhost:32770/ # port number found using kitematic ui
最后,我得到了 404:
啊,我刚刚切换到node:6-onbuild
而不是node:argon
一切正常?
也许我们可以将其添加到 wiki 并关闭此问题?
我使用 Dockerfile 来构建镜像。图像大小为 776 MB,是否太大? node:6-onbuild 镜像已经有 651 MB。有没有更苗条的节点镜像可以用?
@johannchen 尝试节点:alpine
应该可以与任何 Node docker 镜像一起使用。除了运行npm run build
和之外,Next 不需要任何特殊的东西npm run start
。
这里提到的 Dockerfile 对我不起作用`
找不到
pages
目录。请在项目根目录下创建一个`
这对我有用。问题是由尝试运行的脚本Couldn't find a pages directory. Please create one under the project root
引起的,但此时文件尚未复制。postinstall
next build
我的 Dockerfile:
FROM node:11-alpine
WORKDIR /workspace
COPY package.json yarn.lock /workspace/
RUN yarn install
COPY . .
RUN yarn build
CMD ["yarn", "start"]
使用此 Dockerfile —— 起始大小约为 130MB。
FROM node:12-alpine as build
COPY . /src
WORKDIR /src
RUN npm ci
RUN npm run build
RUN npm prune --production
FROM node:12-alpine
WORKDIR /usr/app
COPY --from=build /src/node_modules /usr/app/node_modules
COPY --from=build /src/package.json /usr/app/package.json
COPY --from=build /src/.next /usr/app/.next
ENV NODE_ENV production
CMD ["npm", "start"]
@Meemaw 非常感谢
以下是我的稍作生产准备的修改版本
FROM node:13.7.0-alpine3.11 as build
COPY . .
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
RUN npm run build
FROM node:13.7.0-alpine3.11
COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build .next .next
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
EXPOSE 3000
CMD npm start
@whs-dot-hk 谢谢。
添加公共文件夹
FROM node:12-alpine as build
COPY . .
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
RUN npm run build
FROM node:12-alpine
COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build .next .next
COPY --from=build public public
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
EXPOSE 3000
CMD npm start
这是我正在运行的应用程序 Dockerfile在根文件夹中创建它 -
来自节点:12
# Create app directory
RUN mkdir /var/movable/ && mkdir /var/movable/app
WORKDIR /var/movable/app
# Installing dependencies
COPY package*.json /var/movable/app/
RUN npm install
# Copying source files
COPY . /var/movable/app
# Building app
RUN npm run build
# Running the app
CMD "npm" "run" "start"
感谢@whs-dot-hk 和@dickamin
添加next.config.js
(例如,如果您用于basePath
路由)
FROM node:14-alpine as build
COPY . .
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
RUN npm run build
FROM node:14-alpine
COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build next.config.js next.config.js
COPY --from=build .next .next
COPY --from=build public public
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install --production
EXPOSE 3000
CMD npm start
带修剪
FROM node:14-alpine as BUILD_IMAGE
WORKDIR /app
COPY package.json package-lock.json ./
# install dependencies
RUN npm install
COPY . .
# build
RUN npm run build
# remove dev dependencies
RUN npm prune --production
FROM node:14-alpine
WORKDIR /app
# copy from build image
COPY --from=BUILD_IMAGE /app/package.json ./package.json
COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
COPY --from=BUILD_IMAGE /app/next.config.js next.config.js
COPY --from=BUILD_IMAGE /app/.next ./.next
COPY --from=BUILD_IMAGE /app/public ./public
EXPOSE 3000
CMD ["npm", "start"]