未验证 提交 111d2df9 编写于 作者: N Nghiệp 提交者: GitHub

[Examples] Optimize with-docker (#17116)

* Fix missing yarn.lock
* Add --frozen-lockfile flag to speed up install deps
* Make sure node_modules do not contains devDependencies
* Add --targe stage docker build
上级 db82f9ce
FROM mhart/alpine-node
WORKDIR /app
COPY . .
RUN yarn install
RUN yarn install --frozen-lockfile
RUN yarn build
EXPOSE 3000
CMD ["yarn", "start"]
# Do the npm install or yarn install in the full image
# Stage 1: Building the code
FROM mhart/alpine-node AS builder
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN yarn --production
RUN yarn build
RUN yarn install --production --frozen-lockfile
# Stage 2: And then copy over node_modules, etc from that stage to the smaller base image
FROM mhart/alpine-node:base as production
# And then copy over node_modules, etc from that stage to the smaller base image
FROM mhart/alpine-node:base
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
# COPY package.json next.config.js .env* ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node_modules/.bin/next", "start"]
CMD ["node_modules/.bin/next", "start"]
\ No newline at end of file
......@@ -22,7 +22,7 @@ Build it with docker:
# build
docker build -t next-app .
# or, use multi-stage builds to build a smaller docker image
docker build -t next-app -f ./Dockerfile.multistage .
docker build --target production -t next-app -f ./Dockerfile.multistage .
```
Alternatively you can add these commands as scripts to your package.json and simply run
......
......@@ -6,7 +6,7 @@
"build": "next build",
"start": "next start",
"build-docker": "docker build -t next-app .",
"build-docker-multistage": "docker build -t next-app -f ./Dockerfile.multistage .",
"build-docker-multistage": "docker build --target production -t next-app -f ./Dockerfile.multistage .",
"run-docker": "docker run --rm -it -p 3000:3000 next-app"
},
"dependencies": {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册