From 74ddeacab05b16a2961920d9423481d64d3c37c3 Mon Sep 17 00:00:00 2001 From: ChiDaniel Date: Thu, 9 Jan 2025 10:32:13 +0100 Subject: [PATCH] docker included --- .dockerignore | 25 ++++++++++++++ .gitignore | 12 +++++++ Dockerfile | 66 +++++++++++++++++++++++++++++++++++++ app/components/main-nav.tsx | 2 +- cache-handler.js | 33 +++++++++++++++++++ next.config.mjs | 4 +++ 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 cache-handler.js diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8258c23 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +node_modules +npm-debug.log +Dockerfile +docker-compose.yml + +# Ignore node_modules +node_modules + +# Ignore logs +logs +*.log + +# Ignore build output +.next +out + +# Ignore environment variables +.env +.env.local +.env.development.local +.env.test.local +.env.production.local + +# Miscellaneous +.DS_Store \ No newline at end of file diff --git a/.gitignore b/.gitignore index d26b105..0ced391 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,10 @@ # testing /coverage +# Ignore logs +logs +*.log + # next.js /.next/ /out/ @@ -34,6 +38,14 @@ yarn-error.log* .vercel .editorconfig + + +# Production +build/ + # typescript *.tsbuildinfo next-env.d.ts + +# Ignore local Docker files +docker-compose.override.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e1eb45a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,66 @@ +# syntax=docker.io/docker/dockerfile:1 + +FROM node:18-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED=1 + +RUN \ + if [ -f yarn.lock ]; then yarn run build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production +# Uncomment the following line in case you want to disable telemetry during runtime. +# ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT=3000 + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +ENV HOSTNAME="0.0.0.0" +CMD ["node", "server.js"] \ No newline at end of file diff --git a/app/components/main-nav.tsx b/app/components/main-nav.tsx index f3704b4..e335774 100644 --- a/app/components/main-nav.tsx +++ b/app/components/main-nav.tsx @@ -15,7 +15,7 @@ export function MainNav() { {/* */} -

StickeyLikeMickey🚀

+

Stick with me

{siteConfig.name} diff --git a/cache-handler.js b/cache-handler.js new file mode 100644 index 0000000..cd97e1a --- /dev/null +++ b/cache-handler.js @@ -0,0 +1,33 @@ +const cache = new Map(); + +module.exports = class CacheHandler { + constructor(options) { + this.options = options; + } + + async get(key) { + // This could be stored anywhere, like durable storage + return cache.get(key); + } + + async set(key, data, ctx) { + // This could be stored anywhere, like durable storage + cache.set(key, { + value: data, + lastModified: Date.now(), + tags: ctx.tags, + }); + } + + async revalidateTag(tags) { + // tags is either a string or an array of strings + tags = [tags].flat(); + // Iterate over all entries in the cache + for (let [key, value] of cache) { + // If the value's tags include the specified tag, delete this entry + if (value.tags.some((tag) => tags.include(tag))) { + cache.delete(key); + } + } + } +}; diff --git a/next.config.mjs b/next.config.mjs index 3d71ee0..14dd98e 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,5 +1,9 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + /* output: "standalone", */ + /* cacheHandler: require.resolve("./cache-handler.js"), + cacheMaxMemorySize: 0, // disable default in-memory caching + */ reactStrictMode: true, images: { domains: ["cdn.sanity.io"],