From a265038bf6e8bf56ce7c6a98d58337ae436028af Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 5 Sep 2022 16:42:10 +0100 Subject: [PATCH 1/6] Update Dockerfile --- Dockerfile | 57 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 255b45f8..65d49570 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,33 @@ -FROM node:16-alpine AS base +# syntax = docker/dockerfile:latest + +# Install dependencies only when needed +FROM node:16-alpine AS deps + +WORKDIR /app + +COPY package.json pnpm-lock.yaml* ./ + +RUN < Date: Mon, 5 Sep 2022 19:44:58 +0100 Subject: [PATCH 2/6] Update Dockerfile for --link --- Dockerfile | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 65d49570..68ce887c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,12 +5,16 @@ FROM node:16-alpine AS deps WORKDIR /app -COPY package.json pnpm-lock.yaml* ./ +COPY --link package.json pnpm-lock.yaml* ./ RUN < Date: Mon, 5 Sep 2022 22:17:58 +0100 Subject: [PATCH 3/6] Update Dockerfile and add healthcheck --- Dockerfile | 15 +++++++++++---- healthcheck.js | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100755 healthcheck.js diff --git a/Dockerfile b/Dockerfile index 68ce887c..026189dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,10 +45,17 @@ ENV NODE_ENV production WORKDIR /app -COPY --link --from=builder /app/next.config.js /app/.next/standalone ./ -COPY --link --from=builder /app/public ./public/ -COPY --link --from=builder /app/package.json ./package.json -COPY --link --from=builder /app/.next/static ./.next/static/ +# Copy files from context +COPY --link package.json next.config.js ./ +COPY --link --chmod=755 healthcheck.js ./ +COPY --link /public ./public + +# Copy files from builder +COPY --link --from=builder /app/.next/standalone ./ +COPY --link --from=builder /app/.next/static/ ./.next/static/ + +HEALTHCHECK --interval=12s --timeout=12s --start-period=30s \ + CMD node ./healthcheck.js EXPOSE 3000 ENV PORT 3000 diff --git a/healthcheck.js b/healthcheck.js new file mode 100755 index 00000000..8e2301f8 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,19 @@ +var http = require("http"); +var options = { + host: "localhost", + port: "3000", + timeout: 2000, +}; +var request = http.request(options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + if (res.statusCode == 200) { + process.exit(0); + } else { + process.exit(1); + } +}); +request.on("error", function (err) { + console.log("ERROR"); + process.exit(1); +}); +request.end(); \ No newline at end of file From 36d1a9c7385c49010cfb55466e976b42ae3ca07b Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 5 Sep 2022 22:55:14 +0100 Subject: [PATCH 4/6] Update healthcheck to work with PORT variable --- Dockerfile | 5 +++-- healthcheck.js | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 026189dd..3fa1367f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -54,9 +54,10 @@ COPY --link /public ./public COPY --link --from=builder /app/.next/standalone ./ COPY --link --from=builder /app/.next/static/ ./.next/static/ +EXPOSE 3000 +ENV PORT='3000' + HEALTHCHECK --interval=12s --timeout=12s --start-period=30s \ CMD node ./healthcheck.js -EXPOSE 3000 -ENV PORT 3000 CMD ["node", "server.js"] diff --git a/healthcheck.js b/healthcheck.js index 8e2301f8..cd9b288a 100755 --- a/healthcheck.js +++ b/healthcheck.js @@ -1,7 +1,10 @@ var http = require("http"); + +const PORT = process.env.PORT || "3000"; + var options = { host: "localhost", - port: "3000", + port: PORT, timeout: 2000, }; var request = http.request(options, (res) => { @@ -16,4 +19,4 @@ request.on("error", function (err) { console.log("ERROR"); process.exit(1); }); -request.end(); \ No newline at end of file +request.end(); From b023801fe4e9e0d1bb83849c190f7c90957b8426 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 6 Sep 2022 21:39:31 +0100 Subject: [PATCH 5/6] Update Dockerfile to include cache mounts + update healthcheck to new endpoint --- Dockerfile | 27 +++++++++++++-------------- healthcheck.js | 22 ---------------------- 2 files changed, 13 insertions(+), 36 deletions(-) delete mode 100755 healthcheck.js diff --git a/Dockerfile b/Dockerfile index 3fa1367f..ca819d24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,17 +7,17 @@ WORKDIR /app COPY --link package.json pnpm-lock.yaml* ./ -RUN < { - console.log(`STATUS: ${res.statusCode}`); - if (res.statusCode == 200) { - process.exit(0); - } else { - process.exit(1); - } -}); -request.on("error", function (err) { - console.log("ERROR"); - process.exit(1); -}); -request.end(); From 8e2bebcfd91854207b32f8d108934a9473f95a87 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 6 Sep 2022 21:41:53 +0100 Subject: [PATCH 6/6] Updated EXPOSE to variable --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ca819d24..065135bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -53,8 +53,8 @@ COPY --link /public ./public COPY --link --from=builder /app/.next/standalone ./ COPY --link --from=builder /app/.next/static/ ./.next/static/ -EXPOSE 3000 ENV PORT 3000 +EXPOSE $PORT HEALTHCHECK --interval=10s --timeout=3s --start-period=20s \ CMD wget --no-verbose --tries=1 --spider --no-check-certificate http://localhost:$PORT/api/healthcheck || exit 1