Проблемы установки зависимостей и разрешений при настройке пользовательского сервера HTTPS

У меня возникли проблемы с настройкой Docker для приложения Next.js, особенно после установки пользовательского сервера с HTTPS.

Вот проблемы, с которыми я столкнулся:

< p> введите здесь описание изображения

введите описание изображения здесь

Будем очень признательны за любые идеи и предложения.

FROM node:20-alpine AS base

# Step 1. Rebuild the source code only when needed
FROM base AS builder

WORKDIR /app

# Add non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
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; \
  else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
  fi

# Copy source files
COPY src ./src
COPY public ./public
COPY next.config.js .
COPY tsconfig.json .
COPY holiday-kr.d.ts .
COPY server.custom.js .
COPY certificates ./certificates

# Set correct permissions
RUN chown -R nextjs:nodejs /app

# 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

# 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 && \
    adduser --system --uid 1001 nextjs

# Set the correct permission for the /app directory
RUN mkdir -p /app && chown -R nextjs:nodejs /app

# Copy built artifacts and set permissions
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone/server.js ./server.js
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/next ./node_modules/next
COPY --from=builder --chown=nextjs:nodejs /app/server.custom.js .
COPY --from=builder --chown=nextjs:nodejs /app/certificates ./certificates

# Ensure all files are owned by nextjs user
RUN chown -R nextjs:nodejs /app

# Switch to non-root user
USER nextjs

CMD ["node", "server.custom.js"]
const { createServer: https } = require('https');
const { createServer: http } = require('http');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');
const path = require('path');
const { config } = require(path.join(
  process.cwd(),
  '.next',
  'required-server-files.json',
));

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const app = next({ dev: dev, hostname, conf: config });
const handle = app.getRequestHandler();

process.env.NODE_ENV = 'production';

const ports = {
  http: 3000,
  https: 3001,
};

const httpsOptions = {
  cert: fs.readFileSync('certificates/localhost.pem'),
  key: fs.readFileSync('certificates/localhost-key.pem'),
};

app.prepare().then(() => {
  http((req, res) => {
    const parsedUrl = parse(req.url, true);
    handle(req, res, parsedUrl);
  }).listen(ports.http, (err) => {
    if (err) {
      throw err;
    }
    console.log(`> Ready on http://${hostname}:${ports.http}`);
  });

  https(httpsOptions, (req, res) => {
    const parsedUrl = parse(req.url, true);
    handle(req, res, parsedUrl);
  }).listen(ports.https, (err) => {
    if (err) {
      throw err;
    }
    console.log(`> Ready on http://${hostname}:${ports.https}`);
  });
});

Вениамин
Вопрос задан10 июля 2024 г.

1 Ответ

2
Тарас
Ответ получен1 сентября 2024 г.

Ваш ответ

Загрузить файл.