From 111d2df90c3718965a5f54c83c3e8f19714c6bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nghi=E1=BB=87p?= Date: Mon, 21 Sep 2020 11:09:37 +0700 Subject: [PATCH] [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 --- examples/with-docker/Dockerfile | 5 ++++- examples/with-docker/Dockerfile.multistage | 26 ++++++++++++++++------ examples/with-docker/README.md | 2 +- examples/with-docker/package.json | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/with-docker/Dockerfile b/examples/with-docker/Dockerfile index 1ca3dd5bac..2d17c906a6 100644 --- a/examples/with-docker/Dockerfile +++ b/examples/with-docker/Dockerfile @@ -1,10 +1,13 @@ FROM mhart/alpine-node WORKDIR /app + COPY . . -RUN yarn install +RUN yarn install --frozen-lockfile + RUN yarn build EXPOSE 3000 + CMD ["yarn", "start"] diff --git a/examples/with-docker/Dockerfile.multistage b/examples/with-docker/Dockerfile.multistage index c8a1b666f1..c877cd1875 100644 --- a/examples/with-docker/Dockerfile.multistage +++ b/examples/with-docker/Dockerfile.multistage @@ -1,16 +1,28 @@ -# 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 diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md index 8528f967cc..055dd2881b 100644 --- a/examples/with-docker/README.md +++ b/examples/with-docker/README.md @@ -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 diff --git a/examples/with-docker/package.json b/examples/with-docker/package.json index 25d305bb2c..388a8f4068 100644 --- a/examples/with-docker/package.json +++ b/examples/with-docker/package.json @@ -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": { -- GitLab