From 5637a26f57f37ee6cec66f00e557d78ceb38433f Mon Sep 17 00:00:00 2001 From: tan <13320385140@163.com> Date: Tue, 22 Apr 2025 16:09:15 +0800 Subject: [PATCH 1/2] Building for docker --- .gitignore | 3 +- Dockerfile | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ next.config.js | 2 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/.gitignore b/.gitignore index 86b92f2..865d6db 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ yarn-error.log* # typescript *.tsbuildinfo -next-env.d.ts \ No newline at end of file +next-env.d.ts +/.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9b9fd2e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,75 @@ +# syntax=docker.io/docker/dockerfile:1 + +FROM node:18-alpine AS base + +# Step 1. Rebuild the source code only when needed +FROM base AS builder + +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./ +# Omit --production flag for TypeScript devDependencies +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; \ + # Allow install without lockfile, so example works even without Node.js installed locally + else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \ + fi + +COPY src ./src +COPY public ./public +COPY next.config.js . +COPY tsconfig.json . + +# Environment variables must be present at build time +# https://github.com/vercel/next.js/discussions/14030 +ARG ENV_VARIABLE +ENV ENV_VARIABLE=${ENV_VARIABLE} +ARG NEXT_PUBLIC_ENV_VARIABLE +ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE} + +# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry +# Uncomment the following line to disable telemetry at build time +# ENV NEXT_TELEMETRY_DISABLED 1 + +# Build Next.js based on the preferred package manager +RUN \ + if [ -f yarn.lock ]; then yarn build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then pnpm build; \ + else npm run build; \ + fi + +# Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here + +# Step 2. Production image, copy all the files and run next +FROM base AS runner + +WORKDIR /app + +# Don't run production as root +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs +USER 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 + +# Environment variables must be redefined at run time +ARG ENV_VARIABLE +ENV ENV_VARIABLE=${ENV_VARIABLE} +ARG NEXT_PUBLIC_ENV_VARIABLE +ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE} + +# Uncomment the following line to disable telemetry at run time +# ENV NEXT_TELEMETRY_DISABLED 1 + +# Note: Don't expose ports here, Compose will handle that for us + +CMD ["node", "server.js"] diff --git a/next.config.js b/next.config.js index c59957d..cc5ffee 100644 --- a/next.config.js +++ b/next.config.js @@ -1,6 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, + output: 'standalone', }; - module.exports = nextConfig; \ No newline at end of file From 0c8ee10b26ecaf129b72d70fa717bb718685cf08 Mon Sep 17 00:00:00 2001 From: tan <13320385140@163.com> Date: Tue, 22 Apr 2025 16:09:18 +0800 Subject: [PATCH 2/2] Building for docker --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c96d357..3a405ce 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,13 @@ npm run build # or yarn build ``` +### Building for docker +```bash +npm install +docker build -t transferfiles:0.0.1 . +docker run -p 3000:3000 transferfiles:0.0.1 +``` + ## Technology Stack @@ -142,4 +149,4 @@ If you find TransferFiles useful, please consider supporting the project:
Made with ❤️ for a more open and private web -
\ No newline at end of file +